Introduction
Use these guides to add Yjs-based CRDT to your project with Velt. Pick the React Hook wrapper for the fastest integration in React apps, or the core library for other frameworks and custom implementations.Setup
Step 1: Install Dependencies
npm:yarn:
Step 2: Initialize Velt in your app
Initialize the Velt client by following the Velt Setup Docs. This is required for the collaboration engine to work.Step 3: Initialize a CRDT store
Step 4: Set or update the store value
Set or update local changes to the store.Step 5: Listen for changes
Listen and subscribe for updates from local and remote peers.Step 6: Save and restore versions
Create checkpoints and roll back when needed.APIs
Custom Encryption
Encrypt CRDT data before it’s stored in Velt by registering a custom encryption provider. For CRDT methods, input data is of typeUint8Array | number[]
.
Initialization
useVeltCrdtStore
React hook to create and sync a collaborative CRDT store.- Params: useveltcrdtstore
- Returns: Store properties and methods
Common Methods
These methods are available in both React and non-React frameworks.update
Update the store value and sync to peers.- Params:
newValue: T
- Returns:
void
saveVersion
Save a snapshot of the current state as a named version.- Params:
versionName: string
- Returns:
Promise<string>
getVersions
Fetch all saved versions.- Returns:
Promise<Version[]>
getVersionById
Fetch a specific version by ID.- Params:
versionId: string
- Returns:
Promise<Version | null>
setStateFromVersion
Restore the store state from a specific version object.- Params:
version: Version
- Returns:
Promise<void>
React-Specific Methods
value
Current value of the store (React hook only).- Type:
T | null
versions
List of all stored versions (React hook only).- Type:
Version[]
store
Underlying Velt Store instance (React hook only).- Type:
Store<T> | null
restoreVersion()
Restore the store to a specific version by ID (React hook only).- Params:
versionId: string
- Returns:
Promise<boolean>
Non-React Specific Methods
getValue
Get the current store value (non-React frameworks only).- Returns:
T
subscribe
Subscribe to changes in the store (non-React frameworks only).- Params:
(newValue: T) => void
- Returns:
() => void
(unsubscribe)
destroy
Destroy the store, cleaning up resources and listeners (non-React frameworks only).- Returns:
void
Yjs Integration Methods
getDoc
Get the underlying Yjs document.- Returns:
Y.Doc
getProvider
Get the provider instance for the store.- Returns:
Provider
getText
Get the Y.Text instance if store type is ‘text’.- Returns:
Y.Text | null
getXml
Get the Y.XmlFragment instance if store type is ‘xml’.- Returns:
Y.XmlFragment | null