Skip to content

Tusharbecoding/collab-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

22 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Collab Code - Real-time Collaborative Code Editor

A modern, real-time collaborative code editor built with Next.js, Socket.IO, Redis, and RabbitMQ. Multiple developers can edit code simultaneously with live cursor tracking and real-time synchronization.

๐Ÿš€ Features

  • Real-time Collaboration: Multiple users can edit code simultaneously
  • Live Cursor Tracking: See where other users are typing in real-time
  • Monaco Editor Integration: Full-featured code editor with syntax highlighting
  • Multi-language Support: Support for JavaScript, TypeScript, Python, and more
  • Session Management: Persistent coding sessions with unique URLs
  • User Presence: Visual indicators showing active collaborators
  • Conflict Resolution: Robust handling of concurrent edits
  • Auto-save: Changes are automatically persisted to the database

๐Ÿ—๏ธ Architecture

Tech Stack

  • Frontend: Next.js 15, React 19, TypeScript, Tailwind CSS
  • Editor: Monaco Editor (VS Code editor)
  • Backend: Node.js with Socket.IO for WebSocket connections
  • Message Queue: RabbitMQ for reliable message delivery
  • Caching: Redis for session state and real-time data
  • Database: Supabase (PostgreSQL) for persistent storage
  • Real-time: Socket.IO for WebSocket communication

System Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Next.js App   โ”‚    โ”‚  Socket.IO      โ”‚    โ”‚    RabbitMQ     โ”‚
โ”‚   (Frontend)    โ”‚โ—„โ”€โ”€โ–บโ”‚   Server        โ”‚โ—„โ”€โ”€โ–บโ”‚ Message Queue   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚                       โ”‚                       โ”‚
         โ”‚              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”              โ”‚
         โ”‚              โ”‚     Redis       โ”‚              โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚  (Session State)โ”‚โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                        โ”‚    Supabase     โ”‚
                        โ”‚   (Database)    โ”‚
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ”ง Installation & Setup

Prerequisites

  • Node.js 18+ and npm
  • Redis server
  • RabbitMQ server
  • Supabase account

1. Clone the Repository

git clone https://github.com/tusharbecoding/collab-code.git
cd collab-code

2. Install Dependencies

npm install

3. Environment Setup

Create a .env.local file in the root directory:

# Supabase Configuration
NEXT_PUBLIC_SUPABASE_URL=your_supabase_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key

# Redis Configuration
REDIS_URL=redis://localhost:6379

# RabbitMQ Configuration
RABBITMQ_URL=amqp://admin:password@localhost:5672

# WebSocket Server
NEXT_PUBLIC_WEBSOCKET_URL=ws://localhost:3001

4. Database Setup

Set up the following tables in your Supabase database:

-- Sessions table
CREATE TABLE sessions (
  id TEXT PRIMARY KEY,
  code TEXT DEFAULT '',
  language TEXT DEFAULT 'javascript',
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Session participants table
CREATE TABLE session_participants (
  id BIGSERIAL PRIMARY KEY,
  session_id TEXT REFERENCES sessions(id),
  user_id TEXT,
  username TEXT,
  last_seen TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

5. Start the Services

Make sure Redis and RabbitMQ are running on your system, then:

# Start both the Next.js app and Socket.IO server
npm run dev

This will start:

  • Next.js frontend on http://localhost:3000
  • Socket.IO server on http://localhost:3001

๐ŸŽฎ Usage

  1. Create a Session: Visit http://localhost:3000 and create a new coding session
  2. Join a Session: Share the session URL with collaborators
  3. Start Coding: Begin editing code and see real-time changes from all participants
  4. View Cursors: See where other users are typing with colored cursor indicators

Session URL Format

http://localhost:3000/editor/[sessionId]?username=[your-username]

๐Ÿ”„ Conflict Resolution

Our conflict resolution system ensures data consistency across all collaborative sessions:

How It Works

  1. Last-Write-Wins Strategy: The system implements a simple but effective last-write-wins approach
  2. Timestamp-based Ordering: Each code change includes a timestamp for ordering
  3. Redis State Management: Session state is maintained in Redis for fast access
  4. RabbitMQ Message Queue: Ensures reliable delivery of code changes
  5. Real-time Synchronization: Socket.IO broadcasts changes to all connected clients

Conflict Resolution Flow

User A makes change โ”€โ”€โ”
                      โ”œโ”€โ”€โ–บ Redis State Update โ”€โ”€โ–บ RabbitMQ Queue โ”€โ”€โ–บ Broadcast to all users
User B makes change โ”€โ”€โ”˜

Key Components

  • server.ts:96-125: Handles incoming code changes and broadcasts them
  • src/lib/redis.ts:24-34: Updates session state with latest code
  • src/lib/rabbitmq.ts:26-38: Publishes changes to message queue
  • src/components/CodeEditor.tsx:31-55: Applies remote changes to editor

Conflict Prevention

  • Immediate State Updates: Changes are immediately reflected in Redis
  • Optimistic UI Updates: Local changes appear instantly for better UX
  • Remote Change Integration: Incoming changes are applied while preserving cursor position
  • Message Ordering: RabbitMQ ensures proper message delivery order

๐Ÿ“ Project Structure

collab-code/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ app/
โ”‚   โ”‚   โ”œโ”€โ”€ editor/[sessionId]/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ page.tsx          # Editor page component
โ”‚   โ”‚   โ”œโ”€โ”€ layout.tsx            # Root layout
โ”‚   โ”‚   โ””โ”€โ”€ page.tsx              # Home page
โ”‚   โ”œโ”€โ”€ components/
โ”‚   โ”‚   โ””โ”€โ”€ CodeEditor.tsx        # Monaco editor wrapper
โ”‚   โ”œโ”€โ”€ lib/
โ”‚   โ”‚   โ”œโ”€โ”€ rabbitmq.ts           # RabbitMQ client
โ”‚   โ”‚   โ”œโ”€โ”€ redis.ts              # Redis client
โ”‚   โ”‚   โ””โ”€โ”€ supabase.ts           # Supabase client
โ”‚   โ””โ”€โ”€ types/
โ”‚       โ””โ”€โ”€ collaboration.ts     # TypeScript types
โ”œโ”€โ”€ server.ts                     # Socket.IO server
โ”œโ”€โ”€ package.json
โ””โ”€โ”€ README.md

๐Ÿ™ Acknowledgments

  • Monaco Editor - The code editor that powers VS Code
  • Socket.IO - Real-time bidirectional event-based communication
  • Redis - In-memory data structure store
  • RabbitMQ - Message broker
  • Supabase - Open source Firebase alternative

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors