Lifecycle
Lifecycle Hooks
Actor lifecycle hooks are defined as functions in the actor configuration.
createState
and state
The createState
function or state
constant defines the initial state of the actor (see state documentation). The createState
function is called only once when the actor is first created.
createVars
and vars
The createVars
function or vars
constant defines ephemeral variables for the actor (see state documentation). These variables are not persisted and are useful for storing runtime-only objects or temporary data.
The createVars
function can also receive driver-specific context as its second parameter, allowing access to driver capabilities like Rivet KV or Cloudflare Durable Object storage.
onCreate
The onCreate
hook is called at the same time as createState
, but unlike createState
, it doesn’t return any value. Use this hook for initialization logic that doesn’t affect the initial state.
onStart
This hook is called any time the actor is started (e.g. after restarting, upgrading code, or crashing).
This is called after the actor has been initialized but before any connections are accepted.
Use this hook to set up any resources or start any background tasks, such as setInterval
.
onStateChange
Called whenever the actor’s state changes. This is often used to broadcast state updates.
createConnState
and connState
There are two ways to define the initial state for connections:
connState
: Define a constant object that will be used as the initial state for all connectionscreateConnState
: A function that dynamically creates initial connection state based on connection parameters
onBeforeConnect
The onBeforeConnect
hook is called whenever a new client connects to the actor. Clients can pass parameters when connecting, accessible via params
. This hook is used for connection validation and can throw errors to reject connections.
The onBeforeConnect
hook does NOT return connection state - it’s used solely for validation.
Connections cannot interact with the actor until this method completes successfully. Throwing an error will abort the connection. This can be used for authentication - see Authentication for details.
onConnect
Executed after the client has successfully connected.
Messages will not be processed for this actor until this hook succeeds. Errors thrown from this hook will cause the client to disconnect.
onDisconnect
Called when a client disconnects from the actor. Use this to clean up any connection-specific resources.
Destroying Actors
Actors can be shut down gracefully with c.shutdown()
. Clients will be gracefully disconnected.
This action is permanent and cannot be reverted.
Using ActorContext
Type Externally
When extracting logic from lifecycle hooks or actions into external functions, you’ll often need to define the type of the context parameter. ActorCore provides helper types that make it easy to extract and pass these context types to external functions.
See Helper Types for more details on using ActorContextOf
.