0
0
Node.jsframework~20 mins

Socket.io overview in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Socket.io Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Socket.io server code do?
Consider this Node.js Socket.io server snippet. What will happen when a client connects?
Node.js
import { Server } from 'socket.io';
const io = new Server(3000);
io.on('connection', (socket) => {
  socket.emit('welcome', 'Hello client!');
});
AThe server sends 'Hello client!' to the connected client immediately.
BThe server waits for the client to send a message before responding.
CThe server broadcasts 'Hello client!' to all connected clients.
DThe server closes the connection after sending a welcome message.
Attempts:
2 left
💡 Hint
Look at the 'socket.emit' inside the 'connection' event.
state_output
intermediate
2:00remaining
What is the output of this client-side Socket.io code?
Given this client code, what will be logged in the browser console?
Node.js
import { io } from 'socket.io-client';
const socket = io('http://localhost:3000');
socket.on('welcome', (msg) => {
  console.log(msg);
});
AAn error will occur because the server URL is missing the port.
BNothing will be logged because the event name is wrong.
C'welcome' will be logged instead of the message.
D'Hello client!' will be logged when the server sends it.
Attempts:
2 left
💡 Hint
Check the event name and server URL in the code.
📝 Syntax
advanced
2:00remaining
Which option correctly initializes a Socket.io server with CORS enabled?
You want to allow clients from any origin to connect. Which code snippet correctly sets up the server?
Aconst io = new Server(3000, { cors: { origin: '*' } });
Bconst io = new Server(3000, { cors: '*' });
Cconst io = new Server(3000, { cors: { origins: '*' } });
Dconst io = new Server(3000, { cors: { origin: true } });
Attempts:
2 left
💡 Hint
Check the correct property name for CORS origin in Socket.io options.
🔧 Debug
advanced
2:00remaining
Why does this Socket.io client fail to receive messages?
Client code: import { io } from 'socket.io-client'; const socket = io('http://localhost:3000'); socket.on('message', (data) => { console.log('Received:', data); }); Server code: import { Server } from 'socket.io'; const io = new Server(3000); io.on('connection', (socket) => { socket.emit('msg', 'Hello'); }); Why does the client never log 'Received: Hello'?
AThe server emits before the client connects, so the message is lost.
BThe client listens for 'message' but the server emits 'msg', so events don't match.
CThe client forgot to call socket.connect() explicitly.
DThe server is not running on port 3000.
Attempts:
2 left
💡 Hint
Check the event names used on client and server.
🧠 Conceptual
expert
3:00remaining
What is the main advantage of using Socket.io over plain WebSockets?
Choose the best explanation for why developers prefer Socket.io instead of using raw WebSocket APIs.
ASocket.io requires no server setup and works purely on the client side.
BSocket.io uses less bandwidth than WebSockets by compressing all messages automatically.
CSocket.io automatically handles reconnection, fallback transports, and event-based messaging, making real-time apps easier to build.
DSocket.io only works with HTTP/2, which is faster than WebSocket.
Attempts:
2 left
💡 Hint
Think about features Socket.io adds beyond basic WebSocket communication.