Real-Time Collaborative Code Editor

A low-latency distributed system for live coding interviews with AI assistance. Live Demo'

The live coding interface supporting multi-user synchronization, AI debugging, and code execution.

Overview

This project is a low-latency collaborative coding platform designed to mimic Google Docs for code. It solves the challenge of synchronizing state across multiple clients in real-time while allowing for secure remote code execution and AI-driven assistance.

Technical Architecture

The system mimics a distributed architecture common in high-frequency trading or gaming servers, prioritizing speed and consistency.

  • Frontend: React.js with Monaco Editor (VS Code engine). It uses optimistic UI updates to ensure typing feels instant, even with network latency.
  • Backend: Python FastAPI coupled with WebSockets. I chose FastAPI over Django/Flask for its asynchronous capabilities (async/await), which allows handling thousands of concurrent connections.
  • AI Integration: Google Gemini API is integrated directly into the backend to parse code context and generate fix suggestions or optimizations on demand.
  • Synchronization: Implemented a broadcast pattern where cursor positions and keystrokes are transmitted via WebSockets and synchronized across all active clients in milliseconds.
  • Deployment:
    • Frontend: Vercel (CDN edge network).
    • Backend: Render (Containerized Python environment).
    • Database: PostgreSQL (hosted on Neon) with SQLAlchemy.

Key Features

  1. Real-Time Synchronization: Users can type simultaneously without race conditions.
  2. AI-Powered Debugging: Leveraging Google Gemini, the editor can analyze the current code buffer, identify syntax or logic errors, and suggest optimized refactors with a single click.
  3. Live Cursor Tracking: See exactly where other users are typing (multi-cursor support).
  4. Remote Code Execution: Securely sends code to the backend, executes it in an isolated Python environment, and streams stdout/stderr back to the client.

Challenges & Learnings

The biggest engineering challenge was handling the “Ghost User” problem with WebSockets. When a user disconnected abruptly (closed tab), the server maintained the connection state. I implemented a robust cleanup protocol using WebSocket lifecycle events (on_disconnect) and a heartbeat mechanism to ensure the active user list remains accurate.