What is Yjs?
Yjs is a high-performance CRDT (Conflict-Free Replicated Data Type) implementation that enables real-time collaboration without conflicts. It provides shared data types that can be distributed across multiple clients and merged seamlessly without manual conflict resolution.CRDT stands for Conflict-Free Replicated Data Type - a data structure designed to be replicated across multiple computers in a network, where replicas can be updated independently and concurrently without coordination, and it is always possible to resolve inconsistencies that might occur.
Why Yjs?
When building a collaborative editor, there are two main approaches to handling concurrent edits:- Operational Transformation (OT) - Transforms operations based on concurrent changes
- Conflict-Free Replicated Data Types (CRDT) - Ensures mathematical convergence without conflicts
- Extensive library support - Works seamlessly with Monaco Editor, WebSockets, and other modern tools
- Built-in fallbacks - Supports cross-tab communication, allowing sessions to persist even if the central WebSocket server goes down
- Performance - Highly optimized for real-time collaboration
- Proven reliability - Used in production by many collaborative applications
How Plant Together Uses Yjs
Shared Document Structure
Plant Together creates a Yjs document for each collaborative room, with a shared text type for the PlantUML content:WebSocket Provider Integration
The WebSocket provider connects the Yjs document to the server for real-time synchronization:The
wsID is a combination of the room ID and document ID, ensuring each document has its own synchronization channel.Awareness Protocol
Yjs includes an awareness protocol that tracks user presence and state:- Seeing who else is editing the document
- Displaying remote cursor positions
- Showing user colors and names
Server-Side Document Persistence
On the server side, Yjs documents are persisted to PostgreSQL and synchronized through Redis:Key Benefits
- No Merge Conflicts - Multiple users can edit simultaneously without conflicts
- Offline Support - Changes sync automatically when connection is restored
- Cross-Tab Sync - Multiple browser tabs stay in sync automatically
- Efficient Updates - Only sends delta changes, not entire documents
Learn More
Related Technologies
Monaco Editor
Integration with the VS Code-based editor
WebSockets
Real-time communication layer