Challenge - 5 Problems
Message Mastery in Express
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when a message is emitted and received?
Consider an Express server using Socket.IO. When the server emits a message 'greet' with data 'Hello', what will the client receive?
Express
const io = require('socket.io')(3000); io.on('connection', socket => { socket.emit('greet', 'Hello'); });
Attempts:
2 left
💡 Hint
Think about how Socket.IO sends named events with data.
✗ Incorrect
The server emits an event named 'greet' with the string 'Hello'. The client listens for 'greet' and receives the data exactly as sent.
❓ state_output
intermediate2:00remaining
What is the value of the message count after multiple emits?
In an Express app with Socket.IO, the server counts how many times it emits a 'ping' message to a client. After emitting 3 times, what is the count?
Express
let count = 0; const io = require('socket.io')(3000); io.on('connection', socket => { for(let i=0; i<3; i++) { socket.emit('ping', 'ping'); count++; } }); // What is the value of count after connection?
Attempts:
2 left
💡 Hint
Count increments inside the loop for each emit.
✗ Incorrect
The loop runs 3 times, each time emitting and incrementing count. So count is 3 after connection.
📝 Syntax
advanced2:00remaining
Which option correctly emits a message to all connected clients?
Given a Socket.IO server instance 'io', which code correctly emits 'update' with data 'refresh' to all clients?
Express
const io = require('socket.io')(3000);
Attempts:
2 left
💡 Hint
Check the official Socket.IO method to emit to all clients.
✗ Incorrect
The method io.emit sends the event to all connected clients. Other options are invalid or do not exist.
🔧 Debug
advanced2:00remaining
Why does the client not receive the emitted message?
Server code:
const io = require('socket.io')(3000);
io.on('connection', socket => {
socket.emit('hello', 'world');
});
Client code:
const socket = io('http://localhost:3000');
socket.on('Hello', data => {
console.log(data);
});
Why does the client not log anything?
Attempts:
2 left
💡 Hint
Check event name spelling and case sensitivity.
✗ Incorrect
Event names are case sensitive. 'hello' and 'Hello' are different events, so client never receives 'Hello'.
🧠 Conceptual
expert2:00remaining
What happens if you emit a message to a disconnected socket?
In an Express app using Socket.IO, if the server tries to emit an event to a socket that has disconnected, what is the expected behavior?
Attempts:
2 left
💡 Hint
Think about how Socket.IO handles disconnected clients.
✗ Incorrect
Emitting to a disconnected socket does not throw errors. The message is simply not delivered because the client is gone.