Pipes

There is no fundamental difference between regular pipes and microservice pipes. The only difference is that instead of throwing HttpException, you should use WsException. In addition, all pipes will be only applied to the data parameter (because validating or transforming client instance is useless).

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

Binding pipes

The following example uses a manually instantiated method-scoped pipe. Just as with HTTP based applications, you can also use gateway-scoped pipes (i.e., prefix the gateway class with a @UsePipes() decorator).

  1. @UsePipes(new ValidationPipe())
  2. @SubscribeMessage('events')
  3. handleEvent(client: Client, data: unknown): WsResponse<unknown> {
  4. const event = 'events';
  5. return { event, data };
  6. }
  1. @UsePipes(new ValidationPipe())
  2. @SubscribeMessage('events')
  3. handleEvent(client, data) {
  4. const event = 'events';
  5. return { event, data };
  6. }