JavaScript Runtime
The Rivet JavaScript runtime is built on lightweight JavaScript containers called V8 isolates, providing a high-performance, secure environment for your actor code.
It's designed to be widely compatible with Node.js and NPM dependencies, making it easy to use familiar libraries and tools.
This guide is for advanced usage of Rivet
For getting started quickly, we recommend using ActorCore.
Basic Setup
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:
- Sets up a simple HTTP server using Node.js's built-in http module
- Creates a response that includes 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: