You can configure your gameβs Rivet config in the created rivet.yaml file. For the purposes of this demo,
you can copy and paste this config into your file:
rivet.yaml
Copy
scripts: start: npm run start# How the game lobbies run and how players connect to the game.## https://docs.rivet.gg/matchmaker/introductionmatchmaker: # How many players can join a specific lobby. # # Read more about matchmaking: https://docs.rivet.gg/matchmaker/concepts/finding-lobby max_players: 32 # The hardware to provide for lobbies. # # Available tiers: https://docs.rivet.gg/serverless-lobbies/concepts/available-tiers tier: basic-1d1# Which regions the game should be available in.## Available regions: https://docs.rivet.gg/serverless-lobbies/concepts/available-regionsmatchmaker: regions: atl: {} fra: {} # Runtime configuration for the lobby's Docker container. docker: # If you're unfamiliar with Docker, here's how to write your own # Dockerfile: # https://docker-curriculum.com/#dockerfile dockerfile: Dockerfile # Which ports to allow players to connect to. Multiple ports can be defined # with different protocols. # # How ports work: https://docs.rivet.gg/serverless-lobbies/concepts/ports ports: default: port: 3000 # What game modes are available. # # Properties like `max_players`, `tier`, `dockerfile`, `regions`, and more can # be overridden for specific game modes. game_modes: default: {}# How Rivet CDN should host your static assets on our CDN.## https://docs.rivet.gg/cdn/introductioncdn: # Command to run before uploading the site to Rivet. This can be used to # build any JavaScript bundles or generate assets. build_command: npm install && npm run build:client:prod # The folder to upload to Rivet. # # If you're hosting a website, ensure that `index.html` is in the root of # this folder. build_output: ./dist/kv: {}identity: {}
import { RivetClient } from '@rivet-gg/api';const RIVET = new RivetClient({ token: process.env.RIVET_TOKEN });
Find the connect function in client/Client.ts and replace it with the following:
client/Client.ts
Copy
async function connect(client: Client) { // Find a lobby to connect to on Rivet let res = await RIVET.matchmaker.lobbies.find({ gameModes: ['default'] }); let port = res.ports['default']; // Open a new connection to the lobby client.connection = new Connection(client, port.isTls, port.host, { token: res.player.token });}
import { RivetClient } from '@rivet-gg/api';const RIVET = new RivetClient({ token: process.env.RIVET_TOKEN });// Notify Rivet that this lobby is ready to accept playersRIVET.matchmaker.lobbies.ready();
Find the setupConnection function in server/index.ts and replace it with the following:
server/index.ts
Copy
async function setupConnection(socket: Socket) { // Read the token passed to the socket query let playerToken = socket.handshake.query.token as string; // Validate the player token with the matchmaker await RIVET.matchmaker.players.connected({ playerToken }); // Remove the player when disconnected socket.on('disconnect', () => RIVET.matchmaker.players.disconnected({ playerToken })); new Connection(game, socket);}
Finally, after stopping the previous npm run start, run rivet run start and validate the game still connects.
What is "rivet run"?
The rivet run command automatically adds important environment variables required for running a game to
whatever script you choose to run with it. In this case, it is reading the start script defined in
rivet.yaml.
Optional: Check for connectivity to Rivet
Press Ctrl + Shift + I (or Cmd + Option + I on macOS) to open the dev tools.
Navigate to the βNetworkβ tab.
Refresh your page.
Find a POST request to https://api.rivet.gg/matchmaker/lobbies/find. This means you are
successfully connected through Rivet.
Now that you managed to get the game running locally, you can deploy your game to Rivet with:
Copy
rivet deploy prod
What does this do?
Builds & uploads your Docker image for Rivet Dynamic Servers - Builds &
uploads your site for Rivet CDN - Creates a version on Rivet and deploys the version to the
Production namespace
The CLI will print a link ending in rivet.game. Share the link with a friend to play your game on Rivet! π