Skip to main content

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 @SubscribeMessage was 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 was ws.
  • 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 by maxPayloadSize.
  • Status and duration: completed or failed, 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. handleConnection and handleDisconnect are 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'],
},
},
})
OptionTypeDefaultDescription
enabledbooleanfalseRecord gateway messages
captureDatabooleantrueRecord the data the client sent
captureResponsebooleantrueRecord what the handler returned
maxPayloadSizenumber65536Bytes of data and of response kept, each; a larger value is recorded as its size. 0 keeps none
ignoreEventsstring[][]@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.