Back to Dispatches
Engineering6 min readAug 28, 2026

Building Real-Time 1v1 Code Battle Arenas with WebSockets & Judge0

An inside look at how CosmoDex synchronizes code state, executes multi-language submissions in sandboxed containers, and broadcasts live opponent progress in under 50ms.

Shubham gharte

Shubham gharte

Lead Architect & Founder

Building Real-Time 1v1 Code Battle Arenas with WebSockets & Judge0

Building Real-Time 1v1 Code Battle Arenas with WebSockets & Judge0

Competitive coding platforms have traditionally relied on asynchronous submission queues where users submit code and wait seconds for test suite execution. At CosmoDex, we wanted to create an adrenaline-fueled, real-time 1v1 battle experience where two developers duel head-to-head on identical algorithmic challenges with live opponent progress.

In this deep dive, we break down our real-time WebSocket architecture, low-latency code execution engine, and anti-cheat state verification pipelines.

---

⚡ The Architecture at a Glance

When two players click "Find Opponent", our matchmaking engine pairs them based on Elo combat rank and establishes a persistent bi-directional WebSocket pipeline:

[ Client Player 1 ] <---> [ Node WebSocket Relay ] <---> [ Client Player 2 ]
                                 │
                                 ▼
                     [ Judge0 Isolated Execution Cluster ]

Key Architectural Requirements:

1.Sub-50ms State Sync: Live typing indicators, line execution progress, and test case milestones broadcast instantly to the opponent's viewport.
2.Sandboxed Code Execution: Submissions run inside isolated Linux containers with memory and CPU bounds to prevent fork bombs or malicious socket calls.
3.Deterministic Win Resolution: The engine evaluates test coverage, execution speed, and submission timestamp down to milliseconds to award victory points.

---

🔒 Sandboxing Submissions with Judge0

When a player hits Run Tests or Submit Solution, the payload is dispatched to our Judge0 engine cluster over high-speed RPC:

{
  "source_code": "def solve(nums, target):
    seen = {}
    for i, num in enumerate(nums):
        diff = target - num
        if diff in seen:
            return [seen[diff], i]
        seen[num] = i
    return []",
  "language_id": 71,
  "stdin": "[2, 7, 11, 15]
9",
  "expected_output": "[0, 1]"
}

Execution Rules Enforced:

1.CPU Time Limit: 2.0 seconds
2.Memory Limit: 128 MB
3.Max Output Size: 1024 KB
4.Network Isolation: Disabled outbound socket connections

---

🚀 Optimized WebSocket Broadcast Engine

To ensure seamless 60 FPS visual feedback during battles without overloading network bandwidth, we implement delta compression for live cursor position and milestone events:

export function broadcastMilestone(roomId: string, player: string, milestoneIndex: number) {
  const payload = JSON.stringify({
    type: 'OPPONENT_PROGRESS',
    player,
    milestoneIndex,
    timestamp: Date.now(),
  });
  
  wsServer.to(roomId).emit('game_event', payload);
}

---

🎯 What's Next?

We are currently testing Multi-Player Squad Battles (4v4 Team Arenas) and expanding our Judge0 cluster nodes to support Rust and C++20.

Stay tuned for our upcoming developer blog on Designing Anti-Cheat Algorithmic Verification Systems!

Tags:#WebSockets#System Design#Judge0#Architecture#Python
Cosmo Mascot
Left Eye
Right Eye