Challenge - 5 Problems
Broadcasting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when broadcasting a message to all clients?
Consider a Node.js server using WebSocket. When the server broadcasts a message to all connected clients, what will each client receive?
Node.js
const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', function connection(ws) { ws.on('message', function incoming(message) { wss.clients.forEach(function each(client) { if (client.readyState === WebSocket.OPEN) { client.send(message); } }); }); });
Attempts:
2 left
💡 Hint
Think about how the server loops through all clients and sends the message.
✗ Incorrect
The server loops through all connected clients and sends the received message to each one that is open. This means every client connected at that moment gets the message.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in broadcasting code
Which option contains a syntax error that prevents broadcasting messages to all clients?
Node.js
const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', ws => { ws.on('message', message => { wss.clients.forEach(client => { if (client.readyState === WebSocket.OPEN) { client.send(message) } }); }); });
Attempts:
2 left
💡 Hint
Check the if condition inside the forEach loop carefully.
✗ Incorrect
Missing semicolon after client.send(message) can cause issues in some JavaScript environments.
🔧 Debug
advanced2:00remaining
Why does broadcasting fail when clients disconnect?
Given this broadcasting code, why might some clients not receive messages after disconnecting and reconnecting?
Node.js
wss.on('connection', ws => { ws.on('message', message => { wss.clients.forEach(client => { if (client.readyState === WebSocket.OPEN) { client.send(message); } }); }); });
Attempts:
2 left
💡 Hint
Think about how the server tracks connected clients and their states.
✗ Incorrect
The server's wss.clients set includes all clients, but some may be closed. The readyState check prevents sending to closed clients, but if the server does not clean up properly, it may cause issues.
❓ state_output
advanced2:00remaining
What is the state of clients after broadcasting a message?
After broadcasting a message to all clients, what is the state of each client's WebSocket connection?
Node.js
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send('Hello');
}
});Attempts:
2 left
💡 Hint
Sending a message does not close the connection.
✗ Incorrect
Sending a message over WebSocket does not change the connection state. Clients remain OPEN unless explicitly closed.
🧠 Conceptual
expert3:00remaining
Which option best describes broadcasting scalability challenges?
When broadcasting messages to many connected clients in Node.js WebSocket server, what is a key scalability challenge?
Attempts:
2 left
💡 Hint
Consider what happens on the server when sending many messages at once.
✗ Incorrect
Sending messages to many clients simultaneously can increase CPU and memory usage on the server, potentially causing delays or crashes if not managed properly.