📖 Scenario: You are building a simple NestJS application that can send a broadcast message to multiple connected clients using WebSockets.This is useful in real-time apps like chat rooms or live notifications.
🎯 Goal: Create a NestJS WebSocket gateway that can broadcast a message to all connected clients.
📋 What You'll Learn
Create a WebSocket gateway class named AppGateway
Add a method called broadcastMessage that sends a message to all clients
Use the @WebSocketServer() decorator to access the server instance
Broadcast a string message 'Hello everyone!' to all connected clients
💡 Why This Matters
🌍 Real World
Broadcasting messages is essential in real-time applications like chat apps, live notifications, and multiplayer games to keep all users updated instantly.
💼 Career
Understanding WebSocket gateways and broadcasting in NestJS is valuable for backend developers working on scalable real-time systems.
Progress0 / 4 steps
1
Create the WebSocket gateway class
Create a class called AppGateway and decorate it with @WebSocketGateway() from @nestjs/websockets. Import the decorator correctly.
NestJS
Hint
Use @WebSocketGateway() above the class declaration.
2
Add the WebSocket server property
Inside the AppGateway class, add a property called server decorated with @WebSocketServer() imported from @nestjs/websockets. This will hold the WebSocket server instance.
NestJS
Hint
Import WebSocketServer and add @WebSocketServer() server: Server; inside the class.
3
Add the broadcastMessage method
Add a method called broadcastMessage inside AppGateway that calls this.server.emit with event name 'broadcast' and message 'Hello everyone!'.
NestJS
Hint
Define broadcastMessage() that uses this.server.emit to send the message.
4
Call broadcastMessage in the constructor
Add a constructor to AppGateway and call this.broadcastMessage() inside it to send the broadcast message when the gateway starts.
NestJS
Hint
Add a constructor() method and call this.broadcastMessage() inside it.
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
Step 1: Understand broadcasting meaning
Broadcasting means sending the same message to everyone connected.
Step 2: Apply to NestJS WebSocket gateway
In NestJS, broadcasting sends messages to all clients, not just one.
Final Answer:
Sends a message to all connected clients at once -> Option C
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
Step 1: Recall NestJS WebSocket broadcast syntax
The correct syntax is calling emit on this.server with event and data.
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.