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:

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.