0
0
NestJSframework~10 mins

Broadcasting messages in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AOnGatewayInit
BWebSocketServer
CSubscribeMessage
DWebSocketGateway
Attempts:
3 left
💡 Hint
Common Mistakes
Importing WebSocketServer instead of WebSocketGateway.
Using SubscribeMessage decorator in place of WebSocketGateway.
2fill in blank
medium

Complete 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'
Aemit
Bsend
Cbroadcast
Don
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.
3fill in blank
hard

Fix 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'
Asocket
Bclient
Cconnection
Dserver
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'server.broadcast.emit' which sends to all including sender.
Using an undefined variable like 'socket' or 'connection'.
4fill in blank
hard

Fill 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'
AclientId
Bsend
Cemit
Did
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.
5fill in blank
hard

Fill 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'
Abroadcast
Broom
Cemit
Dserver
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'server' instead of 'broadcast' to exclude sender.
Using 'send' instead of 'emit' to send the event.