- Realtime messaging: Send and receive messages with clients via WebSockets.
- Persistent storage: Keep session state alive between idle periods.
- Scheduled work: Run logic at a set interval or at a specific time.
Actors
Actor is the class you extend in your backend.
The actor class defines the behavior of a live session. With actors, you can:
- Manage connections: Accept or reject a client connecting to the session, and identify who is connecting.
- Handle messages: Receive a message from a client, update session state, and reply to one client or broadcast to all.
- Schedule work: Run logic on an interval or at a scheduled time.
Sessions
A session is a running instance of an actor, identified by the actor name and a session ID you choose.Each session manages its own state, storage, and client connections independently.
Connections
A connection represents a client’s WebSocket to a session. Each client that joins gets a unique connection. An actor uses that connection to identify who is connecting, manage client connections, and send messages. To learn more, see connections. For a sample flow, see manage client connections.Active and idle sessions
A session is always in one of the following states:- Active: The session is live and processing events. A session becomes active when a client connects, when a client messages an idle session, or when a scheduled wake time arrives.
- Idle: The session isn’t processing events. A session goes idle shortly after it finishes its last message and all scheduled work stops. The session maintains its stored data, scheduled times, and open connections until it becomes active again.
Client
Usebase44.actors in the JavaScript SDK to connect a client to an actor session and exchange messages.
The actors SDK supports the following functionality:
- Connect: Open a WebSocket connection to a session.
- Subscribe: Receive messages from the actor.
- Send: Send messages to the actor.
- Close: Close the WebSocket connection to the actor.
How actors and clients interact
The following diagram shows the general interaction between a client and an actor:- The client connects to the actor over a WebSocket.
- The session activates.
handleStart()runs on first start and each time the session activates from idle. - The actor handles the connection. You can check the client’s identity and reject the connection, or send the current state.
- The client sends a message.
- The actor handles the message. You can validate the format and the sender’s permissions before you change session state.
- The actor broadcasts the update to every connected client, or replies only to the sender.
- The client closes the connection.
- The actor handles the disconnect. You can remove per-connection state.
Realtime messages
Each connected client belongs to a session and can send messages in realtime via WebSockets. Clients can also listen for incoming messages usingsubscribe().
Base44 provides several realtime features that use a subscription-based listener:
- Actor
subscribe()listens for messages the actor sends to connected clients. - Entity
subscribe()listens for changes to app entities.
- Reply to a specific client.
- Broadcast to all connected clients.
- List the clients that are currently connected.
- Identify each client and check whether the client has logged in.
Message types
A client and an actor exchange messages over a connection. Messages fall into the following types:- Incoming messages: What a connected client sends to the actor, such as a text, a move, or a vote. The client sends them with
send(), and the actor receives them inhandleMessage(). - Outgoing messages: What the actor sends to connected clients, such as a reply to a client, a broadcast to all connected clients, or the current board when someone joins. The actor sends them with
conn.send()orbroadcast(), and the client receives them in asubscribe()callback.
- Backend: Pass
IncomingandOutgoingtypes as generics to theActorclass. For a complete example, see create an actor. - Client: Extend the
ActorRegistryinterface with the same shapes, so TypeScript checks the types ofsend()andsubscribe().
Storage
Actors provide several storage options depending on how your session uses and retains data:- In-memory storage: Working state that lives in class fields while the session is active and resets when the session goes idle.
- Persistent storage: A key-value store for data that needs to survive when the session goes idle.
In-memory storage
Class fields hold the working state while the session is active, such as the current board or a round timer. Use in-memory storage when your data changes frequently and doesn’t need to persist when the session goes idle, such as a live score or a cursor position. When a session becomes idle, class fields reset.Persistent storage
Each session has its own key-value store that you can fully manage. Use persistent storage when your data needs to survive when the session goes idle, such as chat history or a game board. To store data that’s available across the entire app, use entities. To learn more about actor storage, see storage.Restoring state
When a session goes idle, in-memory state resets. Persistent storage survives, but the session doesn’t automatically load the stored data back into memory when the session activates. UsehandleStart() to restore persistent data, such as the chat history or the game board.
For a sample flow, see persist session data.
Scheduled work
Actors provide functionality for a session to run logic on a schedule, independent of client messages:- Ticks: Run code on an interval and keep the session active while they repeat.
- Wakes: Run once at a time you choose, whether the session is active or idle.
Ticks
Ticks run code on an interval, and they keep the session active while they repeat. UseshouldTick() to set the conditions for when ticking runs.
An actor re-evaluates shouldTick() after every connection, message, disconnection, and tick, so ticking stops as soon as the condition is no longer met.
Ticking needs at least one connected client.
- Choose the length of each interval.
- Define a condition that starts and stops ticking, such as “at least two players are in the session.”
- Write the code that runs each interval.
Wakes
A wake runs once at a time you choose, whether the session is active or idle. With wakes, you can:- Schedule when a wake runs.
- Implement logic that runs when a wake occurs.
Deploy an actor
Deploy actors withactors deploy. To push all project resources at once, use deploy. Deploying adds and updates remote actors and leaves the rest in place.
Delete an actor
To delete an actor:- Run
actors deleteto delete the remote actor from Base44. - Delete the actor directory from your local project.
If you don’t delete the actor from your local project, the next
actors deploy or deploy recreates and deploys the actor to your Base44 project.See also
- Create an Actor: Actor files and a minimal class
- Actors: Sample Flows: Lifecycle, timers, connections, storage, entities, and backend functions
- Actors Files and Code: Complete backend class API
actorsSDK reference: Client SDK API