WebSocket Watcher
The WebSocket Watcher records every message a gateway's @SubscribeMessage
handler receives, on Nest's Socket.IO adapter (@nestjs/platform-socket.io)
and its ws adapter (@nestjs/platform-ws), with the data the client sent,
what the handler returned, how long it took and, when it failed, why. It needs
nothing from your code but the switch:
NestLensModule.forRoot({
watchers: {
websocket: true,
},
})
What it records
One websocket entry per message a handler took:
- The event
@SubscribeMessagewas given, the gateway's class and the handler. - The client: for Socket.IO, the namespace, the socket id and the address
from the handshake; for
ws, which keeps neither, only that it wasws. - The data the client sent and what the handler returned — the
acknowledgement, or the
{ event, data }it sends back. Both are masked like a request body and bounded bymaxPayloadSize. - Status and duration:
completedorfailed, and the thrown value's message when it failed.
Nest never gives a gateway the global exception filters, so an exception a
handler throws reached nothing of NestLens's. It is recorded from here, as an
exception entry linked to the message, and the client is answered exactly as
it is without NestLens: a WsException's error, or Nest's Internal server error for anything else.
Each message runs in a request context of its own, so what its handler does is
attributed to it: a query, a log line or a cache read made while handling the
message carries its requestId.
The watcher reports waiting when it is on and no @WebSocketGateway
provider has a @SubscribeMessage handler: there is nothing for it to record.
Otherwise it is recording, with the number of handlers it is bound to.
How handlers are reached
The watcher is bound to each @SubscribeMessage handler as
@UseInterceptors() on the method would bind it — the same way, and for the
same reasons, as the message watcher.
Your own interceptors on a handler run around it.
Not recorded:
- Connections and disconnections.
handleConnectionandhandleDisconnectare not message handlers, and Nest runs no interceptor around them. - What a guard throws. Guards run before any interceptor, and a gateway has no global filter to catch what they throw.
- What the server emits outside a handler's return value —
this.server.emit(...),client.emit(...)— which is the adapter's, not Nest's.
Configuration
NestLensModule.forRoot({
watchers: {
websocket: {
captureData: true,
captureResponse: true,
maxPayloadSize: 64 * 1024,
ignoreEvents: ['ping'],
},
},
})
| Option | Type | Default | Description |
|---|---|---|---|
enabled | boolean | false | Record gateway messages |
captureData | boolean | true | Record the data the client sent |
captureResponse | boolean | true | Record what the handler returned |
maxPayloadSize | number | 65536 | Bytes of data and of response kept, each; a larger value is recorded as its size. 0 keeps none |
ignoreEvents | string[] | [] | @SubscribeMessage event names not to record |
When an option is off
captureData: false and captureResponse: false leave data and response
out, and the entry lists each under payload.notCaptured with the option, such
as { field: 'data', option: 'watchers.websocket.captureData' }. A failed
handler answered with its error and lists no response. The dashboard's Data
and Response tabs say Not captured — captureData is off for the WebSocket
watcher, where they would otherwise read as a handler that returned nothing.
The exception a failed handler records leaves the data out too,
and lists it there as origin.data.
Payload Structure
interface WebSocketEntry {
type: 'websocket';
payload: {
event: string; // 'chat:send'
gateway: string; // 'ChatGateway'
handler?: string; // 'ChatGateway.send'
adapter?: string; // 'socket.io' | 'ws'
namespace?: string; // Socket.IO
clientId?: string; // Socket.IO
ip?: string; // Socket.IO
data?: unknown;
response?: unknown;
status: 'completed' | 'failed';
error?: string;
duration: number; // ms
notCaptured?: { field: string; option: string }[]; // What a capture option kept out
};
}
Dashboard View
The WebSockets page lists each message with its event, gateway, adapter, status and duration, and filters by status, event (matching text inside the name, ignoring case) and gateway. The detail page shows the data, the response, the client and everything the handler did while answering.
Related Watchers
- Exception Watcher - The exception a failed message threw
- Message Watcher - Messages a microservice receives