@veltdev/codemirror-crdt-react library enables real-time collaborative editing on CodeMirror Editors. The collaboration editing engine is built on top of Yjs and Velt SDK.
Prerequisites
- Node.js (v14 or higher)
- React (v16.8 or higher for hooks)
- A Velt account with an API key (sign up)
- Optional: TypeScript for type safety
Setup
Step 1: Install Dependencies
Install the required packages:- React / Next.js
- Other Frameworks
Step 2: Setup Velt
Initialize the Velt client by following the Velt Setup Docs. This is required for the collaboration engine to work.Step 3: Initialize Velt CRDT Extension
- Initialize the Velt CRDT extension and use it to initialize the CodeMirror editor.
- Pass an
editorIdto uniquely identify each editor instance you have in your app. This is especially important when you have multiple editors in your app.
- React / Next.js
- Other Frameworks
Notes
- Unique editorId: Use a unique
editorIdper editor instance. - Wait for auth: Initialize the store only after the Velt client is ready.
- Use yCollab: Pass the store’s Yjs text, awareness, and undo manager to
yCollab. - Initialize and clean up: Destroy the
EditorViewand store on unmount to avoid leaks.
Testing and Debugging
To test collaboration:- Login two unique users on two browser profiles and open the same page in both.
- Edit in one window and verify changes and cursors appear in the other.
- Cursors not appearing: Ensure each editor has a unique
editorIdand users are authenticated. Also ensure that you are logged in with two different users in two different browser profiles. - Editor not loading: Confirm the Velt client is initialized and the API key is valid.
- Disconnected session: Check network.
Complete Example
- React / Next.js
- Other Frameworks
- Live Demo
Open Live Demo In Larger Window | View Demo Repo
Complete Implementation
APIs
Custom Encryption
You can encrypt CRDT data before it’s stored in Velt by registering a custom encryption provider. For CRDT methods, input data is of typeUint8Array | number[].
- React / Next.js
- Other Frameworks
Initialization
- React / Next.js
- Other Frameworks
useVeltCodeMirrorCrdtExtension()
React hook that provides real-time collaborative editing on CodeMirror editor with Yjs and Velt SDK.- Signature:
useVeltCodeMirrorCrdtExtension(config: VeltCodeMirrorCrdtExtensionConfig) - Params: VeltCodeMirrorCrdtExtensionConfig
editorId: Unique identifier for the editor instance.initialContent: Initial content for the editor.debounceMs: Debounce time for update propagation (ms).
- Returns: VeltCodeMirrorCrdtExtensionResponse
store: CodeMirror CRDT store instance.isLoading: Whether the store is initialized.falseonce store is initialized,trueby default.
Store Methods
store.getStore()
Get the underlyingStore<string> that manages document state and synchronization.
- Returns:
Store<string>
store.getYDoc()
Access the Yjs document used for collaborative editing.- Returns:
Y.Doc
store.getYText()
Get the shared Y.Text bound to the current CodeMirror document.- Returns:
Y.Text | null
store.getAwareness()
Access the Yjs Awareness instance associated with the store.- Returns:
Awareness
store.getUndoManager()
Access the Yjs UndoManager for collaborative undo/redo.- Returns:
Y.UndoManager
store.destroy()
Cleans up all resources, listeners, and destroys the store instance. Call this when unmounting the editor to prevent memory leaks.- Returns:
void

