Guards

There is no difference between web sockets guards and the regular guards. The only thing you should be aware of is that instead of throwing HttpException, you should use the WsException.

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

Binding guards#

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

JS TS

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

@UseGuards(AuthGuard)
@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