📖 Scenario: You are building a simple Express server that listens for client connections. You want to handle connection events to log when a client connects and disconnects.
🎯 Goal: Create an Express server that listens on port 3000 and logs messages when clients connect and disconnect.
📋 What You'll Learn
Create an Express app instance
Set up a server to listen on port 3000
Handle the 'connection' event on the server
Log a message when a client connects
Log a message when a client disconnects
💡 Why This Matters
🌍 Real World
Servers often need to know when clients connect or disconnect to manage resources or log activity.
💼 Career
Understanding connection events is important for backend developers working with network servers and real-time applications.
Progress0 / 4 steps
1
Create Express app and HTTP server
Create a variable called express that requires the 'express' module. Then create a variable called app by calling express(). Finally, create a variable called server by requiring the 'http' module and calling createServer(app).
Express
Hint
Use require('express') to import Express. Then call express() to create the app. Use require('http') and createServer(app) to create the server.
2
Set the server to listen on port 3000
Add a line to make the server listen on port 3000 by calling server.listen(3000).
Express
Hint
Use server.listen(3000) to start the server on port 3000.
3
Handle the 'connection' event on the server
Add an event listener on server for the 'connection' event using server.on('connection', (socket) => { ... }). Inside the callback, log the message 'A client connected'.
Express
Hint
Use server.on('connection', (socket) => { ... }) to listen for new connections and log a message inside the callback.
4
Log when a client disconnects
Inside the 'connection' event callback, add a listener on socket for the 'close' event using socket.on('close', () => { ... }). Inside this callback, log the message 'A client disconnected'.
Express
Hint
Inside the connection callback, use socket.on('close', () => { ... }) to detect when the client disconnects and log a message.
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
Step 1: Understand the event name 'connection'
The 'connection' event triggers when a new client connects to the server.
Step 2: Understand the role of the callback
The callback runs code to handle the new connection, such as setting up listeners.
Final Answer:
Runs the callback when a new user connects to the server -> Option C
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
Step 1: Identify the object to listen on
The disconnect event is listened on the individual socket, not the io server object.
Step 2: Use the correct method and event name
Use socket.on('disconnect', callback) to run code when that socket disconnects.
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
Step 1: Understand broadcast
socket.broadcast.emit sends to all except the current socket.
Step 2: Compare options
io.emit sends to all including current socket; socket.emit sends only to current socket; socket.on listens, not sends.
Final Answer:
socket.broadcast.emit('message', 'A new user joined') -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.