A production-ready collaborative code editor built with modern web technologies, featuring real-time synchronization using Operational Transformation (OT) and WebSocket communication.
- Overview
- Features
- Architecture
- Tech Stack
- Project Structure
- Getting Started
- Usage
- How It Works
- Deployment
- Future Enhancements
- Contributing
DevFlow is a real-time collaborative code editor that allows multiple users to edit code simultaneously in isolated rooms. It uses Operational Transformation to resolve concurrent edits and maintain document consistency across all clients without requiring central authority.
Perfect for:
- Pair programming sessions
- Code interviews
- Team collaboration
- Teaching and mentoring
- Real-time coding demonstrations
- โ Real-Time Collaboration - See changes instantly across all connected users
- โ Multi-Room Architecture - Create isolated editing spaces for different projects
- โ Conflict Resolution - Operational Transformation ensures consistent state across clients
- โ Syntax Highlighting - Multiple language support via Monaco Editor
- โ Connection Status - Real-time connection state indicators
- โ Room Management - Create, list, and join rooms seamlessly
- โ WebSocket Communication - Low-latency real-time updates
- โ Full TypeScript - Type-safe codebase throughout
- โ Responsive Design - Works on desktop and mobile
- โ Automatic Room Cleanup - Rooms delete after 60 seconds of inactivity
- โ Operation Versioning - Track and manage concurrent operations
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Client Layer (Next.js) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Monaco Editor โ โ WebSocket Client โ โ
โ โ - Syntax Highlight โ โ - Real-time Sync โ โ
โ โ - Multi-language โ โ - Pending Ops Queue โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
WebSocket (ws://)
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Server Layer (Node.js) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ WebSocket Server (ws) โ โ
โ โ - Connection Management โ โ
โ โ - Message Routing โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Operational Transformation Engine โ โ
โ โ - Transform(opA, opB): Operation โ โ
โ โ - Resolve concurrent edits โ โ
โ โ - Maintain version history โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Room Manager โ โ
โ โ - Room state management โ โ
โ โ - Client tracking โ โ
โ โ - Automatic cleanup โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
REST API
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Data Layer โ
โ - In-memory storage (current) โ
โ - MongoDB (future) โ
โ - Operation history (future) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
DevFlow uses Operational Transformation to handle concurrent edits:
- Client Sends Operation - User types text โ client generates operation with
serverVersion - Server Receives & Transforms - Server receives operation, transforms it against newer operations in history
- Apply & Broadcast - Server applies transformed operation, increments version, broadcasts to other clients
- Client Transforms Pending - Receiving clients transform their pending operations against the remote operation
This ensures eventual consistency without requiring locks or central arbitration.
Client A Server Client B
โ โ โ
โโ insert 'a' v0 โโโโโโโโโ>โ โ
โ โโ apply op โโโโโโโโโโค
โ โโ transform โโโโโโโโโค
โ โ pending ops โ
โ<โโโโโโโโโโโโโโโโโโโโโโโโโคโโโโโโโโโโโโโโโโโโโโโค
โ remote ops broadcast โ insert 'b' v0 โ
- Next.js 16 - React framework with SSR
- TypeScript - Type safety
- Monaco Editor - VS Code's editor component
- TailwindCSS - Utility-first CSS
- Axios - HTTP client
- WebSocket API - Browser native WebSocket
- Node.js - JavaScript runtime
- Express.js - HTTP server
- ws - WebSocket server
- TypeScript - Type safety
- CORS - Cross-origin support
- Vercel - Frontend hosting
- Railway - Backend hosting
- Docker (roadmap) - Containerization
devflow/
โโโ devflow-client/ # Next.js frontend
โ โโโ app/
โ โ โโโ page.tsx # Room listing page
โ โ โโโ room/
โ โ โ โโโ [roomId]/
โ โ โ โโโ page.tsx # Collaborative editor
โ โ โโโ layout.tsx # Root layout
โ โ โโโ globals.css # Global styles
โ โโโ lib/
โ โ โโโ ot.ts # OT algorithm (client copy)
โ โโโ public/ # Static assets
โ โโโ package.json
โ โโโ tsconfig.json
โ โโโ next.config.ts
โ
โโโ devflow-server/ # Express.js backend
โ โโโ src/
โ โ โโโ index.ts # WebSocket server & REST API
โ โ โโโ ot.ts # Operational Transformation
โ โ โโโ room.ts # Room management
โ โ โโโ config.ts # Configuration
โ โโโ dist/ # Compiled JavaScript
โ โโโ package.json
โ โโโ tsconfig.json
โ โโโ .env # Environment variables
โ
โโโ README.md # This file
- Node.js 18+
- npm or yarn
- Git
git clone https://github.com/slayer1371/Devflow.git
cd devflowcd devflow-server
# Install dependencies
npm install
# Create .env file
cat > .env << EOF
PORT=4000
NODE_ENV=development
CLIENT_URL=http://localhost:3000
EOF
# Compile TypeScript
npm run build
# Start server
npm run devServer runs on http://localhost:4000
cd devflow-client
# Install dependencies
npm install
# Create .env.local file
cat > .env.local << EOF
NEXT_PUBLIC_API_URL=http://localhost:4000
NEXT_PUBLIC_WS_URL=ws://localhost:4000
EOF
# Start dev server
npm run devClient runs on http://localhost:3000
Open two browser tabs:
- Tab 1:
http://localhost:3000 - Tab 2:
http://localhost:3000
Create a room, open it in both tabs, and start typing!
- Click "Create New Room" button on home page
- Room is created and you're redirected to the editor
- Share the URL with collaborators
- Click on an active room from the list
- Start collaborating in real-time
- Syntax Highlighting - Automatic for JavaScript (extensible)
- Real-Time Updates - See others' changes instantly
- Connection Status - Indicator shows connection state
- Version Tracking - See current server version
DevFlow supports three operation types:
interface InsertOp {
type: 'insert';
position: number; // Where to insert
text: string; // Text to insert
version: number; // Server version when created
}
interface DeleteOp {
type: 'delete';
position: number; // Where to delete from
length: number; // How many characters
version: number;
}
interface ReplaceOp {
type: 'replace';
position: number; // Where to replace
deleteLength: number; // How many to delete
insertText: string; // What to insert
version: number;
}The core of DevFlow - how it resolves concurrent edits:
function transform(opA: Operation, opB: Operation): Operation {
// If both are inserts at same position, opB wins
if (opA.type === 'insert' && opB.type === 'insert') {
if (opB.position < opA.position) {
return { ...opA, position: opA.position + opB.text.length };
}
return opA;
}
// If A inserts and B deletes before A's position, shift A left
if (opA.type === 'insert' && opB.type === 'delete') {
if (opB.position < opA.position) {
return { ...opA, position: opA.position - opB.length };
}
return opA;
}
// ... (more cases for all combinations)
}Initial state: "hello"
Client A types at position 5: "!" โ "hello!"
Client B deletes at position 0-1: "" โ "ello!"
Both changes concurrent (same version):
Server receives A's insert:
- No prior ops to transform against
- Applies: "hello!"
- Broadcasts to B
Server receives B's delete:
- Must transform against A's insert
- Transform: A inserted "!" at position 5, so B's delete at 0-1 unaffected
- Applies delete to "hello!": "ello!"
- Broadcasts to A
A receives B's delete:
- Must transform against pending insert
- A's pending insert at position 5 is unaffected by B's delete at 0-1
- Applies: "ello!"
Result: Both clients show "ello!" โ
# Push to GitHub
git push origin main
# Connect on Vercel dashboard
# - Select devflow-client directory as root
# - Set NEXT_PUBLIC_API_URL environment variable
# - Deploy# Push to GitHub
git push origin main
# Connect on Railway dashboard
# - Select devflow-server directory
# - Set PORT=4000, CLIENT_URL=<your-vercel-url>
# - DeployFrontend (.env.local)
NEXT_PUBLIC_API_URL=https://your-server.railway.app
NEXT_PUBLIC_WS_URL=wss://your-server.railway.app
Backend (.env)
PORT=4000
NODE_ENV=production
CLIENT_URL=https://your-frontend.vercel.app
- MongoDB Persistence - Save rooms and operation history
- User Authentication - JWT-based auth system
- Docker Setup - Containerization for easy deployment
- User Presence - Show active cursor positions and user names
- Undo/Redo - Full undo/redo stack with OT support
- Multiple Files - File tree and multi-file support
- Room Permissions - Read-only mode, invite-only rooms
- Unit Tests - Test OT algorithm thoroughly
- Integration Tests - WebSocket communication tests
- Error Recovery - Graceful reconnection and state recovery
- Analytics - Track room usage and user activity
- Operational Transformation - Wikipedia
- Google Docs Realtime API Explained
- WebSocket API - MDN
- Next.js Documentation
- Express.js Guide
The OT algorithm handles all combinations:
Insert vs Insert โ
Insert vs Delete โ
Insert vs Replace โ
Delete vs Insert โ
Delete vs Delete โ
Delete vs Replace โ
Replace vs Insert โ
Replace vs Delete โ
Replace vs Replace โ
All combinations are tested with real-time typing scenarios.
Contributions welcome! Areas for contribution:
- Bug Fixes - Found an issue? Create a PR
- Features - Implement from the roadmap
- Tests - Add unit or integration tests
- Documentation - Improve docs or examples
- Performance - Optimize algorithm or reduce latency
# Create feature branch
git checkout -b feature/your-feature
# Make changes and commit
git commit -m "feat: add your feature"
# Push and create PR
git push origin feature/your-featureMIT License - see LICENSE file for details
- GitHub: @slayer1371
- Project: DevFlow Repository
- Mozilla MDN for WebSocket documentation
- Google Docs for OT algorithm inspiration
- Monaco Editor team for the amazing editor
- The open-source community
Built with โค๏ธ using modern web technologies
Have questions? Create an issue on GitHub!