0
0
NestJSframework~20 mins

Broadcasting messages in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Broadcasting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when broadcasting a message to all connected clients?

Consider a NestJS WebSocket gateway that broadcasts a message to all connected clients using this.server.emit('message', data). What will the clients receive?

NestJS
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Server } from 'socket.io';

@WebSocketGateway()
export class ChatGateway {
  @WebSocketServer()
  server: Server;

  broadcastMessage(data: string) {
    this.server.emit('message', data);
  }
}
AAll connected clients receive the 'message' event with the data.
BOnly the client that sent the last message receives the broadcast.
CNo clients receive the message because emit is not a broadcast method.
DOnly clients in a specific room receive the message.
Attempts:
2 left
💡 Hint

Think about how emit works on the server instance in Socket.IO.

state_output
intermediate
2:00remaining
What is the state of connected clients after broadcasting?

In a NestJS WebSocket gateway, after calling this.server.emit('update', payload), what happens to the list of connected clients?

NestJS
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Server } from 'socket.io';

@WebSocketGateway()
export class UpdateGateway {
  @WebSocketServer()
  server: Server;

  sendUpdate(payload: any) {
    this.server.emit('update', payload);
  }
}
AClients are disconnected automatically after receiving the broadcast.
BThe list of connected clients is cleared after broadcasting.
CThe list of connected clients remains unchanged after broadcasting.
DOnly clients that received the update remain connected.
Attempts:
2 left
💡 Hint

Broadcasting sends messages but does not affect client connections.

📝 Syntax
advanced
2:00remaining
Which option correctly broadcasts a message to a specific room?

Given a NestJS WebSocket gateway, which code snippet correctly broadcasts a message only to clients in the room 'chat-room'?

NestJS
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Server } from 'socket.io';

@WebSocketGateway()
export class RoomGateway {
  @WebSocketServer()
  server: Server;

  broadcastToRoom(message: string) {
    // Which option is correct here?
  }
}
Athis.server.emit('chat-room', 'message', message);
Bthis.server.to('chat-room').emit('message', message);
Cthis.server.in('chat-room').broadcast.emit('message', message);
Dthis.server.broadcast.to('chat-room').emit('message', message);
Attempts:
2 left
💡 Hint

Use the Socket.IO method to target a specific room.

🔧 Debug
advanced
2:00remaining
Why does broadcasting fail with 'this.server.broadcast.emit'?

In a NestJS WebSocket gateway, a developer tries to broadcast a message using this.server.broadcast.emit('event', data) but it fails. Why?

NestJS
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Server } from 'socket.io';

@WebSocketGateway()
export class DebugGateway {
  @WebSocketServer()
  server: Server;

  sendEvent(data: any) {
    this.server.broadcast.emit('event', data);
  }
}
AThe server instance requires a namespace before using 'broadcast'.
B'broadcast' is deprecated and replaced by 'emit' on the server.
C'broadcast' only works inside client event handlers, not in the gateway class.
D'broadcast' is not a property of the server instance; it exists on individual sockets.
Attempts:
2 left
💡 Hint

Consider where the 'broadcast' property is available in Socket.IO.

🧠 Conceptual
expert
3:00remaining
How does NestJS handle broadcasting messages in clustered environments?

In a NestJS application running multiple instances behind a load balancer, what must be done to ensure WebSocket broadcasts reach all clients connected to any instance?

AUse a shared adapter like Redis adapter to synchronize broadcasts across instances.
BBroadcasting works automatically across instances without extra setup.
CUse HTTP polling instead of WebSockets to avoid broadcast issues.
DConfigure each instance to broadcast only to its own clients; cross-instance broadcast is not possible.
Attempts:
2 left
💡 Hint

Think about how multiple server instances share state for WebSocket events.