DooMQ brokered by LavinMQ How it works Connecting…

How it works

Every DooMQ game is a handful of MQTT topics on LavinMQ. This page follows one game from the lobby to the last frag, with the messages it sends.

The lobby opens

The browser connects to LavinMQ with MQTT 3.1.1 over a WebSocket, on /mqtt, as user doomq in the vhost doomq. It subscribes to the listings of games and of saves.

Every listing is a retained message, so the broker hands over the latest one of each game the moment the subscription starts. The lobby needs no database and no API, only the broker. Meanwhile the page downloads Doom: Chocolate Doom compiled to WebAssembly, and Freedoom's maps.

SUBSCRIBE  doomq/lobby/+
RECEIVE    doomq/lobby/465811044f7b  retained
           {"title":"Rip and tear","mode":"deathmatch","skill":3,"map":"E1M1",
            "monsters":true,"host":"Hanna","players":2,"maxPlayers":8,"state":"playing"}

A player hosts

The host's browser tab runs Doom's server, as player number 1. It publishes the game's listing, retained, and again every 5 seconds with the number of players.

Its last will is an empty retained listing. When the tab closes or the connection drops, the broker publishes it, and the game disappears from every lobby. A listing that goes quiet for 15 seconds, say after a broker restart, gets cleared by the players in the lobby.

CONNECT    will doomq/lobby/465811044f7b  ""  retained
PUBLISH    doomq/lobby/465811044f7b  retained, every 5 s
SUBSCRIBE  doomq/games/465811044f7b/1/+

Players join

A player who joins picks a random number as its address, subscribes to the packets for that number, and Doom connects to the host. The last level of every topic says who sent the packet.

A player's last will is Doom's own disconnect packet, sent to the host. When a tab closes, the game goes on with the others at once, instead of waiting 10 seconds for the player to time out.

CONNECT    will doomq/games/465811044f7b/1/1196479033  00 08  Doom's disconnect
SUBSCRIBE  doomq/games/465811044f7b/1196479033/+

The game runs

Doom's multiplayer is lockstep: every player runs the whole game, and only the moves travel. Each player sends the host one packet with its moves 35 times a second. The host gathers everyone's moves and sends them back to all, and the game moves on once it has the moves of every player.

PUBLISH    doomq/games/465811044f7b/1/1196479033  to the host
           00 06 c0 be 02 00 00 00 00 00 00
RECEIVE    doomq/games/465811044f7b/1196479033/1  from the host
           00 06 bf 02 00 22 01 10 86 00 18 01 10 c1
           35 of each a second, 00 06 is Doom's game data

The packets go as QoS 0, like UDP, the way Doom was built for. Doom numbers its packets and asks again for any that don't arrive, so the broker needn't acknowledge anything.

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.

The game saves

Every two minutes all players save the game at the same moment. The host publishes its savegame, gzipped, and a summary of it, both retained.

When the game has ended, the lobby offers to resume it. Whoever resumes it hosts the new game, reads both messages, and Doom loads the save once as many players as before have joined, or when the host starts it. Players in the lobby clear saves older than a day with an empty retained message.

PUBLISH    doomq/savedata/465811044f7b  retained
           13 KB of gzipped savegame
PUBLISH    doomq/saves/465811044f7b  retained
           {"title":"Rip and tear","mode":"deathmatch","skill":3,"map":"E1M1",
            "monsters":true,"host":"Hanna","players":2,"savedAt":1795528106676,"format":2}

Spectators watch

Nine times a second the host publishes where the players are, their health and their scores, and twice a second the monsters too. The broker delivers a copy to every spectator, so the host's cost stays the same however many watch. A spectator of all games subscribes to doomq/watch/+, and draws the maps from Doom's own files.

PUBLISH    doomq/watch/465811044f7b  9 a second
           {"state":"level","map":"E1M1","time":191,"deathmatch":true,"players":[
            {"number":0,"name":"Hanna","x":766,"y":592,"angle":224,"health":53,...},
            {"number":1,"name":"Ozzy","x":2485,"y":-560,"angle":96,"health":24,...}]}

Spectators can also watch in Doom, if they join before the game starts. Each joins Doom's server as a drone, which sends no moves, and all of them listen on peer 0: the host publishes each tic there once, and the broker copies it to every spectator. Doom's server doesn't wait for spectators, so a slow one only falls behind itself.

CONNECT    will doomq/games/465811044f7b/1/2734151093  00 08  Doom's disconnect
SUBSCRIBE  doomq/games/465811044f7b/2734151093/+
SUBSCRIBE  doomq/games/465811044f7b/0/+  with every other spectator
RECEIVE    doomq/games/465811044f7b/0/1  the game, published once for all

The broker keeps it in check

LavinMQ does no game logic, but it decides what gets through:

doomq/lobby/+          listings
doomq/games/+/+/+      Doom's packets
doomq/saves/+          save summaries
doomq/savedata/+       savegames
doomq/watch/+          for spectators

Why MQTT

A web page can't send UDP packets, but it can open a WebSocket to a server. With a broker as that server, every piece of the game above comes from MQTT itself: topics as addresses, QoS 0 for Doom's packets, retained messages for the lobby and the saves, last wills for players who leave, and fan-out for spectators. The page's MQTT client is about 230 lines of JavaScript.

Next up would be MQTT 5's topic aliases, which shrink a 37 byte topic to a few bytes after the first packet. LavinMQ is working on MQTT 5 support. The longer case for MQTT goes through what Doom asks of a network, feature by feature.

Play a game MQTT in LavinMQ The source