Skip to content
Download all docs .md

Custom Matchmaking

Matchmaking decides which room a player goes into when they call joinQueue. GameFuse does it for you with a simple rule. When you want something smarter, such as matching players of similar skill, you write one function, findRoom, in your room code. GameFuse calls it and does what it says.

The default: no code needed

If your room code has no findRoom:

  1. A player who calls mp.joinQueue('pong') goes into the oldest open pong room that already has players in it and a free seat.
  2. If there is no such room, a new room is opened for them, and they wait in it for others.
  3. When config.minPlayers players are in, start(room) is called and the game begins.
  4. If the room has not started 3 minutes after it opened, nobody else is coming. The room closes, and every player in it gets closed with the reason 'no_match' and the text "No one is online right now. Try again later."
room.on('closed', (reason, detail) => {
  if (reason === 'no_match') showMessage(detail); // "No one is online right now. Try again later."
});

To wait longer or shorter, set waitSeconds. Nothing else changes:

export default {
  config: { minPlayers: 2, maxPlayers: 2, tickRate: 30 },
  matchmaking: { waitSeconds: 60 }, // give up after 1 minute instead of 3
  start(room) {},
  update(room, dt) {},
  gameEnd(room, result) {},
};

Your own rules: findRoom

Add a findRoom(player, lobby) function to the default export, next to start and update. With findRoom:

  • A player who calls joinQueue does not go into a room straight away. They wait in the queue's lobby.
  • Every matchmaking.everySeconds (1 second unless you change it), GameFuse calls findRoom once for each waiting player, oldest first.
  • findRoom looks at the player, at every open room in the queue, and at everyone else waiting, and returns what to do with this player.
  • joinQueue in the player's browser resolves when they are given a room.
  • If they are still waiting after matchmaking.waitSeconds (3 minutes unless you change it), joinQueue rejects with error.code === 'no_match' and the message "No one is online right now. Try again later."
export default {
  config: { minPlayers: 2, maxPlayers: 2, tickRate: 30 },

  matchmaking: {
    waitSeconds: 120,      // give up after 2 minutes
    everySeconds: 1,       // ask findRoom about everyone waiting once a second
    attributes: ['skill'], // load these GameFuse user attributes for every waiting player
  },

  findRoom(player, lobby) {
    const skill = Number(player.attributes.skill ?? 1000);
    const close = (other) => Math.abs(Number(other.attributes.skill ?? 1000) - skill) <= 100;

    const room = lobby.rooms.find((r) => r.players.every(close));
    if (room) return lobby.join(room);

    const partner = lobby.waiting.find(close);
    if (partner) return lobby.create([partner]);

    return lobby.wait();
  },

  start(room) {},
  update(room, dt) {},
  gameEnd(room, result) {},
};

The toolbox

player: who is waiting

Field What it is
player.id Their GameFuse user ID.
player.name Their username.
player.options Whatever their browser passed to joinQueue(queue, options), for example { region: 'eu', partySize: 2 }.
player.attributes The GameFuse user attributes you listed in matchmaking.attributes, for example { skill: '1320' }. GameFuse stores attribute values as text, so use Number(...) for numbers. An attribute the player does not have is left out.
player.waitedSeconds How long they have been waiting.
player.secondsLeft How long until they are told nobody is online.

lobby: what is going on, and your four answers

Field What it is
lobby.rooms Every open room in this queue with at least one free seat (a room whose game has started is only here if its room code has config.joinAfterStart: true): { id, code, players, seatsLeft, maxPlayers, started, ageSeconds }. Each of players has id, name, options and attributes.
lobby.waiting Everyone else still waiting in this queue, oldest first, in the same shape as player.
lobby.config { minPlayers, maxPlayers } from your config.
lobby.random() A number from 0 to 1.

Return exactly one of these:

Answer What happens
lobby.join(room) The player gets a seat in that room (one of lobby.rooms, or its id).
lobby.create() A new room is opened for the player alone, and they wait in it. The 3-minute (waitSeconds) rule then applies to that room.
lobby.create([a, b]) A new room is opened for the player and the waiting players you name (from lobby.waiting, or their IDs). Everyone named goes in together. At most maxPlayers in total.
lobby.wait() Nothing yet. findRoom is asked about this player again next time. Returning nothing does the same.
lobby.reject('message') The player is sent away: joinQueue rejects with error.code === 'rejected' and your message.

Answers are applied in order, and each one is visible to the next player's call. If you put a player in the last seat of a room, the next player does not see that room any more. If you take Ann and Bo into a new room, they are not in lobby.waiting for anyone after, and findRoom is not called for them again.

matchmaking: the settings

Setting Default Meaning
waitSeconds 180 How long a player waits before "No one is online right now. Try again later." 5 to 1800. Applies with and without findRoom.
everySeconds 1 How often findRoom is asked about everyone waiting. 0.25 to 30. Waiting longer between rounds lets more players gather before you decide.
attributes [] GameFuse user attribute names to load for each player when they join the queue, up to 20.

