@lexical/yjs package, which integrates with Yjs for conflict-free replicated data types (CRDTs).
Overview
The collaboration system enables:- Multi-user editing with automatic conflict resolution
- Cursor presence showing where other users are typing
- Undo/Redo that works correctly with concurrent edits
- Offline support with automatic sync when reconnected
Setup
import { Doc } from 'yjs';
import { WebsocketProvider } from 'y-websocket';
const doc = new Doc();
const provider = new WebsocketProvider(
'ws://localhost:1234',
'my-document-id',
doc,
);
React Integration
Use theCollaborationPlugin for React applications:
packages/lexical-react/src/LexicalCollaborationPlugin.tsx for implementation details.
Cursor Presence
Show where other users are editing:User Awareness
Track connected users:packages/lexical-yjs/src/index.ts:99-152 for awareness implementation.
Undo/Redo with Collaboration
Create a collaborative undo manager:Sync Strategies
WebSocket Provider
Best for centralized server architecture:WebRTC Provider
Best for peer-to-peer without a central server:IndexedDB Provider
For offline persistence:Server Setup
Example WebSocket server usingy-websocket:
packages/lexical-playground/src/collaboration.ts
Conflict Resolution
Yjs automatically resolves conflicts using CRDTs:Testing Collaboration
Test with multiple browser windows:Advanced Features
Presence Metadata
Share additional user data:Custom Sync Logic
Version History
packages/lexical-yjs/src/index.ts:34-47 for experimental version diff commands.
Best Practices
- Unique User IDs: Assign unique IDs to each user
- Connection Status: Show connection state to users
- Offline Support: Use IndexedDB for offline persistence
- Error Handling: Handle connection failures gracefully
- Cleanup: Disconnect providers on unmount
- Security: Add authentication to your WebSocket server
- Scalability: Use a dedicated Yjs server for production
Troubleshooting
Content Not Syncing
- Check WebSocket connection status
- Verify document ID matches across clients
- Ensure
shouldBootstrapis set correctly
Cursor Positions Wrong
- Verify all clients use the same node types
- Check that custom nodes implement serialization correctly
Performance Issues
- Limit awareness updates frequency
- Use debouncing for cursor position updates
- Consider server-side persistence for large documents
See Also
- Yjs Documentation
- API Reference: Yjs Package
- API Reference: React Plugins
- Source:
packages/lexical-yjs/src/ - Example:
packages/lexical-playground/src/plugins/CollaborationPlugin/