Bird
Raised Fist0
NestJSframework~10 mins

Broadcasting messages in NestJS - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What does broadcasting messages in a NestJS WebSocket gateway do?
easy
A. Sends a message to only one client
B. Saves messages to a database
C. Sends a message to all connected clients at once
D. Blocks messages from being sent

Solution

  1. Step 1: Understand broadcasting meaning

    Broadcasting means sending the same message to everyone connected.
  2. Step 2: Apply to NestJS WebSocket gateway

    In NestJS, broadcasting sends messages to all clients, not just one.
  3. Final Answer:

    Sends a message to all connected clients at once -> Option C
  4. Quick Check:

    Broadcasting = send to all clients [OK]
Hint: Broadcast means send to everyone connected [OK]
Common Mistakes:
  • Thinking broadcasting sends to one client only
  • Confusing broadcasting with saving messages
  • Assuming broadcasting blocks messages
2. Which is the correct way to broadcast a message named 'update' with data in a NestJS WebSocket gateway?
easy
A. this.emit.server('update', data);
B. this.server.emit('update', data);
C. server.this.emit('update', data);
D. emit.this.server('update', data);

Solution

  1. Step 1: Recall NestJS WebSocket broadcast syntax

    The correct syntax is calling emit on this.server with event and data.
  2. Step 2: Check each option's syntax

    Only this.server.emit('update', data); matches the correct method call: this.server.emit('update', data); others have wrong order or method names.
  3. Final Answer:

    this.server.emit('update', data); -> Option B
  4. Quick Check:

    Correct emit call = this.server.emit(event, data) [OK]
Hint: Use this.server.emit(event, data) to broadcast [OK]
Common Mistakes:
  • Swapping order of 'this' and 'server'
  • Using emit as a property instead of a method
  • Incorrect method call syntax
3. Given this code inside a NestJS WebSocket gateway:
this.server.emit('notify', { message: 'Hello' });

What will happen when this line runs?
medium
A. The server logs the message but does not send it
B. Only the client who sent the last message receives the event
C. An error occurs because emit needs a callback
D. All connected clients receive the 'notify' event with the message

Solution

  1. Step 1: Understand this.server.emit behavior

    This method broadcasts the event to all connected clients.
  2. Step 2: Analyze the code effect

    The 'notify' event with the message object is sent to everyone connected.
  3. Final Answer:

    All connected clients receive the 'notify' event with the message -> Option D
  4. Quick Check:

    this.server.emit sends to all clients [OK]
Hint: Emit on server sends to all clients, not just one [OK]
Common Mistakes:
  • Thinking only one client receives the event
  • Assuming emit logs instead of sending
  • Believing emit requires a callback
4. Identify the error in this NestJS WebSocket gateway code snippet:
this.server.emit('chat', message)

where message is undefined.
medium
A. No error; undefined message is sent as is
B. Runtime error because message must be a string
C. Syntax error due to missing semicolon
D. Emit method requires a callback function

Solution

  1. Step 1: Check if undefined data causes error

    Emitting undefined data does not cause syntax or runtime errors in NestJS.
  2. Step 2: Confirm emit method requirements

    Emit accepts any data type; no callback is required.
  3. Final Answer:

    No error; undefined message is sent as is -> Option A
  4. Quick Check:

    Emit accepts undefined data without error [OK]
Hint: Undefined data can be emitted without error [OK]
Common Mistakes:
  • Assuming undefined causes runtime error
  • Thinking emit needs a callback
  • Confusing missing semicolon as error
5. You want to broadcast a message only to clients in a specific room named 'chat-room' in NestJS. Which code correctly does this?
hard
A. this.server.to('chat-room').emit('message', data);
B. this.server.emit('chat-room', 'message', data);
C. this.server.broadcast('chat-room').emit('message', data);
D. this.server.emit('message', data).to('chat-room');

Solution

  1. Step 1: Understand broadcasting to rooms in NestJS

    To send to a specific room, use this.server.to(roomName).emit(event, data).
  2. Step 2: Check each option for correct syntax

    this.server.to('chat-room').emit('message', data); uses correct chaining: to('chat-room').emit('message', data). Others misuse emit or method order.
  3. Final Answer:

    this.server.to('chat-room').emit('message', data); -> Option A
  4. Quick Check:

    Broadcast to room = this.server.to(room).emit(event, data) [OK]
Hint: Use this.server.to(room).emit(event, data) for room broadcast [OK]
Common Mistakes:
  • Calling emit before to()
  • Using broadcast() method which doesn't exist
  • Passing room name as event name
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard