Why MQTT
Doom was written for a LAN, sending small packets straight to other computers. A web page can't do that. MQTT over WebSockets can, and its topics, retained messages and last wills cover the rest of what a multiplayer game needs.
What Doom asks of a network
Doom's multiplayer is lockstep. Every player runs the whole game, and only the players' moves travel: one small packet per player, 35 times a second, sent to the player who hosts. The host gathers everyone's moves and sends them back to all. A game only moves on once it has the moves of every player.
That packet is tiny, 11 to 22 bytes here, and Doom takes care of losing one: it numbers its packets and asks for the ones that don't arrive. So Doom needs little from a network: send these bytes to that player, quickly, and let them through in any order.
A web page can't send UDP packets, but it can open a WebSocket, a connection to a server. A broker in the middle turns that into what Doom wants: every browser connects once, and the broker passes each packet on to the player it's for.
How Doom maps onto MQTT
Chocolate Doom, the version that runs here, keeps its network behind a small interface: send a packet to an address, receive the next packet and its address. DooMQ adds an implementation of it that moves packets between Doom and the page, about 170 lines of C, and the page publishes and subscribes. Nothing else in Doom's network code changed.
| Doom needs | MQTT has | In DooMQ |
|---|---|---|
| A player's address | Topic levels |
Each player has a number, the host is 1. A packet goes to
doomq/games/<game>/<to>/<from>.
|
| Receiving packets | A subscription with a wildcard |
Each player subscribes to doomq/games/<game>/<me>/+, and the last level says who
sent it.
|
| Sending a packet, like UDP | QoS 0 | One message per packet, no acknowledgements. Doom resends what goes missing. |
| Finding a game | Retained messages |
The host keeps a listing on doomq/lobby/<game>. A new visitor subscribes to
doomq/lobby/+ and gets every listing at once.
|
| Noticing a host is gone | Last will | The host's will is an empty retained listing, which clears it from the lobby. |
| Noticing a player left | Last will | A player's will is Doom's own disconnect packet, sent to the host. The game goes on at once instead of waiting 10 seconds for the player to time out. |
| Saved games | Retained binary messages | The host publishes Doom's savegame, gzipped, so anyone can resume the game from the lobby later. |
| Spectators | Fan-out |
The host publishes the players and the scores to doomq/watch/<game> once, the broker
delivers them to every spectator. Spectators in Doom get the game the same way, on
doomq/games/<game>/0/1.
|
The topics
Five topics carry everything. The broker allows these and nothing else.
doomq/lobby/<game> retained JSON listing, refreshed every 5 s
doomq/games/<game>/<to>/<from> Doom packets, 35 a second per player, to 0 for spectators
doomq/saves/<game> retained JSON summary of the last save
doomq/savedata/<game> retained gzipped savegame
doomq/watch/<game> JSON for spectators, 9 a second
The traffic
Measured in four player games here:
- Each player publishes 35 packets a second to the host, 11 to 13 bytes each.
- The host publishes 35 a second to each other player, up to 22 bytes in a four player game.
- A four player game puts about 210 messages a second through the broker, and as many out. Spectators add about 9 a second each, 300 to 600 bytes, with the monsters' positions twice a second.
- Spectators in Doom get 35 packets a second each from the broker. The host publishes them once for all, so its upload stays the same however many watch.
- A save is at most about 60 KB gzipped, every two minutes.
The topic is the biggest part of a packet: doomq/games/37238d88cf2f/1/3735928559 is 37 bytes, and
a PUBLISH adds 4 more, against a 12 byte packet. That's still about 2 KB a second each way per player.
What MQTT gives here
- MQTT over WebSockets. How a browser connects, and LavinMQ serves it on its HTTP port.
- QoS 0 publish and subscribe. Doom's netcode does its own acknowledgements, so the broker doesn't have to.
- Topic levels and the
+wildcard. Addresses for players, games and kinds of message, with no setup per player. - Retained messages. The lobby and the saves need no database. An empty retained message deletes one.
- Last wills. Presence for hosts, and a clean goodbye for players whose tab closes.
- Binary payloads. Doom's packets and savegames go as they are, listings and spectator updates as JSON.
- Clean sessions and keepalives. Nothing is kept for a player who's gone.
The page's MQTT 3.1.1 client is about 230 lines of JavaScript with no dependencies. A game needs only a small part of MQTT.
What LavinMQ adds
-
MQTT over WebSockets on
/mqtt, on the same port as the management interface and the HTTP API. - A vhost of its own. DooMQ's username is
doomq:doomq, vhost and user. - Topic permissions. A permission group allows the five topic filters above, and the broker drops anything else.
-
Policies on MQTT sessions. LavinMQ keeps each MQTT session in a queue,
mqtt.<client id>, so a queue policy limits what a client that stops reading can pile up: 1 MiB, oldest first out. - A connection limit on the vhost, 500.
-
The HTTP API.
bin/configure-broker.jssets all of the above up from scratch, so a schedule can wipe the broker clean every day.
The broker does no game logic. Players in the lobby clear out listings of hosts that vanished without their will firing, and saves older than a day.
What it costs, and what's next
Every packet takes a detour through the broker, and lockstep makes everyone wait for the slowest player. In a test with the round trip of a player in New Zealand, a host in Sweden and the broker in Amsterdam, a move took 200 to 250 ms to show up, for everyone. A broker roughly between the players keeps that detour short.
MQTT 5's topic aliases would shrink the 37 byte topic to a few bytes after the first packet. LavinMQ is working on MQTT 5 support.