DooMQ brokered by LavinMQ How it works Connecting…

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 on MQTT

Doom needsMQTT hasIn 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:

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

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

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.

Play a game MQTT in LavinMQ The source