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.