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

Play across several screens

The TV shows the match; the phones act as controllers. So everyone sees the same thing at the same time, we need a referee: the server.

1The problem

Until now, everything happened on a single computer. Now: a TV, two phones… three different devices that must live the same match, perfectly in sync.

If each device computed the match in its own corner, they'd end up disagreeing (a ball here, not there…). We need a single source of truth.

2The server is the referee

We add a program running “in the middle”: the server. The devices are clients. The server is the only one to compute the match (the physics, the scores); the clients only display what it decides and send it the players' orders.

Remember. “The referee is always right”: since the server decides everything, the screens can't contradict each other — and cheating is harder.

3A phone line that stays open: the WebSocket

A classic web page asks for a piece of info, gets an answer, then hangs up. Too slow for a game! We use a WebSocket: a line that stays open both ways. The server can “speak” whenever it wants, ~30 times per second.

Try it! Click to launch a volley and watch the messages travel: the orders go up to the referee, then the state comes back down to every screen.

The dance of the messages

Blue/red: the players' orders. Green: the game state sent back to everyone.

4The same landscape everywhere, without sending it all

The terrain can be thousands of points: too big to send. The trick: the server only sends a small random seed (one number). Each screen recomputes the same landscape from that seed (remember the generator from chapter 2!).

the idea of sharing by seed
// the server just sends:
{ seed: 428193 }
// each screen does:
const terrain = generateTerrain(428193); // → identical everywhere!

5Joining a match: room code & QR

The TV creates a room and shows a 4-letter code and a QR code. The phones scan the QR: they land straight into the right match. Handy!

📺 TV 7F3K 📱 scan → Player 1 📱 scan → Player 2
The TV shows the code & the QR; two phones join as players.
For the curious. The QR must point to the TV's local network address (something like 192.168.1.20), not to “localhost” (which means “myself”). Otherwise the phone would be looking for itself! The server finds the right address on its own.

6Who does what? The TV manages on its own

Remember. Batch 3 = going from a game “on one screen” to a “networked” game, without rewriting everything: we just moved the computation to the referee.
← Chapter 2 Chapter 4 · Destroy the landscape →