Lifecycle
Lifecycle Hooks
_onInitialize
_onInitialize(): State | Promise<State>
Called when the actor is first created. This method should return the initial state of the actor.
_onStart
_onStart(): void | Promise<void>
Called after the actor has been initialized but before any connections are accepted. If the actor crashes or is upgraded, this method will be called before startup. If you need to upgrade your state, use this method.
Use this to set up any resources or start any background tasks.
_onStateChange
_onStateChange(newState: State): void | Promise<void>
Called whenever the actor's state changes. This is often used to broadcast state updates.
_onBeforeConnect
_onBeforeConnect?(opts: OnBeforeConnectOptions<this>): ConnState | Promise<ConnState>
Called whenever a new client connects to the actor. Clients can pass parameters when connecting, accessible via opts.parameters
.
The returned value becomes the connection's initial state and can be accessed later via connection.state
.
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
_onConnect?(connection: Connection<this>): void | Promise<void>
Executed after the client has successfully connected.
Messages will not be processed for this actor until this method succeeds.
Errors thrown from this method will cause the client to disconnect.
_onDisconnect
_onDisconnect?(connection: Connection<this>): void | Promise<void>
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 this._shutdown()
. Clients will be gracefully disconnected.
This action is permanent and cannot be reverted.
Actors can also be destroyed externally via the platform API with actors.destroy.