website/src/content/docs/actors/actions.mdx
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.
By default, actions run in parallel. If you need advanced control over concurrency, use queues.
Actions are defined in the actions object when creating an actor:
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.
Actions can be called in different ways depending on your use case:
<Tabs> <Tab title="Frontend (createClient)"> <CodeSnippet file="examples/docs/actors-actions/calling-frontend.ts" title="frontend.ts" />Learn more about communicating with actors from the frontend.
</Tab> <Tab title="Backend (registry.handler)"> <CodeSnippet file="examples/docs/actors-actions/calling-backend.ts" title="server.ts" />Learn more about communicating with actors from the backend.
</Tab> <Tab title="Actor-to-Actor (c.client())"> <CodeSnippet file="examples/docs/actors-actions/calling-actor.ts" title="actor.ts" />Learn more about communicating between actors.
</Tab> </Tabs> <Note> Calling actions from the client are async and require an `await`, even if the action itself is not async. </Note>The actor client includes type safety out of the box. When you use createClient<typeof registry>(), TypeScript automatically infers action parameter and return types:
Actors provide robust error handling out of the box for actions.
UserError can be used to return rich error data to the client. You can provide:
For example:
<CodeGroup> <CodeSnippet file="examples/docs/actors-actions/user-error-actor.ts" title="actor.ts" /> <CodeSnippet file="examples/docs/actors-actions/user-error-client.ts" title="client.ts" /> </CodeGroup>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.
If passing data to an actor from the frontend, use a library like Zod to validate input data.
For example, to validate action parameters:
<CodeSnippet file="examples/docs/actors-actions/schema-validation.ts" title="actor.ts" />Actions have a single return value. To stream realtime data in response to an action, use events.
For operations that should be cancelable on-demand, create your own AbortController. Chain it with c.abortSignal so actor shutdown also cancels the operation.
See Actor Shutdown Abort Signal for automatically canceling operations when the actor stops.
ActionContext ExternallyWhen 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:
See types for more details on using ActionContextOf and other utility types.
GET /inspector/rpcs lists all available actions on an actor.POST /inspector/action/:name executes an action with JSON args and returns output.Actions - Interface for defining actionsActionContext - Context available in action handlersActorDefinition - Interface for defining actors with actionsActorHandle - Handle for calling actions from clientActorActionFunction - Type for action functions