Browser Client Reference¶
The client library is what your game imports in the browser. It signs the player in, gets tickets, joins rooms, and keeps the connection alive.
It is built on colyseus.js 0.16, the client of the Colyseus framework the multiplayer server runs on. You do not use colyseus.js yourself: the library handles the Colyseus connection, seat reservations and reconnecting, and gives you the smaller API below. Its own dependency on colyseus.js is bundled in, so there is nothing else to install.
Using Unity or Unreal? See the Unity C# client or the Unreal C++ client. They join the same rooms.
With the GameFuse JavaScript SDK¶
The GameFuse JavaScript SDK (game-fuse-js) has the same client built in, with no dependencies. If your game already loads gameFuseFull.min.js, GameFuseMultiplayer is there:
<script src="https://cdn.jsdelivr.net/gh/GameFuseCo/game-fuse-js@main/gameFuseFull.min.js"></script>
On its own, the file is V3/gameFuseMultiplayer.js. It works as a script tag, with import, or with require in Node 22 or later.
It adds two shortcuts for games that use the SDK:
new GameFuseMultiplayer()with no options uses the game ID and token fromGameFuse.setUpGame(...).mp.useToken()with no token plays as the player signed in withGameFuse.signInorGameFuse.signUp.
Everything else on this page is the same.
A whole session¶
const mp = new GameFuseMultiplayer({
gameId: 123,
gameToken: 'your-game-api-token',
});
await mp.signInAsGuest();
const room = await mp.joinQueue('1v1', { clientVersion: '1' });
room.on('players', (players) => drawPlayerList(players));
room.on('message', (type, data) => handleMessage(type, data));
room.on('closed', (reason) => showMenu(reason));
room.send('ready', {});
new GameFuseMultiplayer({ gameId, gameToken })¶
Makes the client.
| Option | Meaning |
|---|---|
gameId |
Your game's ID. |
gameToken |
Your game's API token. This is the same ID and token pair the rest of GameFuse uses to connect to your game. |
Signing in¶
A player must be signed in before joining a room. Use one of these.
mp.signInAsGuest()¶
Makes a guest player, so someone can play without an account. Guests get a username like Guest-4821.
The guest is made once. The library keeps the guest's token in localStorage under gamefuse.guest.<gameId>, so the same browser is the same guest next time.
Guest sign-in is limited to 10 a minute for each IP address. Guests who have not played for 30 days are removed.
mp.useToken(authenticationToken)¶
Use this for a player who has signed in to GameFuse with their own account. Pass the authentication_token GameFuse gave you when they signed in.
Getting into a room¶
Each of these gets a fresh ticket from the GameFuse API and joins a room on the multiplayer server. Each returns a promise for a room object.
The options you pass are given to your room code as player.options.
mp.joinQueue(queueName, options)¶
Joins a queue. The queue name is also the game mode: the room runs the room code uploaded for that mode, or your default room code if it has none. You are put in a room with other players who asked for the same queue, and you wait in that room until it is matched.
clientVersion is part of the match. Players are only matched with players who passed the same clientVersion. Change it when you release a build of your game that cannot play with the old one.
How long you wait, and which room you get, is up to matchmaking. By default you are in a room at once and wait there; if nobody else joins within 3 minutes the room closes with the reason 'no_match'. If the room code has its own findRoom, joinQueue resolves only when you are placed, and can reject:
error.code |
Why |
|---|---|
'no_match' |
Nobody was found in time. error.message is "No one is online right now. Try again later." |
'rejected' |
The room code turned you away (lobby.reject); error.message is its message. |
'cancelled' |
You called mp.leaveQueue(). |
'busy' |
Your game is at its usage limit, so no new room could be opened. Show something like "All rooms are busy. Try again in a minute." createRoom can reject with this too. |
mp.leaveQueue()¶
Stops waiting in a queue. A joinQueue that has not been placed yet rejects with error.code === 'cancelled'. (Once you are in a room, use room.leave().)
mp.createRoom(gameMode?, options)¶
Makes a private room. room.code is a 6-character code to give to friends. gameMode is optional: it picks which game mode's room code the room runs. Leave it out for default.
const room = await mp.createRoom({ skin: 'red' }); // default room code
const room = await mp.createRoom('superpong', { skin: 'red' }); // the superpong room code
showInviteCode(room.code);
mp.joinRoom(code, options)¶
Joins the private room with that code.
mp.resume()¶
Gets back into the room this browser tab was in before the page was reloaded. Gives you the room, or null when there is no room to go back to. Call it with await.
See connection drops and rejoining.
The room object¶
room.code¶
The room's 6-character code. Queue rooms have one too.
room.me¶
You: { id, name, seat }. id is your GameFuse user id. seat is 0, 1, 2 and so on, in the order players joined.
room.send(type, data)¶
Sends a message to your room code. It arrives in the room code's message(room, player, type, data).
A message may be at most 4 KB, and you may send 60 a second. More than that are dropped. See limits.
room.leave()¶
Leaves the room on purpose. Your room code sees playerLeft with 'quit'.
room.on(event, fn)¶
Listens for an event. The events are below.
Events¶
message¶
Your room code sent you something with room.send or room.broadcast.
players¶
The list of players changed. You get the whole list: [{ id, name, seat, connected }]. connected is false for a player who has dropped and whose seat is being held.
room.on('players', (players) => {
for (const p of players) {
drawPlayer(p.seat, p.name, p.connected);
}
});
reconnecting¶
Your own connection dropped. The library is trying to get back in. You do not have to do anything, but it is a good time to show a message.
reconnected¶
You are back in the room.
closed¶
You are out of the room, and the library has stopped trying. reason says why.
| Reason | Meaning |
|---|---|
'ended' |
Your room code called room.end. The game is over. |
'kicked' |
Your room code called room.kick on you. |
'lost' |
Your connection dropped and you could not get back in time. |
'left' |
You called room.leave(). |
'no_match' |
Nobody else joined in time, so the room closed before the game started. The second argument is "No one is online right now. Try again later." See Custom Matchmaking. |
room.on('closed', (reason) => {
if (reason === 'lost') showMessage('You were disconnected.');
showMenu();
});
Without the client library¶
The client library makes two calls to the GameFuse API for you: one to make a guest, one to get a ticket. Both are described in upload key and upload API.