Multiplayer Game Servers
Rivet provides a robust platform for deploying, scaling, and managing game servers globally.
Quickstart
In this guide, we'll implement a simple game server on Rivet and create a backend API that manages server instances. This tutorial assumes you already have your own API server that will handle game logic and player management.
Before starting, you'll need to choose a runtime for your game server. Rivet offers two options:
- Actors: For lightweight, fast-starting game servers using JavaScript/TypeScript
- Containers: For maximum flexibility, supports any language, ideal for complex game servers
Step 1: Preparing your game server code
We'll package your existing game server to run on Rivet. Depending on your chosen runtime, you'll need to create different files:
Determining port to listen on
Your existing game server code (server.js
) should read environment variables for port configuration. Read more here.
Step 2: Creating a Rivet configuration
Create a minimal rivet.json
configuration file that tells Rivet how to deploy your game server:
Step 3: Deploying your game server
Install the Rivet CLI here. Then deploy your game server to Rivet using the CLI:
This will not create a server
This uploads your game server code to Rivet but doesn't start any instances yet. Your code is now available to be launched on-demand.
Step 4 (optional): Starting game server instances with a backend API
In your backend API, add code to start game server instances when needed.
It's up to you when you choose to call createGameServer
. Read more about different scaling patterns under Scaling Methods.
Only call Rivet API from your backend
The Rivet API requires a private Service Token and should only be called from your backend. Do not make this service token public.
Step 5 (optional): Connecting players to your game server
When players need to join a game, your backend API provides the WebSocket connection URL:
Caching server URLs
We recommend storing the connection URL returned from actors.create
instead of calling actors.list
for every API call.
Step 6 (optional): Destroying servers once finished
When a game finishes, clean up the server to avoid unnecessary costs:
Global Regions
Rivet's global edge network allows you to deploy game servers in multiple regions around the world, ensuring low latency for players regardless of their location.
Available Regions
Rivet offers server deployments across multiple geographic regions. See the list of available regions here.
To fetch the available regions dynamically, use:
Region Selection
You can also use the recommendation API from the client to get the recommended region based on the player's IP:
Scaling Methods
Choose the scaling approach that best fits your game's architecture and player patterns:
- Static Server Fleet: Best for games with predictable player counts and consistent traffic
- Dynamic Load-Based: Ideal for games with variable player counts throughout the day
- On-Demand Lobby Creation: Perfect for session-based games where matches have distinct lifetimes
- Custom Game Lobbies: Suited for games where players create rooms with specific settings
Static Server Fleet
This approach maintains a predetermined number of game servers running in each region. It uses actors.list
to check for existing servers and automatically creates or destroys servers to maintain the desired count.
- Ensures a consistent number of servers are available in each region
- Servers are durable, meaning they automatically restart if they crash
- Monitor these servers in the Rivet dashboard
Example
Use this script to maintain a fixed number of servers across specified regions:
To run this with the credentials auto-populated, use:
This script will output a list of server connection URLs that you can copy & paste in your game's frontend to show a server list.
Dynamic Load-Based Scaling
Scale your server fleet up or down based on demand from your backend.
- Periodically check metrics (player count, server load) from your running servers
- Call
actors.create
to add servers when needed - Call
actors.destroy
to remove underutilized servers - Implement custom scaling logic based on your game's patterns
Example: Periodic scaling with setInterval
In your own backend:
On-Demand Lobby Creation
Create game servers on-demand as players request to join lobbies.
- Your lobby management service maintains a state of available lobbies
- When a player requests to join, check for available space in existing lobbies
- If no space is available, create a new server instance
- Clean up servers once they're empty
Key Endpoints:
-
Request to join lobby (called by client)
- Check if there is space in existing lobbies
- If not, create a new actor (handle race conditions appropriately)
- Return connection information to the client
-
Player disconnected (called by lobby)
- Remove player from lobby tracking
- Destroy lobby if empty after a grace period
-
Heartbeat/watchdog
- Implement timeout mechanisms for players who connect but never join
- Clean up abandoned servers to prevent resource waste
Example: On-demand lobby system with Hono
Custom Game Lobbies
Create customized game servers on-demand with specific configurations.
Notice
Always implement lobby creation in your trusted backend, never in client code.
- Create actors with specific configurations via environment variables
- Customize CPU and memory resources for demanding game modes
- Use tags for organizing and querying actors with
actors.list
- Filter and monitor lobbies in the dashboard
Example: Custom lobby creation with Hono
Upgrading Servers
Choose the upgrade approach that best fits your game's requirements:
- Default Behavior: Best for development or games that can tolerate brief interruptions
- Targeted Upgrading: Ideal for testing new versions on a subset of servers before full rollout
- Zero-Downtime Rolling: Essential for production games where player sessions must be preserved
Default Behavior
When you run rivet deploy
, your game server code is uploaded and all running durable actors are automatically upgraded:
- When deploying a new version, actors receive a SIGTERM signal
- They have a 30-second grace period to clean up and shutdown
- New actors start automatically using the updated code
- This is the simplest approach but will disconnect active players
Targeted Server Upgrading
If you want more control over upgrading your servers, you can use targeted upgrades to selectively update specific servers:
- Call
actors.upgrade
on specific actors - Useful for testing updates on a subset of servers
- Allows controlled rollout of new versions
- Can target empty or low-population servers first
- Example: Upgrade only empty servers first
- Validate new version behavior before full rollout
Example: Manual selective upgrading
Zero-Downtime Rolling Upgrades With Draining
For production games, it's highly recommended to implement a system for routing new players to updated servers while allowing existing sessions to complete naturally:
- Implement custom logic to gradually upgrade servers
- Start sending new players to new server versions
- Wait for old servers to naturally empty out as players finish their sessions
- This approach preserves gameplay sessions on existing servers
- Requires more complex implementation but provides the best player experience
Server Configuration Options
When creating game servers with actors.create
, you can configure:
-
Network: Define HTTPS/WSS ports, custom paths, and routing options
TypeScript -
Resources: Customize CPU and memory allocation
TypeScript -
Environment Variables: Configure server behavior via
process.env
TypeScript -
Tags: Add metadata for filtering and organization
TypeScript -
Lifecycle: Set up durability and idle timeouts
TypeScript -
Build Selection: Target specific versions of your server code
TypeScript -
Region Selection: Deploy to specific regions for lower latency
TypeScript
For detailed documentation, see:
Learning More
For more comprehensive coverage of game server development with Rivet:
- Rivet API Docs - Complete API reference for direct Rivet integration
- Local Development - Setting up your local environment for development
- Troubleshooting - Common issues and their solutions