Real-Time Collaborative Code Editor
A low-latency distributed system for live coding interviews with AI assistance. Live Demo'
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
- Real-Time Synchronization: Users can type simultaneously without race conditions.
- 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.
- Live Cursor Tracking: See exactly where other users are typing (multi-cursor support).
- 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.