Lifecycle Hooks
Handle client and room lifecycle events using the hooks configuration:
HooksConfig Options
Authentication Hook
The authenticate hook allows you to validate client authentication data and return JWT claims:
Hook Signature:
Parameters:
context: Global context withio,clients, androomsclientSocket: The raw Socket.IO socketauthData: Data sent from client during connection
Returns: Ok(AuthData) on success or Err(string) with error message
AuthData Structure:
The authenticated user's data is available via client.auth and the user ID is extracted from jwt.sub.
Socket Lifecycle Hooks
The socket.onConnect and socket.onDisconnect hooks provide low-level access to Socket.IO socket lifecycle events. These hooks receive the raw socket before the ConnectedClient wrapper is created, making them useful for socket-level operations.
onConnect Hook Signature:
onDisconnect Hook Signature:
Parameters:
context: Global context withio,clients, androomsclientSocket: The raw Socket.IO socket instance
When to use socket hooks vs client hooks:
-
Use
socket.onConnect/onDisconnectwhen you need:- Access to raw socket data (handshake, IP address, socket rooms)
- Socket-level operations before ConnectedClient creation
- Logging or monitoring at the socket layer
-
Use
clients.onConnected/onDisconnectedwhen you need:- Access to the high-level ConnectedClient API
- User-level operations (joining rooms, sending messages)
- Business logic based on user ID or auth data
Room Join Hook
The beforeJoin hook allows you to control room access and validate join requests:
Hook Signature:
Event Middleware Hooks
The beforeEach and afterEach hooks allow you to intercept and transform events:
beforeEach Hook Signature:
afterEach Hook Signature:
See Also
- Authentication - Auth data structure and client auth
- TypeScript Types - Hook type signatures and interfaces
- Dialogue Configuration - DialogueContext object