Real-time Collaboration
FlowCollaborationEngine is based on WebSocket and lightweight CRDT (LWW Register / G-Counter) to achieve multi-user real-time editing of Flow, supporting cursor synchronization, selection synchronization, and conflict resolution. The collaboration engine provides connect, disconnect, and event listeners, which can be directly interfaced with existing WebSocket services.
Collaboration Architecture
- CRDT State: Locally maintains Maps of
nodes,edges, andviewport, processing concurrent updates combined with the LWW register. - WebSocket Messages: join/leave/operation/sync/cursor/selection six message types, used for room management, state synchronization, and instant interaction.
- Event Callbacks: Supports
sync,operation,cursor_update,user_joined,user_leftevents, making it easy for UI response. - Reconnection Mechanism: Automatic exponential backoff reconnection, up to 5 attempts.
Core API
| API | Description |
|---|---|
connect(options) | Connect to a collaboration room (serverUrl, roomId, userId, userName, initialNodes/Edges) |
disconnect() | Disconnect and clean up heartbeats |
addNode(node) / updateNode(nodeId, updates) / deleteNode(nodeId) | Local operations are automatically broadcasted |
addEdge(edge) / updateEdge(edgeId, updates) / deleteEdge(edgeId) | Edge operations synchronization |
updateViewport(viewport) / updateCursor(x, y) / updateSelection(nodeIds) | Viewport and interaction synchronization |
getState() / getCursors() / getConnectionState() | Query current state |
on(event, callback) / off(event, callback) | Event subscription |
Example
The example below demonstrates how to initialize the collaboration engine, connect to a room, listen for events, and display user cursors and status in the UI.
Status: disconnected
Real-time Collaboration Example
Best Practices
- Integrate with Business WebSocket Service: Replace the
serverUrlinconnectwith your collaboration backend address; the backend only needs to forward messages. - Local First: All local operations take effect immediately, while network synchronization is completed asynchronously via
operationmessages. The LWW mechanism ensures eventual consistency. - Handle Conflicts: Monitor remote updates via
on('operation'), enabling conflict UI prompts or rollbacks for specific nodes/edges. - Optimize Cursor & Selection: Use unreliable transmission for
updateCursorandupdateSelectionto avoid frequent network jitter from impacting the user experience.