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
Lead Architect & Founder
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:
---
🔒 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:
---
🚀 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!

