An industry-grade, real-time collaborative code editor built using a custom RGA-CRDT (Replicated Growable Array) algorithm from scratch, enabling conflict-free concurrent editing for 50+ simultaneous users without central locking.
- Custom RGA-CRDT Algorithm: Built from scratch to resolve concurrent text edits mathematically, ensuring all clients converge to the exact same document state regardless of network latency or out-of-order operations.
- Monaco Editor Integration: Embedded VS Code's core editor engine in React, complete with smooth caret animations, customized dark theme, and intelligent remote cursor shifting.
-
Dual Simulation & Workspace Modes:
- Local Split-Screen Simulator: Allows developers to simulate network partitions and offline typing, merging concurrent text buffers conflict-free upon reconnection.
- Multi-Instance Server Sync: Connects real users over persistent WebSocket connections to collaborate globally.
- Distributed Scaling (Redis Pub/Sub): Scales horizontally by fanning out collaborative sync operations across multiple server instances using Redis Pub/Sub rooms.
-
Defensive & Event-Sourced Persistence (MongoDB):
- Periodically collapses RGA linked-list states to save snapshot checkpoints.
- Appends granular operations to an operation log to replay edits since the last snapshot.
- Utilizes a compound database index
{ documentId: 1, timestamp: -1 }to hydrate documents in$O(1)$ time on client joins.
- Docker Compose Orchestration: Containerized multi-service setup including an Nginx reverse proxy, Node WebSockets, Redis, and MongoDB.
graph TD
Client1[React Client A] <-->|WebSockets /socket| Nginx[Nginx Reverse Proxy]
Client2[React Client B] <-->|WebSockets /socket| Nginx
Nginx <-->|Load Balance| Backend1[Node WebSocket Server 1]
Nginx <-->|Load Balance| Backend2[Node WebSocket Server 2]
Backend1 <-->|Redis Pub/Sub| Redis[(Redis Broker)]
Backend2 <-->|Redis Pub/Sub| Redis
Backend1 <-->|Read / Write| Mongo[(MongoDB)]
Backend2 <-->|Read / Write| Mongo
When concurrent insertions share the same predecessor parent node, the conflict is resolved deterministically:
- Sibling nodes are sorted in descending order of their Lamport timestamps.
- If timestamps are equal, they are sorted lexicographically by Site ID as a final tie-breaker.
- Descendant subtrees of skipped siblings are preserved in order, preventing character interleaving.
- An optimized
nodeMapprovides$O(1)$ node lookup times to ensure high-frequency concurrent typing remains highly responsive.
├── package.json # Root monorepo workspace coordinator
├── docker-compose.yml # Container orchestration configuration
├── nginx.conf # Nginx gateway and connection upgrade proxy
├── frontend/ # React + Vite + TypeScript + Monaco Editor
│ ├── Dockerfile
│ ├── package.json
│ ├── tsconfig.json
│ └── src/
│ ├── crdt/ # RGA-CRDT core convergence implementation
│ │ ├── rga.ts # RGACRDT class & sibling sorting logic
│ │ └── types.ts # Struct types (NodeId, RGADNode, Operations)
│ ├── components/
│ │ └── Editor.tsx # Monaco Editor React wrapper
│ ├── hooks/
│ │ └── useMonacoCRDT.ts # Keystroke interception & remote edit injection
│ ├── App.tsx # Main layout containing simulation & WebSocket UI
│ └── index.css # Premium glassmorphic styles and animations
└── backend/ # Node.js Express + WebSockets + Redis + MongoDB
├── Dockerfile
├── package.json
└── src/
├── server.js # Room-based WebSocket server & periodic snapshotting
├── crdt.js # Native ESM backend representation of RGA-CRDT
├── db.js # MongoDB event sourcing & indexing
└── redis.js # Redis Pub/Sub scaling module
Both database and Redis clients are built defensively. If MongoDB or Redis are not running on your local machine, the servers will automatically fall back to an in-memory, single-instance WebSocket relay mode.
- Clone the repository and navigate to the directory:
git clone https://github.com/nikhil3495/Real-Time-Collaborative-Code-Editor.git cd Real-Time-Collaborative-Code-Editor - Install dependencies for both frontend and backend workspaces:
npm install
- Start the backend server (runs on
http://localhost:4000):npm run dev:backend
- Start the React Vite development server (runs on
http://localhost:3000):npm run dev:frontend
- Open http://localhost:3000/ in your browser.
Docker Compose spins up the full stack including MongoDB, Redis, two Node servers, and Nginx. Nginx handles serving the React static build and proxying WebSocket streams.
- Build and run the containers:
docker-compose up --build
- Open http://localhost/ in multiple browser tabs or devices. Nginx will route traffic and balance the loads across WebSocket nodes while Redis keeps them perfectly synchronized.
We have built a comprehensive unit test suite to verify convergence and correctness of the CRDT algorithm:
npm run test:frontendThis runs Vitest to verify sequential insertions, multi-character text blocks, character deletions, concurrent sibling sorting, out-of-order merging, and state snapshot recovery.