Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the WebSocketGateway decorator.
NestJS
import { [1] } from '@nestjs/websockets'; @WebSocketGateway() export class EventsGateway {}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing WebSocketServer instead of WebSocketGateway.
Using SubscribeMessage decorator in place of WebSocketGateway.
✗ Incorrect
The WebSocketGateway decorator is imported from '@nestjs/websockets' to define a WebSocket gateway.
2fill in blank
mediumComplete the code to broadcast a message to all connected clients.
NestJS
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets'; import { Server } from 'socket.io'; @WebSocketGateway() export class EventsGateway { @WebSocketServer() server: Server; broadcastMessage(message: string) { this.server.[1]('message', message); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' which is not a method on the server instance.
Using 'broadcast' which is used differently in Socket.IO.
✗ Incorrect
The 'emit' method sends an event to all connected clients in Socket.IO.
3fill in blank
hardFix the error in the method that broadcasts a message only to other clients except the sender.
NestJS
import { WebSocketGateway, WebSocketServer, SubscribeMessage, MessageBody, ConnectedSocket } from '@nestjs/websockets'; import { Server, Socket } from 'socket.io'; @WebSocketGateway() export class EventsGateway { @WebSocketServer() server: Server; @SubscribeMessage('sendMessage') handleMessage(@MessageBody() message: string, @ConnectedSocket() client: Socket) { [1].broadcast.emit('message', message); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'server.broadcast.emit' which sends to all including sender.
Using an undefined variable like 'socket' or 'connection'.
✗ Incorrect
The 'client' parameter is the socket instance; broadcasting to others uses 'client.broadcast.emit'.
4fill in blank
hardFill both blanks to create a method that sends a private message to a specific client by id.
NestJS
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets'; import { Server } from 'socket.io'; @WebSocketGateway() export class EventsGateway { @WebSocketServer() server: Server; sendPrivateMessage(clientId: string, message: string) { this.server.sockets.sockets.get([1]).[2]('privateMessage', message); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'emit' which is the correct Socket.IO method.
Using 'id' instead of 'clientId' to get the socket.
✗ Incorrect
Use 'clientId' to get the socket, then 'emit' to send the event to that client.
5fill in blank
hardFill all three blanks to create a method that broadcasts to a room except the sender.
NestJS
import { WebSocketGateway, WebSocketServer, ConnectedSocket } from '@nestjs/websockets'; import { Server, Socket } from 'socket.io'; @WebSocketGateway() export class EventsGateway { @WebSocketServer() server: Server; broadcastToRoom(room: string, message: string, client: Socket) { client.[1].to([2]).[3]('roomMessage', message); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'server' instead of 'broadcast' to exclude sender.
Using 'send' instead of 'emit' to send the event.
✗ Incorrect
Use 'client.broadcast.to(room).emit' to send a message to all clients in the room except the sender.