Actor Clients
Learn how to call actions and connect to actors from client applications using Rivet's TypeScript client library.
Rivet also supports React and Rust clients.
Using the RivetKit client is completely optional. If you prefer to write your own networking logic, you can either:
- Make HTTP requests directly to the registry (see OpenAPI spec)
- Write your own HTTP endpoints and use the client returned from
registry.runServer
(see below)
Client Setup
There are several ways to create a client for communicating with actors:

From your backend server that hosts the registry:
This client doesn't require authentication.
ActorClient
The ActorClient
provides methods for finding and creating actors. All methods return an ActorHandle
that you can use to call actions or establish connections.
get(key?, opts?)
- Find Existing Actor
Returns a handle to an existing actor or null
if it doesn't exist:
getOrCreate(key?, opts?)
- Find or Create Actor
Returns a handle to an existing actor or creates a new one if it doesn't exist:
get()
and getOrCreate()
are synchronous and return immediately. The actor is created lazily when you first call an action.
create(key?, opts?)
- Create New Actor
Explicitly creates a new actor instance, failing if one already exists:
getForId(id, opts?)
- Find by Internal ID
Connect to an actor using its internal system ID:
Prefer using keys over internal IDs for actor discovery. IDs are primarily for debugging and advanced use cases.
ActorHandle
An ActorHandle
represents a reference to an actor instance and provides methods for calling actions and establishing connections. You get an ActorHandle
from the ActorClient
methods like get()
, getOrCreate()
, and create()
.
Calling Actions
You can call actions directly on an ActorHandle
:
Actions called on an ActorHandle
are stateless - each call is independent and doesn't maintain a persistent connection to the actor.
fetch(input, init?)
- Raw HTTP Requests
Make direct HTTP requests to the actor's onFetch
handler:
See Fetch & WebSocket Handler documentation for more information.
For most use cases, actions provide a higher-level API that's easier to work with than raw HTTP handlers.
websocket(path?, protocols?)
- Raw WebSocket Connections
Create direct WebSocket connections to the actor's onWebSocket
handler:
See Fetch & WebSocket Handler documentation for more information.
For most use cases, actions & events provide a higher-level API that's easier to work with than raw HTTP handlers.
connect(params?)
- Establish Stateful Connection
To open a stateful connection using ActorConn
, call .connect()
:
ActorConn
Real-time connections enable bidirectional communication between clients and actors through persistent connections. Rivet automatically negotiates between WebSocket (preferred for full duplex) and Server-Sent Events (SSE) as a fallback for restrictive environments.
For more information on connections, see the connections documentation. For more information on handling events, see the events documentation.
Calling Actions
You can also call actions through an ActorConn
, just like with an ActorHandle
:
Reconnections
Connections automatically handle network failures with built-in reconnection logic:
- Exponential backoff: Retry delays increase progressively to avoid overwhelming the server
- Action queuing: Actions called while disconnected are queued and sent once reconnected
- Event resubscription: Event listeners are automatically restored on reconnection
on(eventName, callback)
- Listen for Events
Listen for events from the actor:
once(eventName, callback)
- Listen Once
Listen for an event only once:
dispose()
- Clean Up Connection
Always dispose of connections when finished to free up resources:
Important: Disposing a connection:
- Closes the underlying WebSocket or SSE connection
- Removes all event listeners
- Cancels any pending reconnection attempts
- Prevents memory leaks in long-running applications
Authentication
Connection Parameters
Pass authentication data when connecting to actors:
onAuth Hook Validation
Actors can validate authentication using the onAuth
hook:
Learn more about authentication patterns.
Type Safety
Rivet provides end-to-end type safety between clients and actors:
Action Type Safety
TypeScript validates action signatures and return types:
Client Type Safety
Import types from your registry for full type safety:
Best Practices
ActorHandle
(Stateless) vs ActorConn
(Stateful) Clients
Use ActorHandle
(Stateless) For:
- Simple request-response operations
- One-off operations
- Server-side integration
Use ActorConn
(Stateful) Connections For:
- Real-time updates needed
- Event-driven interactions
- Long-lived client sessions
Resource Management
Always clean up connections when finished:
Error Handling
Handle connection errors using the .onError()
method:
Execute Actions in Parallel
You can execute batch requests in parallel:
However, it's recommended to move this logic to run within the actor instead of the client if executing multiple actions is a common pattern.