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:
Method 1: ConnState
constant
Method 2: createConnState
function
The data returned from createConnState
is used as the initial state of the connection. The connection state can be accessed through conn.state
.
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.
Offline & Auto-Reconnection
See Interacting with Actors for details on reconnection behavior.