Pipes

There is no difference between web sockets pipes and the regular pipes. The only thing you should be aware of is that instead of throwing HttpException, you should use the WsException. Besides, all pipes will be applied only to the data parameter (because either validating or transforming client instance sounds awkward).

info Hint The WsException class is exposed from @nestjs/websockets package.

Binding pipes

Here is an example that makes use of a manually instantiated method-scoped pipe (class-scoped works too):

  1. @@filename()
  2. @UsePipes(new ValidationPipe())
  3. @SubscribeMessage('events')
  4. handleEvent(client: Client, data: unknown): WsResponse<unknown> {
  5. const event = 'events';
  6. return { event, data };
  7. }
  8. @@switch
  9. @UsePipes(new ValidationPipe())
  10. @SubscribeMessage('events')
  11. handleEvent(client, data) {
  12. const event = 'events';
  13. return { event, data };
  14. }