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 TheWsExceptionclass is exposed from@nestjs/websocketspackage.
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 };
}