Quickstart

Node.js & Bun Quickstart

Get started with Rivet Actors in Node.js and Bun

Steps

Install Rivet

npm install rivetkit
Command Line

Create an Actor

Create a simple counter actor:

import { actor, setup } from "rivetkit";

export const counter = actor({
	state: { count: 0 },
	actions: {
		increment: (c, x: number) => {
			c.state.count += x;
			c.broadcast("newCount", c.state.count);
			return c.state.count;
		},
	},
});

export const registry = setup({
	use: { counter },
});
registry.ts

Setup Server

Integrate with your preferred web framework:

import { registry } from "./registry";

// Exposes Rivet API on /api/rivet/ to communicate with actors
export default registry.serve();

Run Server

npx srvx server.ts

Your server is now running. See Server Setup for runtime-specific configurations.

Connect To The Rivet Actor

This code can run either in your frontend or within your backend:

import { createClient } from "rivetkit/client";
import type { registry } from "./registry";

const client = createClient<typeof registry>();

// Get or create a counter actor for the key "my-counter"
const counter = client.counter.getOrCreate("my-counter");

// Call actions
const count = await counter.increment(3);
console.log("New count:", count);

// Get current state
const currentCount = await counter.getCount();
console.log("Current count:", currentCount);

// Listen to realtime events
const connection = counter.connect();
connection.on("countChanged", (newCount) => {
	console.log("Count changed:", newCount);
});

// Increment through connection
await connection.increment(1);
client.ts

See the JavaScript client documentation for more information.

Deploy

By default, Rivet stores actor state on the local file system.

To scale Rivet in production, follow a guide to deploy to your hosting provider of choice:

Configuration Options

  • Server Setup: Different ways to run your server with serve(), handler(), or framework adapters.
  • Clients: Connect to actors from JavaScript, React, or other platforms.
  • Authentication: Secure actor connections with custom authentication logic.
  • CORS: Configure origin restrictions to secure your actors from unauthorized access.
  • Logging: Configure logging output for debugging and monitoring.
  • Runtime Modes: Serverless vs runners for different deployment scenarios.
Suggest changes to this page