Skip to main content
You’re viewing developer documentation
This documentation is for developers working with the Base44 developer platform.
Use these sample flows as a starting point for your implementation.

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:
  1. Call base44.actors.ActorName(sessionId) to select the session.
  2. Call .connect() to open the WebSocket connection.
  3. Call conn.subscribe() to register a listener for messages from the actor.
  4. Call conn.send() to send a message to the actor.
The following example connects to a chatRoom actor session, listens for messages, and sends a message:
To learn more about connections, see connections.

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:
  1. Set tickIntervalMs to the interval length in milliseconds.
  2. Implement shouldTick() to return true to keep ticking and false to stop.
  3. In handleTick(), run the work for each interval.
The following example ticks every second while at least two clients are in the session, broadcasting a countdown each tick:
To learn more about ticks, see timer handlers and settings.

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:
  1. Call schedule() with a key and a time to arm the wake.
  2. In handleWake(), check the key and run the work for that wake.
  3. When the wake no longer applies, call cancelSchedule() with the key.
The following example schedules a turn timeout when a turn starts and broadcasts a message when the timeout triggers:
To learn more about wakes, see timer handlers and settings.

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:
  1. In handleConnect(), check conn.identity and call conn.reject() to refuse a client.
  2. Call conn.send() to reply to only that client.
  3. In handleMessage(), call broadcast() to reach every connected client. To identify the sender, include fields from conn in the message.
The following example rejects anonymous clients, sends history to the connecting client, and broadcasts messages with the sender’s user ID:
To learn more about connections, see connections.

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:
  1. In handleStart(), load values from this.storage into class fields.
  2. After each change, call this.storage.put() to save the updated value.
  3. To reset the session, call this.storage.delete() or this.storage.deleteAll().
The following example loads a board on start, saves it after each move, and clears storage when the last client disconnects:
To learn more about storage, see storage.

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:
  1. Validate the incoming message before writing.
  2. Read the user ID from conn.identity.userId.
  3. Use this.client.asServiceRole for elevated access. Write with a stable key, such as the session ID, so a repeated handler updates the same record.
The following example saves a final score for the authenticated client. A later save for the same session updates that record:
To learn more about entity access, see entity access.

Call a backend function for secret work

Actors can read app secrets with secrets.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:
  1. Create a backend function that reads the secret with secrets.get(), does the work, and returns the result.
  2. In a handler, call this.client.functions.invoke() with the function name and the data to send.
  3. Use the result in your session, such as broadcasting it to connected clients.
The following example shows a backend function that reads an API key and returns a translation:
The following example calls that function from an actor and broadcasts the result:

See also