Learn: Collaborative Systems
Understanding the concepts behind real-time collaborative editing
WebSockets establish a persistent connection between client and server, enabling bidirectional communication. When one user edits text, the change is captured, transformed if needed, and broadcast to all other clients.
Each client maintains a local copy of the document state. Edits are sent as operations (insert, delete) with metadata. The server coordinates these operations and ensures all clients converge to the same state.
When two users edit at the same position, conflicts arise. Common strategies: Last-Write-Wins (simple), Operational Transformation (complex), or CRDT (conflict-free). This editor uses operational transformation with timestamps.
TCP ensures ordered, reliable delivery of messages (like WebSocket). UDP is faster but provides no guarantees. WebSocket uses TCP underneath, ensuring no edits are lost.
Simple consensus: the server is the authority. More complex: Raft, Paxos, Byzantine Fault Tolerance. For our editor, the server acts as the single source of truth for edit ordering.
Each user's cursor position is sent to the server with edit operations. The server broadcasts cursor positions to all clients, allowing you to see where others are editing.