Level Discoverystart here Intermediatethe architecture Expertin depth
Intermediate · Batch 3 · Tundra
Chapter 3 · intermediate level

Real-time client/server

One screen becomes many: a TV, two phones, a referee. We look at the authoritative server, the intents/snapshots protocol, the shared simulation, and the small choreography of seats, spectators and disconnects.

1One authority, many rooms

The match is computed in exactly one place: the server. Each game lives in a Room — two player slots, a waiting queue, any number of TV spectators, and one authoritative simulation.

server/Room.js — the room's state
const TICK_MS = 33; // ~30 Hz: both the sim step AND the broadcast rate

this.players = [null, null]; // the two seats
this.waiting = [];          // queue: spectate until a seat frees
this.tvs = new Set();      // passive viewers
this.sim = null;            // the authoritative simulation

The server (server/index.js) only routes WebSocket messages to the right room: host creates a room, join places a participant in it, and everything else (aim, ready, shell…) is routed to the room's methods. No game logic lives in the network layer.

One source of truth. If each device ran its own match, they'd drift apart (a ball here, not there). The room is the single referee — the screens can only display what it decides.

2The protocol: intents up, snapshots down

The two directions of the WebSocket carry very different things. Phones send small intents; the server broadcasts a full game snapshot about 30 times per second.

📱 Controller 📱 Controller 🧑‍⚖️ Server 📺 TV aim / ready (intents) snapshot ~30 Hz
Phones send tiny orders; the server sends the whole state back to every screen.

Each tick, the room advances the simulation by a fixed step, collects the events that occurred, then broadcasts the snapshot to every subscriber of the room:

server/Room.js — one loop turn
step() {
  this.sim.tick(DT);
  const events = this.sim.drainEvents();
  this.broadcast({ t: 'snapshot', state: this.sim.snapshot(), events });
}

3The same simulation on every machine

The server runs under Node, with no browser or canvas. It can still compute the physics because all the logic lives in src/sim/, in pure JavaScript. The server imports the same file as the clients:

server/Room.js — the server runs src/sim/
import Simulation, { PHASE } from '../src/sim/Simulation.js';
import { BIOMES } from '../src/config/biomes.js';

That's the payoff of chapter 1's architecture decision: the same physics runs on the server (the judge) and stays testable under Node. Nobody rewrites the trajectories “for the network”.

src/sim/Simulation.js — what a snapshot ships
// the spectator TV never gets the players' chosen aim values shown;
// only ready flags, and — once fired — the resulting projectiles.
projectiles: this.projectiles
  .filter((p) => p.alive)
  .map((p) => ({ id: p.id, x: Math.round(p.x), y: Math.round(p.y), owner: p.owner })),
Hidden information. The snapshot does not contain the players' angle or power while aiming — those numbers are never shown on the TV. Only the “ready” flags and, once a shot is fired, the projectiles are broadcast. The information asymmetry is in the protocol.

4Seats, queue and leaving

A room is alive: people arrive, leave, take over. The room handles these transitions without breaking the match. An arriving participant takes a free seat, otherwise they join the queue as a spectator.

server/Room.js — free seat or waiting queue
const slot = this.players.findIndex((p) => p === null);
if (slot !== -1) { this.assignPlayer(slot, socket, name); /* player */ }
else { this.waiting.push({ socket, name }); /* spectator in queue */ }

When a player frees their seat (voluntary leave or disconnect), the front of the queue takes over (promoteInto). And if no one fills both seats mid-match, the room returns to the lobby: the TV shows the code and QR again.

server/Room.js — back to the lobby if a seat stays empty
this.promoteInto(i); // a waiting spectator takes the seat
if (this.sim && !this.bothFilled()) this.resetToLobby();
Remember. The network is also all the “unhappy” cases: leaves, reconnects, seats freeing up. Centralising this state in a single Room object makes these transitions readable and testable.
← Chapter 2 · Data & rendering Chapter 4 · Destructible terrain & shared state →