Connections
Connections represent client connections to your actor. They provide a way to handle client authentication, manage connection-specific data, and control the connection lifecycle.
Parameters
When clients connect to an actor, they can pass connection parameters that are handled during the connection process.
For example:
Connection State
There are two ways to define an actor's connection state:
Define connection state as a constant value:
This value will be cloned for every new connection using structuredClone
.
Connection Lifecycle Hooks
The connection lifecycle has several hooks:
onBeforeConnect
: Called before a client connects, returns the connection stateonConnect
: Called when a client successfully connectsonDisconnect
: Called when a client disconnects
See the documentation on Actor Lifecycle for more details.
Connection List
All active connections can be accessed through the context object's conns
property. This is an array of all current connections.
This is frequently used with conn.send(name, event)
to send messages directly to clients.
For example:
Disconnecting clients
Connections can be disconnected from within an action:
If you need to wait for the disconnection to complete, you can use await
:
This ensures the underlying network connections close cleanly before continuing.
Connection Error Handling
Handle connection errors using the .onError()
method:
Offline & Auto-Reconnection
See client documentation for details on reconnection behavior.