Skip to content
Download all docs .md

Multiplayer Quick Start

This page builds a small two-player game. Two players are matched. The first to tap a button 10 times wins, and the winner gets one point added to their GameFuse score.

Before you start

You need:

  • A GameFuse account and a game. Note the game's ID and API token.
  • Multiplayer switched on for that game, and the game's upload key. Both are in the dashboard: open your game, then Multiplayer. See upload key.
  • Node.js on your computer.

1. Install the command

npm i -g gamefuse

2. Add the files to your project

Run this in your game's folder. Name your game, the game mode (the queue name players will join), and give your game's ID from the dashboard:

gamefuse multiplayer init --game-name pong --game-mode default --game 123

It makes a folder called <game-name>-<game-mode>-gamefuse-multiplayer, here pong-default-gamefuse-multiplayer, with four files in it:

File What it is
room.js Your room code. It starts as a small working example.
gamefuse-room.d.ts Types for the room code, so your editor can help you.
AGENTS.md Notes for AI coding agents.
multiplayer.json Settings for the command: the game, the game mode, and which file to upload.
{
  "gameName": "pong",
  "gameMode": "default",
  "gameId": 123,
  "entry": "room.js"
}
Flag Meaning
--game-name Required. Your game's name. It is made folder-safe: "Produce Conflict!" becomes produce-conflict.
--game-mode The queue name players join for this room code, such as superpong. Leave it out for default, which runs for any queue with no code of its own.
--game Your game's ID. Leave it out and set gameId in multiplayer.json later.

A game with several modes gets one folder per mode, each with its own room.js:

pong-pong-gamefuse-multiplayer/
pong-superpong-gamefuse-multiplayer/

The other commands (validate, deploy, list, activate, logs) find the folder by themselves when you run them next to it or inside it. With more than one folder next to you, run them inside the one you mean, or add --dir pong-superpong-gamefuse-multiplayer.

3. Write the room code

Replace room.js in your new folder with this:

const GOAL = 10;

export default {
  config: {
    minPlayers: 2,
    maxPlayers: 2,
    tickRate: 0,
    reconnectSeconds: 15,
  },

  start(room) {
    room.state.taps = {};
    for (const player of room.players) {
      room.state.taps[player.id] = 0;
    }
    room.broadcast('start', { goal: GOAL });
  },

  update(room, dt) {},

  message(room, player, type, data) {
    if (type !== 'tap') return;
    if (!room.state.taps || room.state.over) return;

    room.state.taps[player.id] += 1;
    room.broadcast('taps', room.state.taps);

    if (room.state.taps[player.id] >= GOAL) {
      finish(room, player);
    }
  },

  playerLeft(room, player, how) {
    if (!room.state.taps || room.state.over) return;
    const other = room.players.find((p) => p.id !== player.id);
    if (other) finish(room, other);
  },

  async gameEnd(room, result) {
    await room.gamefuse.users.addScore(result.winnerId, 1);
    room.log('saved a point for', result.winnerId);
  },
};

function finish(room, winner) {
  room.state.over = true;
  room.broadcast('winner', { id: winner.id, name: winner.name });
  room.end({ winnerId: winner.id });
}

What it does:

  • start runs when two players are in. It sets every player's count to 0 and tells both browsers the game has begun.
  • message runs each time a browser sends something. It counts taps and tells both players the new counts.
  • playerLeft runs when a player quits, or drops and does not come back within 15 seconds. The other player wins.
  • gameEnd runs after room.end. It adds one point to the winner's GameFuse score.
  • update is empty because tickRate is 0. This game only reacts to messages.

4. Upload it

Put your upload key in the .env file in your project folder. gamefuse multiplayer init made it for you:

# .env  (keep it out of git: it holds a secret)
GAMEFUSE_UPLOAD_KEY=mpk_your_upload_key
GAMEFUSE_GAME_ID=123

The key is on your game's Multiplayer tab. Never name the key VITE_... or anything else your build tool puts into the game, or players can read it.

Keep .env out of git. init already added it to your .gitignore if you had one. If not:

echo ".env" >> .gitignore

Check the room code. This runs the same check our servers run and uploads nothing:

gamefuse multiplayer validate

Fix anything it lists until it says OK. Then upload:

gamefuse multiplayer deploy

The command checks your room code, uploads it, and makes it the live version. New rooms use it from now on.

This uploads to the folder's game mode, here default, which runs for every queue. If your game has more than one kind of match, make a folder for each with gamefuse multiplayer init --game-name pong --game-mode superpong and deploy from it. The same upload key works for all of them. See Game Modes.

Developing next to a live game

Upload work in progress to its own mode: gamefuse multiplayer deploy --game-mode pong-dev from your pong folder, and have your development build call joinQueue('pong-dev'). Players on pong never see it.

5. Connect from the browser

Making a Unity or Unreal game? Do this step with the Unity C# client or the Unreal C++ client instead. The room code from the steps above stays the same.

Add the gamefuse-multiplayer client library to your game. Then add this page and script. The script uses an import, so build it with the bundler your game already uses.

<p id="status">Connecting...</p>
<button id="tap" disabled>Tap</button>
<script type="module" src="./game.js"></script>
// game.js
import { GameFuseMultiplayer } from 'gamefuse-multiplayer';

const status = document.getElementById('status');
const button = document.getElementById('tap');

const mp = new GameFuseMultiplayer({
  gameId: 123,
  gameToken: 'your-game-api-token',
});

await mp.signInAsGuest();

const room = await mp.joinQueue('1v1', { clientVersion: '1' });
status.textContent = 'Waiting for another player...';

room.on('message', (type, data) => {
  if (type === 'start') {
    button.disabled = false;
    status.textContent = 'Go! First to ' + data.goal + ' taps wins.';
  }
  if (type === 'taps') {
    const mine = data[room.me.id];
    status.textContent = 'Your taps: ' + mine;
  }
  if (type === 'winner') {
    button.disabled = true;
    status.textContent = data.id === room.me.id ? 'You win!' : data.name + ' wins.';
  }
});

room.on('reconnecting', () => {
  status.textContent = 'Connection lost. Trying again...';
});

room.on('closed', (reason) => {
  button.disabled = true;
});

button.addEventListener('click', () => {
  room.send('tap', {});
});

Put in your own game ID and API token.

6. Play it

Open the page in two different browsers, or in one normal window and one private window. The guest player is remembered in the browser's localStorage, so two tabs of the same browser are the same player.

Both windows show "Waiting for another player..." and then "Go!" once the second one joins.

7. Read the logs

Anything your room code passes to room.log shows in the dashboard and here:

gamefuse multiplayer logs --follow

Next