Overview
Excalidraw’s collaboration system enables multiple users to work on the same drawing in real-time. It handles state synchronization, conflict resolution, user presence, and file sharing through a WebSocket-based architecture with end-to-end encryption.Architecture
The collaboration system consists of several key components:- Collab Class
- Portal
- Collaborator Type
The main orchestrator for collaboration features:
excalidraw-app/collab/Collab.tsx
Collaboration Lifecycle
1. Starting Collaboration
Room Link Format
Room Link Format
Collaboration rooms use a specially formatted URL:
- roomId: Unique identifier for the collaboration session
- roomKey: Encryption key for end-to-end encryption
- Both are base64-encoded strings
2. Connection Flow
3. Stopping Collaboration
State Synchronization
Element Synchronization
Elements are synchronized through a reconciliation process:excalidraw-app/collab/Collab.tsx
- Reconciliation
- Conflict Resolution
- Syncable Elements
When receiving remote changes, elements are reconciled using version tracking:Reconciliation logic:
- Compare element versions
- Keep higher version number
- For same version, compare
versionNonce - Preserve local selection state
- Merge deleted elements appropriately
Broadcast Throttling
To optimize network usage, broadcasts are throttled:User Presence
Pointer Tracking
packages/excalidraw/types.ts
Pointer updates are throttled to ~30fps to balance responsiveness with network efficiency.
User State Tracking
Collaborative Cursors
Remote cursors are rendered on the interactive canvas:Following Users
Users can follow each other’s viewports:packages/excalidraw/types.ts
Follow Mode Behavior
Follow Mode Behavior
When following a user:
- Your viewport automatically scrolls to match theirs
- Your zoom level adjusts to match theirs
- Element selections are highlighted
- Following can be broken by manual pan/zoom
File Sharing
Collaboration includes file synchronization for images:Encryption
All collaboration data uses end-to-end encryption:Encryption Implementation
Encryption Implementation
- Uses AES-GCM encryption
- Room key is generated as a secure random string
- Key is never sent to the server (stays in URL fragment)
- Each message has unique IV (initialization vector)
- Authentication tags prevent tampering
WebSocket Protocol
The collaboration server uses Socket.IO with custom event types:excalidraw-app/app_constants.ts
- Message Types
- Message Flow
SERVER_VOLATILE
- Pointer positions
- Transient state updates
- Not persisted if user disconnected
- Element updates
- Selection changes
- File uploads
- Persisted and replayed on reconnection
Collaborative Features
Remote Selection Highlights
Collaborative Locking
packages/excalidraw/types.ts
Scene Versioning
getSceneVersion is deprecated. Use hashElementsVersion for better collision resistance in large scenes.Error Handling
excalidraw-app/collab/CollabError.tsx
Connection States
Integration Example
Best Practices
Network Optimization
Network Optimization
- Throttle broadcasts to prevent network flooding
- Use volatile events for transient state (cursors)
- Batch element updates when possible
- Compress large payloads before encryption
- Implement exponential backoff for reconnection
Conflict Resolution
Conflict Resolution
- Trust version numbers for element reconciliation
- Never decrement version numbers
- Use versionNonce for same-version conflicts
- Preserve user intent during reconciliation
- Handle deleted elements gracefully
Security
Security
- Never send encryption keys to server
- Validate all incoming data before decryption
- Sanitize user-provided content (usernames, etc.)
- Implement rate limiting for broadcasts
- Handle malformed messages gracefully
User Experience
User Experience
- Show clear collaboration status indicators
- Display collaborator avatars and names
- Provide feedback for connection issues
- Allow easy copying of room links
- Implement graceful degradation when offline
Performance Considerations
- Large Scenes
- Many Users
- Mobile
For scenes with many elements:
- Use incremental updates instead of full syncs
- Implement spatial indexing for collision detection
- Throttle reconciliation frequency
- Consider delta compression