How to make an RTS or tower defence game multiplayer
Updated 2026-11-23
Short answer: use deterministic lockstep. A strategy game has far too much state to send (hundreds of units, projectiles, fog, economy), so only the players' orders travel, and every device runs the same simulation from the same seed, one fixed tick at a time. It works if your simulation is deterministic: seeded randomness, no clock, fixed iteration order and maths that gives the same answer in every browser. Joinable's lockstep profile carries the orders, drives the ticks, checks every second that all devices still agree, and runs the waiting room, teams, AI seats and results. Firestorm, a real RTS, ran a 4 v 4 battle on it for 10,160 ticks with 0 differences.
Why lockstep, not state sync
A racer can send each car's position 20 times a second. An RTS can't send hundreds of units that often. But orders are tiny: "these 12 tanks: attack building 340" is a few dozen bytes. If every device applies the same orders on the same tick to the same starting battle, every device computes the same battle. Neither is "the server"; both are the battle.
What you get:
- Heavy games are fine. Meshes, textures and sounds never travel.
- Cheap rooms. An 8-player battle carries about 90 messages a second.
- A small input delay. Orders run about 0.3 s after the click, so they can reach every device first. Acknowledge the click at once (a marker, a unit voice), as RTS games always do.
The trade-offs: the whole simulation must be deterministic, and every device simulates the whole battle, so a cheater with developer tools could see through the fog of war.
Does your game fit?
Check all six before starting:
- The simulation can run without the screen. Logic is separate from drawing, or can be.
- Every random number comes from one seeded generator. Cosmetic effects may still use
Math.random(). - The simulation never reads the clock. No
Date.now(),performance.now()or frame delta: whole fixed ticks only. - Player actions can become data, applied in one place.
- Iteration order is fixed: arrays or sorted ids, not Sets or Maps filled in network order.
- The maths gives the same answer in every browser.
+ − × ÷,Math.sqrt,floor,round,abs,min,maxandimuldo.Math.sin,cos,tan,atan2,hypot,exp,log,powand**may not: Chrome and Safari can differ in the last digit, and a battle feeds each result into the next tick. WriteMath.hypot(x, y)asMath.sqrt(x * x + y * y)and use a lookup table for sin and cos inside the simulation.
Rule 6 came from Firestorm's play-test: Chrome against Safari went out of sync at tick 2412, with no orders anywhere near it. The cause was Math.hypot, sin, cos and atan2 in the simulation.
The code
(window.JoinableQ = window.JoinableQ || []).push((Joinable) => {
Joinable.game({
profile: 'lockstep',
name: 'Tiny Front',
build: 'tiny-front-1', // bump when the simulation changes: other builds never meet
players: { min: 2, max: 8 },
tick: 50, // ms per simulation step
start({ seed, options, players, me }) { buildBattle(seed, players, me); },
step(commands, { tick, away, ai }) {
for (const { player, cmd } of commands) apply(player, cmd);
runOneTick();
},
hash() { return fingerprint(); }, // one number from ids, positions, health, money
testCommand(rng) { return randomValidOrderForMe(rng); },
stop() { showMenu(); },
});
});
// The UI no longer applies orders. It sends them; they run on every device in step().
Joinable.command({ t: 'move', ids: [12, 14], x: 340, y: 220 });
// When the battle ends (every device sees it on the same tick):
window.Joinable?.finish({ place: iWon ? 1 : 2 });
Online, stop your own simulation loop and keep drawing: step() is the only place the battle advances. No pause or speed-up online. Load the script async with data-connect="invite", as in every guide. Tiny Front is the reference RTS: its online.js marks every Joinable line.
What Joinable handles
- Waiting for late orders ("Waiting for Sam…"), and an input delay set per round from the two slowest players' pings: 400 ms in our test with one player 500 ms away.
- Drops: after 3 s the battle carries on and the player's units keep their last orders. Back within a minute, their device catches up from the room's log. After a minute,
step()lists them inaiand your game's AI plays their side. - Teams and AI seats:
teamsgives sides in the waiting room, with Balance and Shuffle for the host. Withbots: true, the host can fill empty seats with your game's own AI, played the same way on every device. - Formats: list
formats(1 v 1, 2 v 2, 4 v 4) and players pick one before Invite or Quick match. Quick match pairs only players who picked the same format and build. - Fingerprints every second. If they ever differ, the battle stops on every device ("This battle went out of sync. It won't count."), and the room keeps a desync report: the first differing tick, each device's browser and build, and the orders around it.
What Firestorm showed
Firestorm is NOJU's real-time strategy game: armies, bases, production queues, generals' powers and a computer opponent. A coding agent with only Joinable's public prompt, guide and the Tiny Front demo made it a two-player lockstep game in about 66 minutes: 9 files, 357 lines added.
Then 4 v 4: 8 headless clients with AI on every side ran 10,160 ticks, compared 71,120 fingerprints with 0 differences, and produced one result in 9.1 minutes. What broke along the way is in what we learned. You can play Firestorm on etheria.games.
Tower defence and auto-battlers
The same rules apply. In a co-op tower defence, placing a tower is an order and waves come from the seed. Auto-battlers are often simpler still: orders come only between rounds.
Test it
npx getjoinable@latest test http://localhost:5173/
The self-test plays the battle on Chromium and WebKit (Safari's engine) with random orders from testCommand, compares fingerprints tick by tick, and names what isn't deterministic: Math.random() or clock calls inside step() with file and line, and browser-dependent maths. It needs WebKit once: npx playwright-core install webkit. Can't run npx? Open the game with ?joinable-check in two windows for the same checks in a panel.
Or paste the prompt into your coding agent.
FAQ
What is deterministic lockstep?
Every device runs the same game simulation from the same seed, applying the same player orders on the same fixed ticks, so they all compute the same result. Only the orders travel over the network, which is why it suits strategy games with hundreds of units.
Why did my lockstep game desync between Chrome and Safari?
Usually maths. Math.sin, cos, atan2, hypot, pow and similar may round differently in the last digit in different browser engines, and the error grows tick after tick. Use Math.sqrt with multiplication, and lookup tables for angles, inside the simulation.
How many players can a lockstep battle have?
Joinable's lockstep is tested at 8 players, 4 against 4, with AI seats. Rooms hold 4 on the free Sandbox plan on a live site, 8 on Starter and Pro, and 16 on Studio; on localhost they hold 8 on every plan.
Can players cheat in a lockstep RTS?
Every device simulates the whole battle, so a cheater with developer tools could reveal the fog of war. That's acceptable between friends. Ranked play would need a server that runs the battle and shows each player only what they can see, which Joinable doesn't offer yet.
Does it work with Phaser or three.js?
Yes, as long as the simulation runs outside the engine's frame loop. Keep units in plain data that step() updates, and let the engine only draw. Don't use Phaser's Arcade physics for the simulation online: its timing follows the frame rate.