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.
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.