Interacting with Actors
This guide covers how to connect to and interact with actors from client applications.
Setting Up the Client
The first step is to create a client that will connect to your actor service:
Finding & Connecting to Actors
ActorCore provides several methods to connect to actors:
get(tags, opts)
- Find or Create
The most common way to connect is with get()
, which finds an existing actor matching the provided tags or creates a new one:
create(opts)
- Explicitly Create New
When you specifically want to create a new actor instance:
getWithId(id, opts)
- Connect by ID
Connect to an actor using its internal ID:
It’s usually better to use tags for discovery rather than directly using actor IDs.
Calling Actions
Once connected, calling actor actions are straightforward:
All actor action calls are asynchronous and require await
, even if the actor’s action is not async.
Listening for Events
Actors can send realtime updates to clients using events:
on(eventName, callback)
- Continuous Listening
To listen for events that will happen repeatedly:
once(eventName, callback)
- One-time Listening
For events you only need to hear once:
Connection Options
Authentication and Connection Parameters
Pass information with your connection using the params
option:
The actor can access these parameters in the onBeforeConnect
or createConnState
hook:
Read more about connection parameters.
Additional Options
opts.noCreate
Connect only if an actor exists, without creating a new one:
Client Options
encoding
"cbor" | "json"
(optional)
Specifies the data encoding format used for communication:
"cbor"
(default): Binary format that’s more efficient for data transfer"json"
: Text-based format with wider compatibility across environments
supportedTransports
("websocket" | "sse")[]
(optional)
Configures which network transport mechanisms the client will use to communicate with actors, sorted by priority:
"websocket"
: Real-time bidirectional communication, best for most applications"sse"
(Server-Sent Events): Works in more restricted environments where WebSockets may be blocked
Default is ["websocket", "sse"]
, which automatically negotiates the best available option.
Error Handling
ActorCore provides specific error types to help you handle different failure scenarios:
Action Errors
When an action fails, it throws an error with details about the failure:
These errors can be thrown from within the actor with UserError
:
ActorCore doesn’t expose internal errors to clients for security, helping to prevent the exposure of sensitive information or internal implementation details.
Other Errors
Other common errors you might encounter:
InternalError
: Error from your actor that’s not a subclass ofUserError
ManagerError
: Issues when connecting to or communicating with the actor managerNoSupportedTransport
: When the client and server have no compatible transport
Disconnecting and Cleanup
The client connection is automatically cleaned up when it goes out of scope.
If you need to explicitly disconnect:
Offline and Auto-Reconnection
Clients automatically attempt to reconnect (with exponential backoff) when disconnected. Actions made while disconnected are queued.
On reconnection, event subscriptions are reestablished & queued actions are executed.
This makes your applications resilient to temporary network failures without any extra code.