Skip to content
Download all docs .md

Matchmaking and Private Room Codes

There are two ways for players to end up in the same room. A queue matches players who do not know each other. A private room is for friends, who share a code.

The queue name is also the game mode: joinQueue('superpong') runs the room code uploaded for the superpong game mode. A game with one kind of match can ignore modes; see Game Modes for games with several.

Queues

A player joins a queue by name:

const room = await mp.joinQueue('1v1', { clientVersion: '1' });

The queue name is yours to choose. Use different names for different ways to play, such as '1v1', 'ranked' or '4-player'. Each name can have room code of its own (make its folder with gamefuse multiplayer init --game-mode <queue name>); a name with none runs your default room code.

Who is matched with whom

Players are put in the same open room when all four of these are the same:

The game Players of your game are never matched with players of another game.
The queue name '1v1' players are only matched with '1v1' players.
clientVersion What each browser passed when joining.
The live version of that mode's room code Players who joined before you uploaded a new version are not mixed with players who joined after.

It is first come, first served. The first player to ask opens a room and waits in it. The next players to ask join that room. If nobody else joins within 3 minutes, the room closes and its players get closed with the reason 'no_match'.

This is the default. To match by skill, region, party or anything else, or to change how long players wait, write a findRoom function: see Custom Matchmaking.

When the game starts and when the room is full

  • When minPlayers players are in, the multiplayer server calls start in your room code.
  • When the game starts, the room locks, unless config.joinAfterStart is true. A locked room takes nobody else: the next player to ask goes to another room.
  • With joinAfterStart: true, players keep joining the running game until maxPlayers are in; then the room locks. Catch them up in playerJoined(room, player, { midGame: true }). See Players who join mid-game.

For a strict two-player game, set both to 2:

config: { minPlayers: 2, maxPlayers: 2, tickRate: 30, reconnectSeconds: 15 },

Showing who is waiting

The players event fires each time someone joins. Use it for a waiting screen. Have your room code send a message from start so every browser knows the game has begun.

// browser
room.on('players', (players) => {
  waitingText.textContent = players.length + ' in the room';
});
room.on('message', (type, data) => {
  if (type === 'start') showGame();
});
// room code
start(room) {
  room.broadcast('start', {});
},

clientVersion

Pass a clientVersion when you join a queue. Players with different values are never matched.

Change it when you release a build of your game that cannot play with the old one, for example when you change what your messages mean. Players still on the old build keep being matched with each other, and players on the new build with each other.

const room = await mp.joinQueue('1v1', { clientVersion: '2' });

The queue name in room code

A queue room's game round has the queue name as its game type. See room.round.

Private rooms

One player makes the room and gets a code:

const room = await mp.createRoom({ skin: 'red' });
inviteText.textContent = 'Tell your friend this code: ' + room.code;

To run a particular game mode's room code, name it first: mp.createRoom('superpong', { skin: 'red' }). Without a name, a private room runs the default room code.

The code is 6 characters, such as K7M2QX. The other players type it in:

const room = await mp.joinRoom(codeInput.value, { skin: 'blue' });

A private room follows the same config as a queue room. start is called when minPlayers are in, and the room locks at maxPlayers.

A private room's game round has the game type 'private'.

Locking a room yourself

Call room.lock() in your room code to stop anyone else joining, even if the room is not full. A common place is start, so nobody walks into a game that is already going:

start(room) {
  room.lock();
  room.broadcast('start', {});
},

This is useful when maxPlayers is larger than minPlayers. Without a lock, a room for 2 to 4 players starts with 2 and can still be joined by a third and fourth until it is full.

Telling players apart

In room code every player has a seat: 0 for the first to join, 1 for the second, and so on. In the browser, room.me.seat is your own seat. Use it to decide sides.

// room code
start(room) {
  for (const player of room.players) {
    room.send(player, 'side', { side: player.seat === 0 ? 'left' : 'right' });
  }
},

What a browser passes when joining shows up in room code as player.options:

// browser
const room = await mp.joinQueue('1v1', { clientVersion: '1', skin: 'red' });
// room code
playerJoined(room, player) {
  room.broadcast('skin', { id: player.id, skin: player.options.skin });
},

player.options comes from the player's browser. Check it before you trust it.