Match players into fair games and manage sessions. A tested queue with a skill window that widens the longer players wait, parties kept together, team balancing, optional regions, backfill for matches that lost players, queue position, and Elo and team Elo rating updates; plus when matchmaking is worth it (player counts), bots for empty queues, showing wait times, and match lifecycle from queue...
Download skill (.zip) Files on GitHubMatchmaking system
Do you need it? Matchmaking needs players waiting at the same time. A small browser game rarely has that: with fewer than about 50 people online at once, use quick join (multiplayer-lobbies, fills the fullest open room) plus invite codes, and fill empty seats with bots. Add rating-based matchmaking when queues are busy.
assets/matchmaker.js
import { createMatchmaker, elo, teamElo } from "./matchmaker.js";
const mm = createMatchmaker({ size: 4, teams: 2, window: 100, widenPerSec: 25, maxWindow: 600, regionStrict: true, maxWaitSec: 45 });
mm.enqueue({ id: "p1", rating: 1200, region: "eu" });
mm.enqueue({ id: "party9", members: ["p2", "p3"], rating: 1350 }); // parties stay together
setInterval(() => { for (const m of mm.tick()) createMatch(m); }, 1000); // m = { players, teams: [[..],[..]], rating, waited }
mm.cancel("p1"); mm.position("p1"); mm.backfill(1, matchRating, { region });
const [a2, b2] = elo(a, b, 1); // 1 = a won, 0.5 draw, 0 = a lost
- The longest-waiting player is matched first, so nobody starves.
- Both players’ windows must allow the gap, so a new player with a wide window isn’t thrown at someone who waited 1 s.
- Regions: same region first, any region after
maxWaitSec. - Teams: biggest parties first, each to the team with the lower total rating.
Session lifecycle
queued → found (show "Match found!") → room created (lobbies.create + join for each) → playing → results → ratings updated → back to menu or requeue
- Ask found players to accept within 10 s only for long matches; for short games, start directly.
- Someone fails to connect within 15 s: start without them, backfill, or return everyone to the queue at the front
(keep their original
since). - Show the queue honestly: time waited, and “Searching wider…” after the window grows. After 60 s offer “Play with bots”.
Ratings
- Start everyone at 1000.
k = 32for the first 20 games, then 16. - Store ratings on the server only. Show a rank name (“Silver”) instead of the number if it’s discouraging.
- Leaving a ranked match counts as a loss.
- Hidden rating for matchmaking and a visible rank that only goes up is common in casual games.
Tested
Node tests: closest rating paired first, no match outside the window, the window widening over time, parties kept on one team, party too big refused, team balancing, Elo and team Elo values, backfill picking the closest player.
Pitfalls
Matchmaking for a game with 5 players online (use quick join), ratings computed in the browser, long waits with no feedback, and parties split across teams.