Clients

Clients

Clients are used to get and communicate with actors from your application. Clients can be created from either your frontend or backend.

Creating a Client

Getting an Actor

getOrCreate

Returns a handle to an existing actor or creates one if it doesn’t exist:

Pass initialization data when creating:

get

Returns a handle to an existing actor or null if it doesn’t exist:

create

Creates a new actor, failing if one already exists with that key:

getForId

Connect to an actor using its internal ID:

Prefer using keys over internal IDs for simplicity.

Calling Actions

In JavaScript, actions called without connect() are stateless. Each call is independent without a persistent connection. In React, useActor automatically manages a persistent connection.

Connecting to an Actor

For real-time use cases, establish a persistent connection to the actor:

Subscribing to Events

Listen for events from connected actors:

Full-Stack Type Safety

Import types from your registry for end-to-end type safety:

Use import type to avoid accidentally bundling backend code in your frontend.

Advanced

Disposing Clients & Connections

Dispose clients to close all connections:

await client.dispose();
TypeScript

Dispose individual connections when finished:

const conn = actor.connect();

try {
  conn.on("event", handler);
  await conn.action();
} finally {
  await conn.dispose();
}
TypeScript

When using useActor in React, connections are automatically disposed when the component unmounts. No manual cleanup is required.

Connection Parameters

Pass custom data to the actor when connecting:

Authentication

Pass authentication tokens when connecting:

See authentication for more details.

Error Handling

See errors for more details.

Actor Resolution

get and getOrCreate return immediately without making a network request. The actor is resolved lazily when you call an action or connect().

To explicitly resolve an actor and get its ID, use resolve():

const handle = client.counter.getOrCreate("my-counter");
const actorId = await handle.resolve();
console.log(actorId); // "lrysjam017rhxofttna2x5nzjml610"
TypeScript

API Reference