0
0
Expressframework~20 mins

Emitting and receiving messages in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Message Mastery in Express
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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');
});
AThe client receives the event 'greet' but with no data.
BThe client receives the event 'message' with data 'Hello'.
CThe client receives no event because emit is server-side only.
DThe client receives the event 'greet' with data 'Hello'.
Attempts:
2 left
💡 Hint
Think about how Socket.IO sends named events with data.
state_output
intermediate
2: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?
A3
B0
C1
DUndefined
Attempts:
2 left
💡 Hint
Count increments inside the loop for each emit.
📝 Syntax
advanced
2: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);
Aio.broadcast.emit('update', 'refresh');
Bio.send('update', 'refresh');
Cio.emit('update', 'refresh');
Dio.toAll('update', 'refresh');
Attempts:
2 left
💡 Hint
Check the official Socket.IO method to emit to all clients.
🔧 Debug
advanced
2: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?
AThe server emits before client connects.
BThe client listens for 'Hello' but the server emits 'hello' (case mismatch).
CThe client does not connect to the correct port.
DThe server emits to the wrong socket.
Attempts:
2 left
💡 Hint
Check event name spelling and case sensitivity.
🧠 Conceptual
expert
2: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?
AThe emit silently fails; no error is thrown but message is not delivered.
BThe server automatically reconnects the socket and sends the message.
CThe message queues until the socket reconnects.
DThe server throws a runtime error stopping the app.
Attempts:
2 left
💡 Hint
Think about how Socket.IO handles disconnected clients.