Concepts

Actions

Actions are how your backend, frontend, or other actors can communicate with actors. Actions are defined as functions in the actor configuration and can be called from clients.

Actions are very lightweight. They can be called thousands of times per second safely.

Actions are executed via HTTP requests or via WebSockets if using .connect().

For advanced use cases that require direct access to HTTP requests or WebSocket connections, see raw HTTP and WebSocket handling.

Writing Actions

Actions are defined in the actions object when creating an actor:

import { actor } from "rivetkit";

const mathUtils = actor({
  state: {},
  actions: {
    // This is an action
    multiplyByTwo: (c, x) => {
      return x * 2;
    }
  }
});
TypeScript

Each action receives a context object (commonly named c) as its first parameter, which provides access to state, connections, and other utilities. Additional parameters follow after that.

Calling Actions

Actions can be called in different ways depending on your use case:

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

const client = createClient<typeof registry>("http://localhost:8080");
const counter = await client.counter.getOrCreate();
const result = await counter.increment(42);
console.log(result); // The value returned by the action
frontend.ts

Learn more about communicating with actors from the frontend.

Calling actions from the client are async and require an await, even if the action itself is not async.

Type Safety

The actor client includes type safety out of the box. When you use createClient<typeof registry>(), TypeScript automatically infers action parameter and return types:

import { setup } from "rivetkit";

// Create simple counter
const counter = actor({
  state: { count: 0 },
  actions: {
    increment: (c, count: number) => {
      c.state.count += count;
      return c.state.count;
    }
  }
});

// Create and export the app
const registry = setup({
  use: { counter }
});

Error Handling

Actors provide robust error handling out of the box for actions.

User Errors

UserError can be used to return rich error data to the client. You can provide:

  • A human-readable message
  • A machine-readable code that's useful for matching errors in a try-catch (optional)
  • A metadata object for providing richer error context (optional)

For example:

import { actor, UserError } from "rivetkit";

const user = actor({
  state: { username: "" },
  actions: {
    updateUsername: (c, username: string) => {
      // Validate username
      if (username.length > 32) {
        // Throw a simple error with a message
        throw new UserError("Username is too long", {
          code: "username_too_long",
          metadata: {
            maxLength: 32
          }
        });
      }

      // Update username
      c.state.username = username;
    }
  }
});

Internal Errors

All other errors will return an error with the code internal_error to the client. This helps keep your application secure, as errors can sometimes expose sensitive information.

Schema Validation

If passing data to an actor from the frontend, use a library like Zod to validate input data.

For example, to validate action parameters:

import { actor, UserError } from "rivetkit";
import { z } from "zod";

// Define schema for action parameters
const IncrementSchema = z.object({
  count: z.number().int().positive()
});

const counter = actor({
  state: { count: 0 },
  actions: {
    increment: (c, params) => {
      // Validate parameters
      try {
        const { count } = IncrementSchema.parse(params);
        c.state.count += count;
        return c.state.count;
      } catch (err) {
        throw new UserError("Invalid parameters", { 
          code: "invalid_params",
          meta: { errors: err.errors }
        });
      }
    }
  }
});
actor.ts

Streaming Data

Actions have a single return value. To stream realtime data in response to an action, use events.

Canceling Long-Running Actions

For operations that should be cancelable on-demand, create your own AbortController and chain it with c.abortSignal for automatic cleanup on actor shutdown.

import { actor } from "rivetkit";

const chatActor = actor({
  createVars: () => ({ controller: null as AbortController | null }),

  actions: {
    generate: async (c, prompt: string) => {
      const controller = new AbortController();
      c.vars.controller = controller;
      c.abortSignal.addEventListener("abort", () => controller.abort());

      const response = await fetch("https://api.example.com/generate", {
        method: "POST",
        body: JSON.stringify({ prompt }),
        signal: controller.signal
      });

      return await response.json();
    },

    cancel: (c) => {
      c.vars.controller?.abort();
    }
  }
});
TypeScript

See Actor Shutdown Abort Signal for automatically canceling operations when the actor stops.

Using ActionContext Externally

When writing complex logic for actions, you may want to extract parts of your implementation into separate helper functions. When doing this, you'll need a way to properly type the context parameter.

Rivet provides the ActionContextOf utility type for exactly this purpose:

import { actor, ActionContextOf } from "rivetkit";

const counter = actor({
  state: { count: 0 },
  
  actions: {
    increment: (c) => {
      incrementCount(c);
    }
  }
});

// Simple helper function with typed context
function incrementCount(c: ActionContextOf<typeof counter>) {
  c.state.count += 1;
}
TypeScript

See types for more details on using ActionContextOf and other utility types.

API Reference

Suggest changes to this page