0
0
Expressframework~20 mins

Socket.io integration with Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Socket.io Express Master
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 client connects?

Given this Express and Socket.io server setup, what will the server log when a client connects?

Express
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');

const app = express();
const server = http.createServer(app);
const io = new Server(server);

io.on('connection', (socket) => {
  console.log('A user connected');
  socket.on('disconnect', () => {
    console.log('A user disconnected');
  });
});

server.listen(3000);
AThe server logs 'connection error' when a client connects.
BThe server logs 'A user disconnected' immediately when a client connects.
CThe server logs nothing when a client connects.
DThe server logs 'A user connected' immediately when a client connects.
Attempts:
2 left
💡 Hint

Look at the event name inside io.on() and what it logs.

📝 Syntax
intermediate
2:00remaining
Which option correctly initializes Socket.io with Express?

Choose the correct way to integrate Socket.io with an Express app.

Aconst io = new Server(server); // where server is created by http.createServer(app)
Bconst io = new Server(app);
Cconst io = new Server(http.createServer(app));
Dconst io = new Server(); io.attach(app);
Attempts:
2 left
💡 Hint

Socket.io needs the HTTP server, not just the Express app.

state_output
advanced
2:00remaining
What is the value of 'count' after 3 clients connect and disconnect?

Consider this code snippet:

let count = 0;
io.on('connection', (socket) => {
  count++;
  socket.on('disconnect', () => {
    count--;
  });
});

What is the value of count after 3 clients connect and then all disconnect?

A3
B0
C-3
D1
Attempts:
2 left
💡 Hint

Think about how count changes on connection and disconnection.

🔧 Debug
advanced
2:00remaining
Why does this Socket.io server not receive client messages?

Review this server code snippet:

io.on('connection', (socket) => {
  socket.on('message', (msg) => {
    console.log('Message:', msg);
  });
});

Clients send messages with socket.emit('msg', 'hello'). Why does the server not log the messages?

AThe client must use <code>socket.on</code> to send messages.
BThe server must use <code>io.emit</code> to receive messages.
CThe server listens for 'message' but clients emit 'msg', so events don't match.
DThe server needs to call <code>socket.connect()</code> before listening.
Attempts:
2 left
💡 Hint

Check the event names used by client and server.

🧠 Conceptual
expert
3:00remaining
Which best describes how Socket.io manages real-time communication with Express?

Choose the statement that best explains Socket.io's integration with Express for real-time features.

ASocket.io attaches to the existing HTTP server created from Express to handle WebSocket and fallback transports alongside HTTP requests.
BSocket.io creates a separate HTTP server and proxies Express requests through it.
CSocket.io replaces Express's routing system to manage all client-server communication.
DSocket.io runs as a standalone server and requires clients to connect to a different port than Express.
Attempts:
2 left
💡 Hint

Think about how Socket.io works with the HTTP server Express uses.