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.
- 📱 The phones send: “my angle = 60°, I confirm”.
- 🧑⚖️ The server computes the volley, who gets hit, the score.
- 📺 Everyone receives the game state and displays it the same way.
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.
The dance of the messages
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 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!
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
- 📺 A detected TV starts straight away in “host” mode: no remote needed.
- 🟢 The match starts on its own as soon as the 2nd player has joined.
- 🧑🤝🧑 Extra players? They watch while waiting for a spot to free up, then take over.