Rules for findRoom

  • Quick and synchronous. No await, no room.gamefuse. Everything it needs is in player and lobby. One round for everyone waiting must finish within 1 second, or it is stopped and nobody is placed that round.
  • It has no room. findRoom runs before there is a room. It cannot send messages or keep room.state.
  • If it throws, the error goes to your dashboard's Logs and gamefuse multiplayer logs, and that player stays waiting.
  • It runs for one game mode. Each game mode has its own room code, so pong and superpong can match players differently.

Recipes

Similar skill, widening the longer they wait

Start strict and relax, so nobody waits the full time just because there is no perfect match.

matchmaking: { waitSeconds: 120, attributes: ['skill'] },

findRoom(player, lobby) {
  const skill = Number(player.attributes.skill ?? 1000);
  const range = 50 + player.waitedSeconds * 10; // 50 at first, 650 after a minute
  const near = (p) => Math.abs(Number(p.attributes.skill ?? 1000) - skill) <= range;

  const room = lobby.rooms.find((r) => !r.started && r.players.every(near));
  if (room) return lobby.join(room);
  const partner = lobby.waiting.find(near);
  if (partner) return lobby.create([partner]);
},

Gather for 30 seconds, then make the fairest pairs

Let a crowd build up, then look at everyone at once. Here the oldest player waits until they have been in the queue 30 seconds, then takes whoever is closest in skill.

matchmaking: { waitSeconds: 180, everySeconds: 5, attributes: ['skill'] },

findRoom(player, lobby) {
  if (player.waitedSeconds < 30 || lobby.waiting.length === 0) return lobby.wait();
  const skill = Number(player.attributes.skill ?? 1000);
  const gap = (p) => Math.abs(Number(p.attributes.skill ?? 1000) - skill);
  const best = [...lobby.waiting].sort((a, b) => gap(a) - gap(b))[0];
  return lobby.create([best]);
},

Regions, from what the browser sends

// browser
const room = await mp.joinQueue('pong', { clientVersion: '1', region: 'eu' });
// room code
findRoom(player, lobby) {
  const same = (p) => p.options.region === player.options.region;
  const room = lobby.rooms.find((r) => r.players.every(same));
  if (room) return lobby.join(room);
  // after a minute, anyone will do
  if (player.waitedSeconds > 60 && lobby.rooms.length) return lobby.join(lobby.rooms[0]);
  return lobby.create();
},

Friends who queue together

The browser passes the size of the party; the room code keeps parties in one room.

// browser: every member of the party passes the same partyId
await mp.joinQueue('2v2', { clientVersion: '1', partyId: 'k3x9', partySize: 2 });
// room code (config.maxPlayers: 4)
findRoom(player, lobby) {
  const party = player.options.partyId;
  if (!party) return lobby.rooms[0] ? lobby.join(lobby.rooms[0]) : lobby.create(); // solo: fill any room
  const mates = lobby.waiting.filter((p) => p.options.partyId === party);
  if (mates.length + 1 < player.options.partySize) return lobby.wait(); // the rest are still coming
  return lobby.create(mates); // the whole party goes into a new room together; solo players fill the rest
},

Turning players away

matchmaking: { attributes: ['banned_until'] },

findRoom(player, lobby) {
  if (Date.parse(player.attributes.banned_until ?? '') > Date.now()) {
    return lobby.reject('You cannot play ranked games until your ban ends.');
  }
  return lobby.rooms[0] ? lobby.join(lobby.rooms[0]) : lobby.create();
},

In the browser

showWaiting('Finding an opponent...');
cancelButton.onclick = () => mp.leaveQueue();

try {
  const room = await mp.joinQueue('pong', { clientVersion: '1' });
  showGame(room);
  room.on('closed', (reason, detail) => {
    if (reason === 'no_match') showMessage(detail); // default matchmaking: nobody joined the room
  });
} catch (error) {
  if (error.code === 'no_match') showMessage(error.message);       // "No one is online right now. Try again later."
  else if (error.code === 'rejected') showMessage(error.message);  // your lobby.reject message
  else if (error.code === 'cancelled') showMenu();                 // mp.leaveQueue() was called
  else if (error.code === 'busy') showMessage('All rooms are busy. Try again in a minute.'); // the game is at its usage limit
  else showMessage('Could not connect. ' + error.message);
}
How it ends Default matchmaking With findRoom
Matched joinQueue resolves straight away; start(room) runs when enough have joined joinQueue resolves when findRoom places them
Nobody in time room.on('closed') with 'no_match' joinQueue rejects with error.code === 'no_match' (or closed('no_match') if findRoom put them in a room with lobby.create() alone and nobody joined)
Turned away — joinQueue rejects with error.code === 'rejected'
Gave up waiting room.leave() mp.leaveQueue(), then joinQueue rejects with error.code === 'cancelled'

On the dashboard

Players waiting in a lobby count in Players waiting in a queue on the Multiplayer tab. Errors from findRoom appear in Logs with the room column set to lobby:<queue name>.