Complete the code to import the WebSocket gateway decorator.
import { [1] } from '@nestjs/websockets';
The @WebSocketGateway decorator is used to create a WebSocket gateway in NestJS.
Complete the code to create a basic WebSocket gateway class.
@WebSocketGateway() export class [1] { }
The class name can be any valid name, but AppGateway is a common choice for the main gateway.
Fix the error in the code to correctly inject the WebSocket server instance.
import { WebSocketGateway, [1] } from '@nestjs/websockets'; import { Server } from 'socket.io'; @WebSocketGateway() export class AppGateway { @[1]() server: Server; }
The @WebSocketServer decorator injects the Socket.IO server instance into the gateway.
Fill both blanks to create a message handler method that listens for 'message' events.
import { WebSocketGateway, [1], MessageBody } from '@nestjs/websockets'; @WebSocketGateway() export class AppGateway { @[2]('message') handleMessage(@MessageBody() data: string): string { return data; } }
The @SubscribeMessage('message') decorator marks the method as a listener for 'message' events.
Fill all three blanks to create a gateway that listens on port 3001 and handles 'chat' events.
import { WebSocketGateway, [1], MessageBody } from '@nestjs/websockets'; @WebSocketGateway({ port: [2] }) export class ChatGateway { @[3]('chat') handleChat(@MessageBody() message: string): string { return message; } }
Use @SubscribeMessage to listen for 'chat' events, and set the port to 3001 in the gateway options.