⏱️ Crash Course
Step 1: Setup project
Create a game on the Rivet Dashboard
Visit the Rivet Dashboard to create a new game.
Install the Rivet CLI
Follow instructions here.
Initialize the project
Open your project's directory in a terminal and run:
rivet init
This will guide you through setting up your project with Rivet.
Write a Dockerfile
for your server
You'll need a Dockerfile
to build and run your game server.
Step 2: Integrate Rivet Matchmaker
Install the Rivet library
npm install --save @rivet-gg/api
Update your client
Copy the following pseudocode into the appropriate places in your source:
import { RivetClient } from '@rivet-gg/api';
let rivet = new RivetClient({ token: process.env.RIVET_TOKEN });
// Find a lobby (a new lobby will automatically be created on demand if needed)
let res = await rivet.matchmaker.lobbies.find({ gameModes: ['default'] });
// Connect to the lobby (Rivet automatically manages your SSL)
let ws = new WebSocket(`wss://${res.ports['default'].host}/?token=${res.player.token}`);
In development, RIVET_TOKEN
will use the development token generated by rivet run
. In production, RIVET_TOKEN
will be automatically added by the CDN.
More info:
Update your server
Copy the following pseudocode into the appropriate places in your source:
// `RIVET_TOKEN` is automatically added to the environment by Rivet.
import { RivetClient } from "@rivet-gg/api";
let rivet = new RivetClient({ token: process.env.RIVET_TOKEN });
// Tell Rivet your server is ready. This gives you time to load maps and
// other data before accepting players.
rivet.matchmaker.lobbies.ready().catch(() => process.exit(1));
myGameServer.on("connect", await (socket) => {
let playerToken = socket.request.query.token;
// Notify the matchmaker that the player connected. This can be
// called only once per player token. This will throw an error and
// close the socket if the player token is already used or invalid.
await rivet.matchmaker.players.connected({ playerToken }).catch(err => socket.close());
socket.on("close", async () => {
// Notify the matchmaker that the player disconnected.
await rivet.matchmaker.players.disconnected({ playerToken });
});
});
In development, RIVET_TOKEN
will use the development token generated by rivet run
. In production, RIVET_TOKEN
is automatically added to your environment by Rivet.
More info:
Step 3: Publish your game
# Deploy to the production namespace
rivet deploy prod
This will print a URL ending in rivet.game
. If you are hosting your game on Rivet CDN, visit that URL to play your game.