Sodor Board — Realtime

Keep a gobak sodor league's live board the same on every phone: emit, rooms, acks, recovery, WebRTC. 15 levels, right in the browser.

What you learn in this game

Socket.IO keeps one connection open between each phone and the server, so the server can send news at any time without being asked first. Both sides talk in events: emit(name, data) sends, on(name, function) listens. In Papan Sodor the code really runs: a Socket.IO 4.8.4 server in Node, each phone its own process, and each phone's line through a connection the recorder can cut. The stage plays back the record of what happened.

A room is a group of sockets on the server. A socket can be in many rooms, and the server can send to one room only. Rooms exist only on the server. A phone does not know which rooms it is in unless the server tells it.

Socket.IO can ask for a receipt (acknowledgement): the last function given to emit is called when the other side answers. Without a deadline, the phone cannot tell "still on its way" from "lost". With a deadline, the phone knows when to stop waiting, even though it still does not know whether its message arrived.

A socket is already connected before its first event is sent, so checking who owns the phone happens in the handshake: the phone sends auth, the io.use middleware checks it, then calls next() to let it in or next(new Error(...)) to turn it away.

Without recovery, a phone that connects again is a new socket: events sent while it was gone never arrive. connectionStateRecovery asks the server to keep sessions and broadcast events, then replay them for a phone that comes back in time. Recovery covers only broadcast events without an ack, and only within its time limit.

WebRTC connects two browsers directly, for video or data. Before they connect, both have to swap an offer and an answer (SDP: what each can send) and ICE candidates (addresses worth trying). WebRTC does not say how that swap travels; here it goes through the same Socket.IO server. Today's record comes from two Chromium pages on one computer, ICE only on 127.0.0.1, the camera Chromium's test pattern.