Networking
Rivet provides a simple yet powerful networking system for your actors. When you create an actor, you define the ports it will use, and Rivet handles the networking infrastructure, including URL generation, port mapping, and traffic routing.
Configuring Networking
When creating an actor, you specify the ports that your application needs to expose. This is done through the network.ports
parameter when creating your actor with the actor.create API:
Once your actor is running with properly configured ports, you can access it through the URLs provided by Rivet. These URLs are available:
- Via the API in the actor creation response under
actor.network.ports.http.url
- In the Rivet Hub dashboard for your actor
Listening For Requests On The Actor
Once your actor is running, Rivet injects environment variables containing information about the assigned ports. Your application should use these variables to determine what port to listen on.
For example:
Environment Variable Naming
For each port defined in your actor configuration under network.ports
, Rivet sets a single environment variable PORT_{NAME}
where {NAME}
is the uppercase version of the key you used in the network.ports
object.
Your application should read these environment variables to know which ports to bind to.
For example:
network.ports.http
becomes thePORT_HTTP
environment variablenetwork.ports.game
becomes thePORT_GAME
environment variablenetwork.ports["hello-world"]
becomes thePORT_HELLO_WORLD
environment variable
Examples
Here are complete examples demonstrating how to set up networking for both the client and server.
HTTP Server Example
Client (Creating the Actor)
Actor (Starting the Server)
TCP Game Server Example
Client (Creating the Actor)
Server (Starting the Server)
Supported Protocols
Rivet supports the following protocols for actor ports:
Protocol | Description | Recommendation |
---|---|---|
https | HTTPS and secure WebSocket traffic | Recommended for web applications |
http | Insecure HTTP or WebSocket traffic | Not recommended (use https instead) |
tcp_tls | Secure TCP sockets | Recommended for TCP connections |
tcp | TCP sockets | Not recommended (use tcp_tls instead) |
udp | UDP sockets | Use for real-time applications requiring UDP |
SSL and TLS
When you use the https
or tcp_tls
protocols, Rivet automatically handles all SSL/TLS encryption for you.
Your server should listen for regular unencrypted HTTP or TCP traffic on the assigned port.
Troubleshooting
Bad Gateway (502)
If you receive a "Bad Gateway" (502) error when accessing your actor, it typically means:
- Your actor is listening on the wrong port (check the environment variables)
- Your actor has crashed during request handling
Resolution: Check the actor logs in the Rivet Hub dashboard to see if your server started correctly. Verify that your application is listening on the port specified by the PORT_{NAME}
environment variable, not a hardcoded port.
Not Found (404)
A "Not Found" (404) error usually indicates:
- Your actor has crashed before receiving the request
- The URL you're using is incorrect
- There's a bug in your application's routing
Resolution: Manually validate the URL shown for the actor in the Rivet Hub dashboard. Check the actor logs to ensure your application started successfully and is handling requests properly.