Multiplayer
P2P for fast games
Players connect straight to each other over WebRTC. One player hosts the game, the others send inputs and draw the host’s snapshots.
How a match runs
- Room. Players create or join a room on the game page, as with any multiplayer game.
- Host. When the match starts, ARCADE picks the host: the player with the lowest lag, then whoever joined first.
sdk.p2p.onHostChangetells every player. - Connect. Every player connects to the host, never to each other (a star, up to 8 players). ARCADE tries a direct connection, then its TURN relay after 5 s, then its own server after 10 s. Messages already flow through the server while that happens, so play starts at once.
- Play. Players send inputs to the host; the host runs the game and sends snapshots back.
- Host leaves. After 6 s ARCADE picks a new host and everyone reconnects to them. The new host carries on from its last snapshot.
| Where | What is kept |
|---|---|
| Host’s browser | The live game state, for the match only. |
| ARCADE server | The room, the match and its result (unranked). Connection offers and relayed messages pass through and are never stored. |
| Each player’s account | Anything your game saves with sdk.save, as in single player. |
The API
const sdk = new ArcadeSDK();
await sdk.ready();
sdk.p2p.onHostChange((state) => {
// state: { selfId, hostId, isHost, epoch, peers: [{ userId, transport, rttMs }] }
if (state?.isHost) startSimulation(lastSnapshot);
});
// Players: send inputs to the host. reliable: false uses the fast channel (may drop, never waits).
sdk.p2p.send({ seq, keys }, { reliable: false });
// Host: send a snapshot to everyone about 20 times a second.
sdk.p2p.broadcast({ t: hostTime, ack, players }, { reliable: false });
sdk.p2p.onMessage(({ from, data, reliable }) => { /* input or snapshot */ });| Call | What it does |
|---|---|
| sdk.p2p.state() | You, the host and each link’s transport (direct, turn, relay) and round trip. null outside a P2P room. |
| sdk.p2p.isHost() | True when this player runs the game. |
| sdk.p2p.send(data, { to?, reliable? }) | A player sends to the host. The host sends to one player with to. |
| sdk.p2p.broadcast(data, { reliable? }) | The host sends to every player. A player’s broadcast goes to the host. |
| sdk.p2p.onMessage(fn) | Every message for you: { from, data, reliable }. |
| sdk.p2p.onState(fn), onHostChange(fn) | Connection changes, and a new host (including the first). |
- A message is JSON or an ArrayBuffer, at most 16 KB. Binary is best for snapshots.
- Players can only talk to the host. If players need to see each other's chat, the host forwards it.
- The fast channel is unordered and never resends: use it for inputs and snapshots, and the reliable channel for events that must arrive (a hit, a pickup, the end of a round).
Smooth movement
The SDK includes three helpers (window.ArcadeSync with the script tag, or named imports from @arcade/sdk):
const { SnapshotInterpolator, Predictor, ClockSync } = ArcadeSync;
// Other players: draw 100 ms in the past, between two snapshots.
const others = new SnapshotInterpolator(); // holds the last state if snapshots stop for 250 ms
sdk.p2p.onMessage(({ data }) => others.push(data.t, data.players));
const view = others.sample(clock.now()); // { state, held }
// Yourself: move at once, then correct when the host confirms.
const me = new Predictor({ x: 0, y: 0 }, step); // step(state, input) is your game's own update
const input = me.input({ dx: 1 }); // { seq, input }, send it to the host
me.reconcile(snapshot.me, snapshot.ack); // replay the inputs the host hasn't applied yet
// The host's clock, for timestamps and countdowns.
const clock = new ClockSync(); // ping the host yourself; clock.add(sentAt, hostTime, receivedAt) on each replyHow many players, and how far apart
ARCADE measured a fast action game (60 ticks a second, up to 20 snapshots a second) on a good network and on a weak one (5% loss, jitter and a 384 kbit/s upload, like a busy phone or Wi-Fi link). Design your game for these numbers.
| Players | Good network | Weak network | Host upload |
|---|---|---|---|
| 2 | Smooth | Fine: about 15 snapshots a second arrive | about 40 kbit/s |
| 4 | Smooth | Fine. The safest choice for most games | about 140 kbit/s |
| 6 | Smooth | Choppy: about 11 snapshots a second, and lag doubles | about 310 kbit/s |
| 8 (the limit) | Works | Not recommended: the host’s upload runs out | about 450 kbit/s |
The host's upload is what runs out first, not the players' downloads and not the server. It grows faster than the player count, because every extra player gets a copy of every snapshot and makes each snapshot bigger.
| Round trip | Typical for | How a fast game feels |
|---|---|---|
| under 40 ms | Same city, on broadband | Instant |
| 40 to 80 ms | Same country, or same city on 4G | Solid |
| 80 to 150 ms | Neighbouring countries, or same country on 4G | Good; hits confirm a moment late |
| 150 to 250 ms | Across continents | Playable, but other players visibly jump |
| over 250 ms | Opposite sides of the world | Sluggish for action; fine for turn-based games |
Messages kept arriving even at a 330 ms round trip, so distance doesn't break a match. It only adds delay. A weak link adds another 30 to 60 ms on top. Your own movement is instant at any distance when you use Predictor; distance delays what the host decides, such as hits, pickups and other players' positions.
- Pick a player count for the weak case. 4 is safe everywhere. Go to 6 or 8 only if your game still feels right with about 10 snapshots a second.
- Keep snapshots small. Send only what changed with
DeltaEncoderandDeltaDecoder, and useSendRateAdapterto send fewer snapshots to a player whose link is struggling. In ARCADE's test, a snapshot was 160 to 420 bytes for 2 to 8 players. - Design around 150 ms. Leave out frame-perfect timing. Give hit boxes a little slack, give bullets travel time, and let the host decide hits.
- Expect about 5% of fast-channel messages to be lost on weak links. Send the keys that are held down every tick, not just presses and releases, and send events that must arrive on the reliable channel.
- Show the lag.
sdk.p2p.state().peers[i].rttMsis each link's round trip. Consider a warning above 150 ms.
Scores and fairness
The host runs the game, so a host could cheat. P2P matches are unranked unless ARCADE can replay them: every player keeps a move log, and when the match ends ARCADE plays the host's log again with your game's rules on the server.
// When the match starts (sdk.p2p.state().matchId is set):
const log = sdk.p2p.createLog();
// Host: record every input you apply, your own included, and send the head with snapshots.
log.record({ tick, from: playerId, seq, input });
sdk.p2p.broadcast({ type: 'snapshot', ...world, head: log.head() }, { reliable: false });
// Player: record each input you send, and note the host's head from each snapshot.
log.record({ seq, input });
log.noteHostHead(snapshot.head);
// When the match ends, everyone uploads. ARCADE answers once all logs are in (or after 30 s).
const { verdict } = await sdk.p2p.submitLog(log, { scores });
// verdict.status: 'ranked' | 'void' | 'unranked'| Verdict | When |
|---|---|
| ranked | Every chain is whole, the host applied each player’s own inputs unchanged and in order, and every move is legal. ARCADE’s own result goes to the leaderboard. |
| void | A log was edited, the host changed, invented or dropped too many of a player’s inputs, or a move broke the rules. Moderators are told. |
| unranked | The game has no replay rules on the server yet, or the host changed during the match. |
- Each entry is chained to the one before it with SHA-256, so a log can't be edited without it showing.
- A player who doesn't upload within 30 s forfeits. The fast channel may drop a few inputs; up to 10% is allowed.
- Your game's rules must be deterministic: whole numbers and the seeded random numbers in the example, never
Math.random(). ARCADE staff add the matching replay rules when your game is set to P2P; Dot Arena inpackages/game-sdk/example-p2pshows both halves. - Logs are kept for 30 days for appeals.
Privacy
A direct connection shows each player's IP address to the host. Players can tick Hide my IP in the room panel; they then connect only through ARCADE's relays, which can add a little lag.
Testing
Open the game in two browsers signed in as two players, create a room in one and join with the invite link in the other. The room panel shows each link's transport and round trip. Outside ARCADE, sdk.p2p.state() stays null, so write your game to work alone too.