This simple gateway listens for 'ping' messages and replies with 'pong' plus the sent data. It shows how WebSockets enable instant two-way communication.
A. Using client.emit instead of this.server.emit for broadcasting
B. Missing type annotations for client and payload parameters
C. handleChat method should return a value to send response
D. The @SubscribeMessage decorator is misspelled
Solution
Step 1: Understand client.emit vs server.emit
Using client.emit sends message only to that client, but this.server.emit broadcasts to all connected clients.
Step 2: Identify intended behavior
Since the event is 'chat', usually the message should broadcast to all clients, so this.server.emit is correct.
Final Answer:
Using client.emit instead of this.server.emit for broadcasting -> Option A
Quick Check:
Broadcast needs this.server.emit, not client.emit [OK]
Hint: Broadcast uses this.server.emit, not client.emit [OK]
Common Mistakes:
Confusing client.emit with broadcast emit
Thinking missing types cause runtime error
Assuming decorator spelling is wrong
5. You want to build a live sports score update feature using NestJS WebSockets. Which approach best ensures all connected clients receive updates instantly and efficiently?
hard
A. Use client-side timers to simulate score changes without server communication
B. Send HTTP requests every 5 seconds from clients to fetch latest scores
C. Store scores in a database and let clients query on page load only
D. Use a WebSocket gateway with this.server.emit to broadcast score updates to all clients
Solution
Step 1: Identify real-time update requirement
Live sports scores need instant updates pushed to all clients without delay.
Step 2: Choose efficient communication method
Using WebSocket gateway with this.server.emit broadcasts updates instantly to all connected clients efficiently.
Final Answer:
Use a WebSocket gateway with this.server.emit to broadcast score updates to all clients -> Option D
Quick Check:
Broadcast via WebSocket = instant updates [OK]
Hint: Broadcast with this.server.emit for instant updates [OK]
Common Mistakes:
Using periodic HTTP polling instead of WebSockets
Relying on client-only timers without server data
Fetching data only on page load, missing live updates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.