0
0
Node.jsframework~10 mins

Room and namespace concepts 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 create a new namespace using Socket.IO.

Node.js
const io = require('socket.io')(server);
const chatNamespace = io.of('[1]');
Drag options to blanks, or click blank then click option'
A/chat
Bchat
Cnamespace
D/room
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the leading slash in the namespace name.
Using a room name instead of a namespace.
2fill in blank
medium

Complete the code to join a client socket to a room named 'lobby'.

Node.js
io.on('connection', (socket) => {
  socket.[1]('lobby');
});
Drag options to blanks, or click blank then click option'
AconnectRoom
BjoinRoom
Center
Djoin
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like 'joinRoom' or 'enter'.
Trying to use 'connectRoom' which does not exist.
3fill in blank
hard

Fix the error in broadcasting a message to all clients in a room 'game'.

Node.js
io.to('game').[1]('start', { msg: 'Game begins!' });
Drag options to blanks, or click blank then click option'
AemitToRoom
Bbroadcast
Cemit
DsendToRoom
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'emitToRoom' or 'sendToRoom'.
Using 'broadcast' incorrectly here.
4fill in blank
hard

Fill both blanks to create a namespace '/chat' and join a room 'general' inside it.

Node.js
const chat = io.of('[1]');
chat.on('connection', (socket) => {
  socket.[2]('general');
});
Drag options to blanks, or click blank then click option'
A/chat
Bjoin
Cconnect
D/general
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connect' instead of 'join' to enter a room.
Omitting the slash in the namespace name.
5fill in blank
hard

Fill all three blanks to emit a message 'hello' to all clients in room 'room1' inside namespace '/news'.

Node.js
const news = io.of('[1]');
news.to('[2]').[3]('message', 'hello');
Drag options to blanks, or click blank then click option'
A/news
Broom1
Cemit
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'emit' to send events.
Omitting the slash in the namespace name.