Communicating with Actors
Learn how to call actions and connect to actors from client applications
This guide covers how to connect to and interact with actors from client applications using Rivet's JavaScript/TypeScript client library.
Client Setup
Creating a Client
There are several ways to create a client for communicating with actors:
For frontend applications or external services connecting to your Rivet backend:
This client communicates over HTTP/WebSocket and requires authentication.
Client Configuration
Configure the client with additional options:
Actor Handles
get(tags, opts?)
- Find Existing Actor
Returns a handle to an existing actor or null
if it doesn't exist:
getOrCreate(tags, input?, opts?)
- Find or Create Actor
Returns a handle to an existing actor or creates a new one if it doesn't exist:
Note
get()
and getOrCreate()
are synchronous and return immediately. The actor is created lazily when you first call an action.
create(tags, input?, opts?)
- Create New Actor
Explicitly creates a new actor instance, failing if one already exists:
getWithId(id, opts?)
- Find by Internal ID
Connect to an actor using its internal system ID:
Notice
Prefer using tags over internal IDs for actor discovery. IDs are primarily for debugging and advanced use cases.
Actions
Calling Actions
Once you have an actor handle, call actions directly. All action calls are async:
Action Parameters
Actions receive parameters exactly as defined in the actor:
Error Handling
Handle action errors appropriately:
Real-time Connections
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.
connect(params?)
- Establish Stateful Connection
For real-time communication with events, use .connect()
:
Events
on(eventName, callback)
- Listen for Events
Listen for events from the actor:
once(eventName, callback)
- Listen Once
Listen for an event only once:
off(eventName, callback?)
- Stop Listening
Remove event listeners:
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
Transports
Connections automatically negotiate the best available transport:
WebSocket Transport
- Full duplex: Client can send and receive
- Low latency: Immediate bidirectional communication
- Preferred: Used when available
Server-Sent Events (SSE)
- Server-to-client: Events only, actions via HTTP
- Fallback: Used when WebSocket unavailable
- Compatibility: Works in restrictive environments
Reconnections
Connections automatically handle network failures with robust reconnection logic:
Automatic Behavior:
- 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
- State synchronization: Connection state is preserved and synchronized after reconnection
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
Actions vs Connections
Use Stateless Actions For:
- Simple request-response operations
- One-off operations
- Server-side integration
- Minimal overhead required
Use Stateful Connections For:
- Real-time updates needed
- Multiple related operations
- Event-driven interactions
- Long-lived client sessions
Resource Management
Always clean up connections when finished:
Error Handling
Implement proper error handling for both actions and connections:
Performance Optimization
Use appropriate patterns for optimal performance: