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).

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):

JS TS

@UsePipes(new ValidationPipe())
@SubscribeMessage('events')
handleEvent(client: Client, data: unknown): WsResponse<unknown> {
  const event = 'events';
  return { event, data };
}

@UsePipes(new ValidationPipe())
@SubscribeMessage('events')
handleEvent(client, data) {
  const event = 'events';
  return { event, data };
}

Support us

Nest is an MIT-licensed open source project. It can grow thanks to the support by these awesome people. If you'd like to join them, please read more here.

Principal Sponsor

Sponsors / Partners

Become a sponsor