Skip to content

slayer1371/Devflow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

13 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

DevFlow - Real-Time Collaborative Code Editor

A production-ready collaborative code editor built with modern web technologies, featuring real-time synchronization using Operational Transformation (OT) and WebSocket communication.

DevFlow Next.js TypeScript License

๐Ÿ“‹ Table of Contents

๐ŸŽฏ Overview

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

โœจ Features

Core Functionality

  • โœ… 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

Technical Features

  • โœ… 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

๐Ÿ—๏ธ Architecture

System Design

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    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)                               โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Operational Transformation

DevFlow uses Operational Transformation to handle concurrent edits:

  1. Client Sends Operation - User types text โ†’ client generates operation with serverVersion
  2. Server Receives & Transforms - Server receives operation, transforms it against newer operations in history
  3. Apply & Broadcast - Server applies transformed operation, increments version, broadcasts to other clients
  4. 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    โ”‚

๐Ÿ› ๏ธ Tech Stack

Frontend

  • 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

Backend

  • Node.js - JavaScript runtime
  • Express.js - HTTP server
  • ws - WebSocket server
  • TypeScript - Type safety
  • CORS - Cross-origin support

DevOps

  • Vercel - Frontend hosting
  • Railway - Backend hosting
  • Docker (roadmap) - Containerization

๐Ÿ“ Project Structure

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

๐Ÿš€ Getting Started

Prerequisites

  • Node.js 18+
  • npm or yarn
  • Git

Local Setup

1. Clone the Repository

git clone https://github.com/slayer1371/Devflow.git
cd devflow

2. Setup Server

cd 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 dev

Server runs on http://localhost:4000

3. Setup Client (New Terminal)

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 dev

Client runs on http://localhost:3000

4. Test Collaboration

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!

๐Ÿ’ป Usage

Create a Room

  1. Click "Create New Room" button on home page
  2. Room is created and you're redirected to the editor
  3. Share the URL with collaborators

Join a Room

  1. Click on an active room from the list
  2. Start collaborating in real-time

Editor Features

  • 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

๐Ÿ”„ How It Works

Operation Types

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;
}

Transform Algorithm

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)
}

Example Scenario

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!" โœ“

๐ŸŒ Deployment

Deploy to Vercel (Frontend)

# 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

Deploy to Railway (Backend)

# Push to GitHub
git push origin main

# Connect on Railway dashboard
# - Select devflow-server directory
# - Set PORT=4000, CLIENT_URL=<your-vercel-url>
# - Deploy

Environment Variables

Frontend (.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

๐Ÿ”ฎ Future Enhancements

Priority 1 - Resume Impact

  • MongoDB Persistence - Save rooms and operation history
  • User Authentication - JWT-based auth system
  • Docker Setup - Containerization for easy deployment

Priority 2 - Features

  • 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

Priority 3 - Polish

  • 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

๐Ÿ“š Learning Resources

๐Ÿงช Testing the OT Algorithm

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.

๐Ÿค Contributing

Contributions welcome! Areas for contribution:

  1. Bug Fixes - Found an issue? Create a PR
  2. Features - Implement from the roadmap
  3. Tests - Add unit or integration tests
  4. Documentation - Improve docs or examples
  5. Performance - Optimize algorithm or reduce latency

Development Workflow

# 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-feature

๐Ÿ“„ License

MIT License - see LICENSE file for details

๐Ÿ‘ค Author

๐Ÿ™ Acknowledgments

  • 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!

Releases

No releases published

Packages

No packages published