Bird
Raised Fist0
Expressframework~10 mins

Handling connection events in Express - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to listen for new client connections on the server.

Express
const express = require('express');
const app = express();
const server = app.listen(3000);
server.on('[1]', (socket) => {
  console.log('A client connected');
});
Drag options to blanks, or click blank then click option'
Aconnecting
Bconnected
Cconnection
Dconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connect' instead of 'connection' causes the event not to fire.
2fill in blank
medium

Complete the code to handle when a client disconnects.

Express
server.on('connection', (socket) => {
  socket.on('[1]', () => {
    console.log('Client disconnected');
  });
});
Drag options to blanks, or click blank then click option'
Adisconnect
Bclose
Cend
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'disconnect' instead of 'close' will not catch client disconnects.
3fill in blank
hard

Fix the error in the code to properly handle data received from a client.

Express
server.on('connection', (socket) => {
  socket.on('[1]', (data) => {
    console.log('Received:', data.toString());
  });
});
Drag options to blanks, or click blank then click option'
Adata
Binput
Cmessage
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'message' causes no data event to be caught on a raw socket.
4fill in blank
hard

Fill both blanks to create a server that logs when a client connects and disconnects.

Express
const net = require('net');
const server = net.createServer((socket) => {
  console.log('Client [1]');
  socket.on('[2]', () => {
    console.log('Client disconnected');
  });
});
server.listen(8080);
Drag options to blanks, or click blank then click option'
Aconnected
Bconnect
Cclose
Dconnection
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connect' event on socket instead of 'close' for disconnection.
5fill in blank
hard

Fill all three blanks to handle connection, data reception, and disconnection events properly.

Express
const net = require('net');
const server = net.createServer();
server.on('[1]', (socket) => {
  console.log('Client connected');
  socket.on('[2]', (data) => {
    console.log('Data:', data.toString());
  });
  socket.on('[3]', () => {
    console.log('Client disconnected');
  });
});
server.listen(9000);
Drag options to blanks, or click blank then click option'
Aconnection
Bdata
Cdisconnect
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'disconnect' instead of 'close' for client disconnection event.

Practice

(1/5)
1. What does io.on('connection', callback) do in an Express app using Socket.IO?
easy
A. Runs the callback when a user disconnects
B. Runs the callback when the server starts
C. Runs the callback when a new user connects to the server
D. Runs the callback when the server shuts down

Solution

  1. Step 1: Understand the event name 'connection'

    The 'connection' event triggers when a new client connects to the server.
  2. Step 2: Understand the role of the callback

    The callback runs code to handle the new connection, such as setting up listeners.
  3. Final Answer:

    Runs the callback when a new user connects to the server -> Option C
  4. Quick Check:

    io.on('connection') = new user connected [OK]
Hint: Remember 'connection' means user joined [OK]
Common Mistakes:
  • Confusing 'connection' with 'disconnect'
  • Thinking it runs on server start
  • Assuming it runs on server shutdown
2. Which of the following is the correct syntax to listen for a user disconnect event on a socket?
easy
A. io.on('disconnect', () => { /* code */ })
B. socket.on('disconnect', () => { /* code */ })
C. socket.emit('disconnect', () => { /* code */ })
D. io.emit('disconnect', () => { /* code */ })

Solution

  1. Step 1: Identify the object to listen on

    The disconnect event is listened on the individual socket, not the io server object.
  2. Step 2: Use the correct method and event name

    Use socket.on('disconnect', callback) to run code when that socket disconnects.
  3. Final Answer:

    socket.on('disconnect', () => { /* code */ }) -> Option B
  4. Quick Check:

    socket.on('disconnect') = user left [OK]
Hint: Listen on socket, not io, for disconnect [OK]
Common Mistakes:
  • Using io.on instead of socket.on for disconnect
  • Using emit instead of on to listen
  • Listening disconnect on wrong object
3. What will be logged when this code runs and a user connects then disconnects?
io.on('connection', (socket) => {
  console.log('User connected');
  socket.on('disconnect', () => {
    console.log('User disconnected');
  });
});
medium
A. User connected
User disconnected
B. User disconnected
User connected
C. User connected
D. User disconnected

Solution

  1. Step 1: When user connects

    The 'connection' event triggers, logging 'User connected'.
  2. Step 2: When user disconnects

    The socket's 'disconnect' event triggers, logging 'User disconnected'.
  3. Final Answer:

    User connected
    User disconnected
    -> Option A
  4. Quick Check:

    Connect then disconnect logs both messages [OK]
Hint: Connect logs first, disconnect logs second [OK]
Common Mistakes:
  • Assuming disconnect logs before connect
  • Missing that disconnect logs separately
  • Thinking only one message logs
4. Identify the error in this code snippet that tries to log when a user disconnects:
io.on('connection', (socket) => {
  socket.on('disconnect', () => {
    console.log('User disconnected');
  });
});

socket.on('disconnect', () => {
  console.log('Another disconnect');
});
medium
A. The second socket.on('disconnect') is outside the connection handler and socket is undefined
B. The first socket.on('disconnect') should be io.on('disconnect')
C. The disconnect event should be 'disconnected' not 'disconnect'
D. The console.log statements are missing semicolons

Solution

  1. Step 1: Check where socket is defined

    Socket is only defined inside the connection callback, so outside it socket is undefined.
  2. Step 2: Identify the error

    The second socket.on('disconnect') is outside the connection handler, causing a runtime error.
  3. Final Answer:

    The second socket.on('disconnect') is outside the connection handler and socket is undefined -> Option A
  4. Quick Check:

    socket undefined outside connection callback causes error [OK]
Hint: Use socket only inside connection callback [OK]
Common Mistakes:
  • Trying to use socket outside connection event
  • Confusing event names 'disconnect' vs 'disconnected'
  • Ignoring scope errors
5. You want to broadcast a message to all connected users except the one who just connected. Which code inside io.on('connection', (socket) => { ... }) correctly does this when a user connects?
hard
A. socket.on('message', 'A new user joined')
B. io.emit('message', 'A new user joined')
C. socket.emit('message', 'A new user joined')
D. socket.broadcast.emit('message', 'A new user joined')

Solution

  1. Step 1: Understand broadcast

    socket.broadcast.emit sends to all except the current socket.
  2. Step 2: Compare options

    io.emit sends to all including current socket; socket.emit sends only to current socket; socket.on listens, not sends.
  3. Final Answer:

    socket.broadcast.emit('message', 'A new user joined') -> Option D
  4. Quick Check:

    Broadcast excludes sender, perfect for this case [OK]
Hint: Use socket.broadcast.emit to exclude sender [OK]
Common Mistakes:
  • Using io.emit which sends to sender too
  • Using socket.emit which sends only to sender
  • Confusing emit with on
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