⏱️ 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

bash 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}`);

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 });
	});
});

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.

Was this page helpful?

Edit Page

Rivet

Open-source multiplayer infrastructure. Easy, flexible, and affordable.

This website is not sponsored by or affiliated with Unity Technologies or its affiliates. Unity Trademark(s) are trademark(s) or registered trademark(s) of Unity Technologies or its affiliates in the U.S. and elsewhere. | This website is not sponsored by, affiliated with, or endorsed by Epic Games, Inc. or its affiliates. 'Unreal Engine' is a trademark or registered trademark of Epic Games, Inc. in the U.S. and elsewhere. | The HTML5 Logo by the World Wide Web Consortium (W3C), used under a Creative Commons Attribution 3.0 License. Source | The Godot Engine Logo by the Andrea Calabró, used under a Creative Commons Attribution 4.0 International License. Source | Docker and the Docker logo are trademarks or registered trademarks of Docker, Inc. in the United States and/or other countries. Docker, Inc. and other parties may also have trademark rights in other terms used herein.

© 2024 Rivet Gaming, Inc. All rights reserved.