0
0
NestJSframework~10 mins

Broadcasting messages in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Broadcasting messages
Client connects
Server receives connection
Server listens for message
Server receives message
Server broadcasts message to all clients
Clients receive broadcasted message
This flow shows how a NestJS server receives a message from one client and broadcasts it to all connected clients.
Execution Sample
NestJS
import { WebSocketGateway, SubscribeMessage, WebSocketServer } from '@nestjs/websockets';
import { Server } from 'socket.io';

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

  @SubscribeMessage('message')
  handleMessage(client: any, payload: string) {
    this.server.emit('message', payload);
  }
}
This code defines a WebSocket gateway in NestJS that listens for 'message' events and broadcasts the received message to all clients.
Execution Table
StepActionInput/ConditionResultBroadcasted Message
1Client connectsClient A connectsConnection establishedNone
2Client sends messageClient A sends 'Hello!'Server receives 'Hello!'None
3Server broadcastsServer emits 'Hello!'All clients receive 'Hello!''Hello!'
4Client B receivesClient B connectedClient B gets 'Hello!''Hello!'
5Client C receivesClient C connectedClient C gets 'Hello!''Hello!'
6No more messagesNo new inputWaiting for next messageNone
💡 No new messages sent, server waits for next input
Variable Tracker
VariableStartAfter Step 2After Step 3Final
serverServer instance createdServer ready to emitServer emitted 'Hello!'Server ready for next
payloadundefined'Hello!''Hello!'undefined or next message
Key Moments - 2 Insights
Why does the server use this.server.emit instead of client.emit?
Using this.server.emit sends the message to all connected clients, not just the sender. See execution_table step 3 where the server broadcasts to all.
What happens if no clients are connected when the server emits?
The message is sent but no clients receive it. The server stays ready for future connections, as shown in execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what message does the server broadcast at step 3?
A'Goodbye!'
BNo message
C'Hello!'
D'Welcome!'
💡 Hint
Check the 'Broadcasted Message' column at step 3 in the execution_table.
At which step does Client B receive the broadcasted message?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' and 'Result' columns for Client B in the execution_table.
If the server used client.emit instead of this.server.emit, what would change?
AOnly the sending client receives the message
BAll clients receive the message
CNo clients receive the message
DThe server crashes
💡 Hint
Refer to key_moments explanation about server.emit vs client.emit.
Concept Snapshot
NestJS broadcasting messages:
- Use @WebSocketGateway() to create gateway
- Use @SubscribeMessage('event') to listen
- Use this.server.emit('event', data) to broadcast
- Emits send to all connected clients
- Clients receive broadcasted messages automatically
Full Transcript
In NestJS, broadcasting messages means sending a message from one client to all connected clients through the server. The server listens for a message event using @SubscribeMessage. When a message arrives, the server uses this.server.emit to send it to everyone. This way, all clients get the message, not just the sender. If no clients are connected, the server waits for new connections. This process helps create real-time chat or notification systems easily.