Challenge - 5 Problems
WebSocket Gateway Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when a client connects to this NestJS WebSocket gateway?
Consider this NestJS WebSocket gateway code. What message will the server send to the client immediately after connection?
NestJS
import { WebSocketGateway, OnGatewayConnection, WebSocketServer } from '@nestjs/websockets'; import { Server, Socket } from 'socket.io'; @WebSocketGateway() export class ChatGateway implements OnGatewayConnection { @WebSocketServer() server: Server; handleConnection(client: Socket) { client.emit('welcome', 'Hello Client!'); } }
Attempts:
2 left
💡 Hint
Look at what the handleConnection method does with the client socket.
✗ Incorrect
The handleConnection method is called when a client connects. It sends a 'welcome' event with the message 'Hello Client!' to that client immediately.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a WebSocket gateway listening on port 3001?
You want to create a NestJS WebSocket gateway that listens on port 3001. Which code snippet correctly sets this up?
Attempts:
2 left
💡 Hint
Check the decorator syntax for passing options as an object.
✗ Incorrect
The @WebSocketGateway decorator accepts an options object. The correct syntax is @WebSocketGateway({ port: 3001 }).
❓ lifecycle
advanced2:00remaining
What happens if you implement OnGatewayDisconnect but forget to handle the disconnect event?
Given this gateway code, what will be the behavior when a client disconnects?
NestJS
import { WebSocketGateway, OnGatewayDisconnect } from '@nestjs/websockets'; import { Socket } from 'socket.io'; @WebSocketGateway() export class MyGateway implements OnGatewayDisconnect { handleDisconnect(client: Socket) { // Intentionally left blank } }
Attempts:
2 left
💡 Hint
Think about what an empty method implementation means for event handling.
✗ Incorrect
An empty handleDisconnect method means the server does nothing when a client disconnects. No errors occur, and the disconnect event is silently ignored.
🔧 Debug
advanced2:00remaining
Why does this WebSocket gateway fail to emit messages to all clients?
Review this gateway code. Why does calling this.server.emit('msg', 'hello') not send messages to all connected clients?
NestJS
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets'; import { Server } from 'socket.io'; @WebSocketGateway() export class BroadcastGateway { @WebSocketServer() server: Server; sendMessage() { this.server.emit('msg', 'hello'); } }
Attempts:
2 left
💡 Hint
Consider how NestJS creates and manages gateway instances and their properties.
✗ Incorrect
If the gateway class is not instantiated by NestJS (for example, manually created), the @WebSocketServer property will be undefined, so emit calls fail silently.
🧠 Conceptual
expert3:00remaining
How does NestJS WebSocket gateway handle multiple namespaces?
You want to create two WebSocket gateways in NestJS, each listening on a different namespace: '/chat' and '/news'. How should you configure the gateways to achieve this?
Attempts:
2 left
💡 Hint
Check how the namespace option is passed in the decorator options object.
✗ Incorrect
NestJS supports namespaces by passing the namespace option as an object to the @WebSocketGateway decorator. This creates separate gateways for each namespace.