0
0
Node.jsframework~10 mins

Socket.io overview in Node.js - Interactive Code Practice

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

Complete the code to import the Socket.io library in a Node.js server.

Node.js
const io = require('[1]');
Drag options to blanks, or click blank then click option'
Asocket.io
Bexpress
Chttp
Dfs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'socket.io'
Using 'http' which is a different Node.js module
2fill in blank
medium

Complete the code to create a new Socket.io server attached to an HTTP server.

Node.js
const io = require('socket.io')( [1] );
Drag options to blanks, or click blank then click option'
Aapp
Bhttp
Csocket
Dserver
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the Express app instead of the HTTP server
Passing the Socket.io instance itself
3fill in blank
hard

Fix the error in the code to listen for new client connections.

Node.js
io.on('[1]', (socket) => {
  console.log('A user connected');
});
Drag options to blanks, or click blank then click option'
Aconnected
Bconnect
Cconnection
Dconnects
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connect' which is incorrect for Socket.io
Using 'connected' which is not an event name
4fill in blank
hard

Fill both blanks to emit a message to all connected clients except the sender.

Node.js
socket.[1].emit('[2]', 'Hello everyone except me');
Drag options to blanks, or click blank then click option'
Abroadcast
Bsend
Cmessage
Demit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'socket.emit' which sends only to the sender
Using 'socket.send' which is not a method on broadcast
5fill in blank
hard

Fill all three blanks to listen for a custom event and respond to the sender.

Node.js
socket.on('[1]', (data) => {
  console.log('Received:', data);
  socket.[2]('[3]', 'Got your message');
});
Drag options to blanks, or click blank then click option'
Achat message
Bemit
Creply
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'message' instead of 'chat message' as event name
Using 'broadcast.emit' instead of 'emit' to reply to sender