Connect a client to a session
When the frontend needs to join a live session, receive messages from the actor, and send messages to the actor, connect a client to an actor session. To connect a client to a session:- Call
base44.actors.ActorName(sessionId)to select the session. - Call
.connect()to open the WebSocket connection. - Call
conn.subscribe()to register a listener for messages from the actor. - Call
conn.send()to send a message to the actor.
chatRoom actor session, listens for messages, and sends a message:
Run ticks
When work needs to repeat on an interval while clients are in the session, run ticks. Examples include broadcasting game state or updating a round timer. To run ticks:- Set
tickIntervalMsto the interval length in milliseconds. - Implement
shouldTick()to returntrueto keep ticking andfalseto stop. - In
handleTick(), run the work for each interval.
Schedule wakes
When work needs to run once at a specific time, whether the session is active or idle, schedule a wake. Examples include ending a turn after 30 seconds or closing an empty lobby. To schedule a wake:- Call
schedule()with a key and a time to arm the wake. - In
handleWake(), check the key and run the work for that wake. - When the wake no longer applies, call
cancelSchedule()with the key.
Manage client connections
When you need to decide who can join a session, reply to a specific client, and reach every client in the session, manage client connections. To manage client connections:- In
handleConnect(), checkconn.identityand callconn.reject()to refuse a client. - Call
conn.send()to reply to only that client. - In
handleMessage(), callbroadcast()to reach every connected client. To identify the sender, include fields fromconnin the message.
Persist session data
When values need to survive an idle period, persist session data. Examples include a board, a history list, or the current turn. To persist session data:- In
handleStart(), load values fromthis.storageinto class fields. - After each change, call
this.storage.put()to save the updated value. - To reset the session, call
this.storage.delete()orthis.storage.deleteAll().
Write entity data from a session
When the session needs to create or update app records, write entity data. Examples include a final score or a completed order. To write entity data from a session:- Validate the incoming message before writing.
- Read the user ID from
conn.identity.userId. - Use
this.client.asServiceRolefor elevated access. Write with a stable key, such as the session ID, so a repeated handler updates the same record.
Call a backend function for secret work
Actors can read app secrets withsecrets.get(). When a handler needs work that uses a secret, such as calling an external API with an API key, we recommend calling a backend function. The function reads the secret and returns the result, never the secret.
To call a backend function from an actor:
- Create a backend function that reads the secret with
secrets.get(), does the work, and returns the result. - In a handler, call
this.client.functions.invoke()with the function name and the data to send. - Use the result in your session, such as broadcasting it to connected clients.
See also
- Actors Overview: Concepts and terminology
- Create an Actor: Actor files and a minimal class
- Actors Files and Code: Complete backend class API
actors deploy: Deploy local actors