Complete the code to import the WebSocketGateway decorator from NestJS.
import { [1] } from '@nestjs/websockets';
The WebSocketGateway decorator is used to create a WebSocket gateway in NestJS, enabling real-time communication.
Complete the code to create a WebSocket gateway class named 'ChatGateway'.
@WebSocketGateway() export class [1] {}
The class name ChatGateway clearly indicates it handles chat-related WebSocket events.
Fix the error in the method to send a message to all connected clients.
import { WebSocketServer } from '@nestjs/websockets'; import { Server } from 'socket.io'; @WebSocketGateway() export class ChatGateway { @WebSocketServer() server: Server; broadcastMessage(message: string) { this.server.[1]('message', message); } }
The emit method sends an event with data to all connected clients in Socket.IO.
Fill in the blank to handle a client message event and respond back.
@SubscribeMessage('[1]') handleMessage(client: any, payload: string): string { return `Received: ${payload}`; }
The message event is the common event name for client messages in WebSocket communication.
Fill in the blanks to create a WebSocket gateway that logs when a client connects and disconnects.
import { WebSocketGateway, OnGatewayConnection, OnGatewayDisconnect } from '@nestjs/websockets'; @WebSocketGateway() export class LoggerGateway implements [1], [2] { handleConnection(client: any) { console.log('Client connected:', client.id); } handleDisconnect(client: any) { console.log('Client disconnected:', client.id); } }
The interfaces OnGatewayConnection and OnGatewayDisconnect allow the gateway to respond to client connect and disconnect events.