A WebSocket Gateway in NestJS is a special class that handles WebSocket connections, allowing real-time communication between the server and clients.
Click to reveal answer
beginner
Which decorator is used to create a WebSocket Gateway in NestJS?
The @WebSocketGateway() decorator is used to mark a class as a WebSocket Gateway in NestJS.
Click to reveal answer
beginner
How do you listen for a specific WebSocket event in a NestJS Gateway?
Use the @SubscribeMessage('eventName') decorator on a method inside the Gateway class to listen for that event.
Click to reveal answer
intermediate
What method signature should a message handler have in a NestJS WebSocket Gateway?
A message handler method should accept two parameters: the client socket and the data sent by the client, e.g., handleMessage(client: Socket, payload: any).
Click to reveal answer
beginner
How can you send a message back to the client from a WebSocket Gateway in NestJS?
You can use the client.emit('eventName', data) method to send a message back to the connected client.
Click to reveal answer
Which decorator marks a class as a WebSocket Gateway in NestJS?
A@Controller()
B@WebSocketGateway()
C@Injectable()
D@Module()
✗ Incorrect
The @WebSocketGateway() decorator is specifically used to create WebSocket Gateways.
How do you listen for a WebSocket event named 'chat' in a NestJS Gateway?
AUse @SubscribeMessage('chat') on a method
BUse @OnEvent('chat') on a method
CUse @Listen('chat') on a method
DUse @WebSocketEvent('chat') on a method
✗ Incorrect
The @SubscribeMessage('eventName') decorator listens for WebSocket events.
What parameters does a WebSocket message handler method usually receive?
Aonly client socket
Brequest and response objects
Conly payload data
Dclient socket and payload data
✗ Incorrect
Handlers receive the client socket and the data sent by the client.
Which method sends a message back to the client in a NestJS WebSocket Gateway?
Agateway.emit()
Bclient.sendMessage()
Cclient.emit()
Dgateway.send()
✗ Incorrect
client.emit() sends an event with data back to the connected client.
What is the main purpose of a WebSocket Gateway in NestJS?
ATo handle real-time WebSocket communication
BTo serve static files
CTo manage database connections
DTo handle HTTP requests
✗ Incorrect
WebSocket Gateways manage real-time communication using WebSockets.
Explain how to create a basic WebSocket Gateway in NestJS and listen for a message event.
Think about decorators and message handlers.
You got /5 concepts.
Describe how a NestJS WebSocket Gateway handles incoming messages and sends responses.
Focus on the flow of message handling.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of a WebSocket gateway in a NestJS application?
easy
A. To serve static files like images and CSS
B. To handle real-time communication between server and clients
C. To manage database connections
D. To handle HTTP request routing
Solution
Step 1: Understand WebSocket gateway role
A WebSocket gateway is designed to manage real-time, two-way communication between the server and clients.
Step 2: Compare with other server roles
Managing database connections, serving static files, or HTTP routing are handled by other parts of NestJS, not WebSocket gateways.
Final Answer:
To handle real-time communication between server and clients -> Option B
Quick Check:
WebSocket gateway = real-time communication [OK]
Hint: WebSocket gateways are for live message exchange [OK]
Common Mistakes:
Confusing WebSocket gateway with HTTP routing
Thinking it manages database connections
Assuming it serves static files
2. Which decorator is used to define a WebSocket gateway class in NestJS?
easy
A. @Gateway()
B. @SocketGateway()
C. @WebSocketGateway()
D. @WebSocketServer()
Solution
Step 1: Identify the correct decorator for gateway class
The official NestJS decorator to create a WebSocket gateway class is @WebSocketGateway().
Step 2: Differentiate from other decorators
@WebSocketServer() is used inside the class to inject the server instance, not to define the class itself. The others are incorrect or do not exist.
Final Answer:
@WebSocketGateway() -> Option C
Quick Check:
Gateway class decorator = @WebSocketGateway() [OK]
Hint: Gateway class always uses @WebSocketGateway() [OK]
Common Mistakes:
Using @WebSocketServer() to decorate the class
Confusing with non-existent decorators
Omitting parentheses on the decorator
3. Given this NestJS WebSocket gateway code, what will be logged when a client sends a 'message' event with payload 'Hello'?
The method logs the string 'Received:' followed by the data payload, which is 'Hello'.
Step 2: Understand what is returned vs logged
The return value is 'Echo: Hello', but only the console.log outputs to the server console. The client receives the return value, but the question asks about what is logged.
Final Answer:
Received: Hello -> Option A
Quick Check:
Console logs 'Received: Hello' [OK]
Hint: Look for console.log to find logged output [OK]
Common Mistakes:
Confusing returned value with logged output
Assuming client message is logged
Ignoring the console.log statement
4. Identify the error in this WebSocket gateway code snippet:
B. Incorrect event name in @SubscribeMessage decorator
C. WebSocketGateway decorator requires a port number
D. Missing @MessageBody() decorator on the method parameter
Solution
Step 1: Check method parameter decorators
In NestJS WebSocket gateways, to receive the message payload, the parameter must be decorated with @MessageBody().
Step 2: Verify other parts
The event name 'chat' is valid, @WebSocketGateway() can be used without a port, and the method does not need to be async unless awaiting something.
Final Answer:
Missing @MessageBody() decorator on the method parameter -> Option D
Quick Check:
Parameter needs @MessageBody() [OK]
Hint: Always decorate message parameters with @MessageBody() [OK]
Common Mistakes:
Omitting @MessageBody() on parameters
Thinking port number is mandatory
Assuming method must be async
5. How can you broadcast a message to all connected clients from within a NestJS WebSocket gateway?
hard
A. Use the injected server instance with this.server.emit('event', data)
B. Return the message string from the event handler method
C. Call client.send(data) on a single client only
D. Use @SubscribeMessage('broadcast') decorator on the method
Solution
Step 1: Understand broadcasting in NestJS gateways
To send a message to all clients, you use the server instance injected with @WebSocketServer() and call emit on it.
Step 2: Differentiate from other options
Returning a string sends a response only to the sender. Calling client.send() targets one client. The decorator @SubscribeMessage() only listens for events, it does not broadcast.
Final Answer:
Use the injected server instance with this.server.emit('event', data) -> Option A
Quick Check:
Broadcast = this.server.emit() [OK]
Hint: Broadcast with this.server.emit() inside gateway [OK]
Common Mistakes:
Returning message instead of emitting
Sending to one client only
Misusing decorators for broadcasting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.