Guards and the handshake — WebSockets · NestJS

Decide who may do what, at two moments: a guard that reads the handshake and refuses one kind of message, and a socket.io middleware, installed by an IoAdapter of your own, that refuses the connection itself before any message is sent.

What you will learn

Read the theory for Guards and the handshake

All WebSockets lessons

All NestJS courses

loading types…

What you'll learn

  • Write a guard for a gateway, reach the socket with context.switchToWs().getClient(), and refuse with a WsException that says why
  • Know what a guard that returns false sends: an exception event with Forbidden resource
  • Refuse a connection at the handshake with a socket.io middleware, server.use(), and know what the client receives: connect_error with the error's message
  • Extend IoAdapter, override createIOServer(), and install it with app.useWebSocketAdapter()