Websocket Intregation
Realtime communication with Socket.IO.
Avleon's realtime layer is built on Socket.IO, an optional peer dependency:
Enabling websockets
Call useSocketIo during app setup. Options are passed straight through to the
Socket.IO server.
The server is created when the app runs; connecting clients are wired to your subscribers automatically.
Subscribing to events
Mark any service method with @Subscribe('event') to handle an incoming
client event. The class must be registered with the container — @AppService
does that.
Handlers run inside a socket context, so the originating socket is available
through SocketContextService for the duration of the call.
Emitting to clients
Inject SocketEventDispatcher to push events out:
dispatch accepts:
| Option | Description |
|---|---|
room | Emit only to this room |
broadcast | Exclude the current socket |
transports | Defaults to ['socket'] |
retry | Attempts on failure |
retryDelay | Base delay in ms; backs off per attempt |
Private channels
@PrivateChannel restricts a subscriber method to a channel derived from the
connecting socket — use it for per-user streams:
Reaching the raw server
For anything the helpers do not cover, resolve the Socket.IO Server itself:
Warning:
SocketIoServeris only registered once the app is running and websockets are enabled. Resolving it during module load will fail.
Broadcasting events
The event system can forward a dispatched event to Socket.IO, which keeps domain events and realtime delivery in one place: