Rivet Actors
Rivet Actors allows you to deploy resilient, stateful services that maintain their state between requests. Use them for websocket servers, game backends, real-time collaboration services, and more.
Get Started Faster With ActorCore
For getting started quickly using JavaScript, we recommend trying ActorCore – our full-stack framework for working with Rivet Actors.
What are actors good for?
- Stateful Services: Applications where maintaining state across interactions is critical. For example, Collaborative Apps with shared editing and automatic persistence.
- Realtime Systems: Applications requiring fast, in-memory state modifications or push updates to connected clients. For example, Multiplayer Games with game rooms and player state.
- Long-Running Processes: Tasks that execute over extended periods or in multiple steps. For example, AI Agents with ongoing conversations and stateful tool calls.
- Durability: Processes that must survive crashes and restarts without data loss. For example, Durable Execution workflows that continue after system restarts.
- Horizontal Scalability: Systems that need to scale by distributing load across many instances. For example, Realtime Stream Processing for stateful event handling.
- Local-First Architecture: Systems that synchronize state between offline clients. For example, Local-First Sync between devices.
Quickstart
Step 1: Writing an actor
Add the @rivet-gg/actor
package to your project for comprehensive TypeScript definitions:
This package is optional
You can use Rivet actors without this package, but you'll need to define your own types.
Every actor must export a default object with an async start
function. Here's a simple HTTP server example:
What this code does:
- Creates a simple HTTP server using Hono with Deno
- Sets up a route that returns the actor ID and region information
- Keeps the actor running indefinitely by returning a promise that never resolves
Using classes
You can also use a class with a static method for your entrypoint:
Step 2: Deploying an actor
Specify the script in your rivet.json
:
Now deploy your actor with:
Step 3: Starting an actor
In this step, you're requesting Rivet to launch your actor code in the cloud:
What happens during creation:
- Rivet finds the latest build matching your
buildTags
- It provisions resources in the specified region (or chooses the best one)
- It starts your actor code with the provided environment variables
- The actor starts running and initializes based on your code's
start
function
See actors.create for more options.
Step 4: Connecting to an actor
Once your actor is running, you can access its URL directly from the actor object:
What happens during connection:
- Each port configured for your actor gets a unique URL
- These URLs are accessible based on your actor's security settings
- The URL routes to your actor regardless of which region it's in
- For additional security, you can use
getConnection
to generate temporary, authenticated URLs
See actors.get for more details.
Step 5: Destroying an actor
When you're finished using the actor, it's important to destroy it to free up resources:
What happens during destruction:
- Rivet sends a termination signal to your actor
- Your actor gets a short grace period to clean up resources
- All compute resources associated with the actor are freed
- You stop being billed for the actor's runtime
See actors.destroy for more details.
Note
Always destroy actors when you're done with them to avoid unnecessary costs. Actors will continue running and billing will continue until explicitly destroyed.
Configuration
See the configuration documentation for all available options.
The ActorContext
Object
Your start
function receives an ActorContext
object with important metadata and services: