Bird
Raised Fist0
Expressframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. In Express with Socket.IO, what does emitting a message mean?
easy
A. Starting the server
B. Listening for incoming messages
C. Closing the connection
D. Sending a message to connected clients

Solution

  1. Step 1: Understand the meaning of emitting

    Emitting means sending a message or event from the server to clients or vice versa.
  2. Step 2: Differentiate emitting from receiving

    Receiving means listening for messages, not sending them.
  3. Final Answer:

    Sending a message to connected clients -> Option D
  4. Quick Check:

    Emitting = Sending message [OK]
Hint: Emitting means sending messages out [OK]
Common Mistakes:
  • Confusing emitting with receiving
  • Thinking emitting closes connections
  • Mixing emitting with server start
2. Which of the following is the correct syntax to emit a message named 'chat message' with data 'Hello' to all clients using Socket.IO in Express?
easy
A. io.send('chat message', 'Hello');
B. io.emit('chat message', 'Hello');
C. socket.emit('chat message');
D. io.on('chat message', 'Hello');

Solution

  1. Step 1: Identify the method to send to all clients

    The io.emit method sends a message to all connected clients.
  2. Step 2: Check the syntax correctness

    io.emit('chat message', 'Hello'); correctly emits the event with data.
  3. Final Answer:

    io.emit('chat message', 'Hello'); -> Option B
  4. Quick Check:

    Use io.emit to broadcast [OK]
Hint: Use io.emit(event, data) to send to all clients [OK]
Common Mistakes:
  • Using io.on instead of io.emit
  • Omitting the data argument
  • Using socket.emit to broadcast to all clients
3. Given this server code snippet in Express with Socket.IO:
io.on('connection', (socket) => {
  socket.on('greet', (msg) => {
    socket.emit('reply', `Hello, ${msg}`);
  });
});

What will the client receive if it emits 'greet' with message 'Alice'?
medium
A. A 'reply' event with message 'Hello, Alice'
B. No message received
C. A 'greet' event with message 'Hello, Alice'
D. An error because of missing broadcast

Solution

  1. Step 1: Understand the event flow

    The server listens for 'greet' from the client, then emits 'reply' back to the same client.
  2. Step 2: Check the emitted message content

    The message sent is `Hello, ${msg}`, so with 'Alice' it becomes 'Hello, Alice'.
  3. Final Answer:

    A 'reply' event with message 'Hello, Alice' -> Option A
  4. Quick Check:

    Reply event sends greeting with name [OK]
Hint: Server emits reply event with personalized message [OK]
Common Mistakes:
  • Expecting broadcast to all clients
  • Confusing event names 'greet' and 'reply'
  • Thinking no message is sent back
4. What is wrong with this Express Socket.IO server code snippet?
io.on('connection', (socket) => {
  socket.emit('welcome', 'Hello client');
  socket.on('message', (msg) => {
    io.emit('message', msg);
  });
  socket.on('disconnect', () => {
    socket.emit('goodbye', 'Bye');
  });
});
medium
A. Missing callback for 'welcome' event
B. Using io.emit inside 'message' handler is invalid
C. Emitting 'goodbye' after disconnect causes error
D. No error, code is correct

Solution

  1. Step 1: Analyze the 'disconnect' event usage

    When a client disconnects, the socket is closed and cannot emit messages.
  2. Step 2: Identify the error

    Trying to emit 'goodbye' after disconnect causes an error because socket is no longer connected.
  3. Final Answer:

    Emitting 'goodbye' after disconnect causes error -> Option C
  4. Quick Check:

    Cannot emit after disconnect [OK]
Hint: Don't emit messages after disconnect event [OK]
Common Mistakes:
  • Thinking io.emit is invalid inside handlers
  • Expecting callback needed for emit
  • Assuming no error on emitting after disconnect
5. You want to send a private message from one client to another using Express and Socket.IO. Which approach correctly emits a message only to the target client identified by targetId?
hard
A. io.to(targetId).emit('private message', msg);
B. io.emit('private message', { to: targetId, text: msg });
C. socket.broadcast.emit('private message', { to: targetId, text: msg });
D. socket.emit('private message', msg);

Solution

  1. Step 1: Understand how to target a specific client

    Socket.IO uses io.to(socketId).emit() to send messages to a specific client.
  2. Step 2: Evaluate each option

    io.to(targetId).emit('private message', msg); correctly uses io.to(targetId).emit() to send only to the client with targetId.
  3. Final Answer:

    io.to(targetId).emit('private message', msg); -> Option A
  4. Quick Check:

    Use io.to(socketId).emit for private messages [OK]
Hint: Use io.to(targetId).emit for private messages [OK]
Common Mistakes:
  • Using io.emit broadcasts to all clients
  • socket.broadcast.emit sends to all except sender
  • socket.emit sends only to sender, not target
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard