Your first gateway — WebSockets · NestJS

Open the cats API to socket.io clients: a gateway on the app's own port, message handlers that acknowledge with their return value, a property picked out of the payload, and an event sent back to the sender instead of an acknowledgement.

What you will learn

Read the theory for Your first gateway

All WebSockets lessons

All NestJS courses

loading types…

What you'll learn

  • Explain what a WebSocket connection adds to HTTP, and what socket.io adds to WebSockets: events, acknowledgements, rooms
  • Turn a provider into a gateway with @WebSocketGateway(), on the HTTP server's port, registered in a module's providers
  • Handle events with @SubscribeMessage() and read the payload, or one of its properties, with @MessageBody()
  • Tell the two ways a handler answers apart: a return value is the acknowledgement, a { event, data } object is an event to the sender, and null or undefined sends nothing at all