Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the leading slash in the namespace name.
Using a room name instead of a namespace.
✗ Incorrect
Namespaces in Socket.IO start with a slash, like /chat.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like 'joinRoom' or 'enter'.
Trying to use 'connectRoom' which does not exist.
✗ Incorrect
The method to join a room is join.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'emitToRoom' or 'sendToRoom'.
Using 'broadcast' incorrectly here.
✗ Incorrect
The correct method to send an event is emit.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connect' instead of 'join' to enter a room.
Omitting the slash in the namespace name.
✗ Incorrect
Namespaces start with a slash, so '/chat' is correct. To join a room, use join.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'emit' to send events.
Omitting the slash in the namespace name.
✗ Incorrect
Namespace is '/news', room is 'room1', and the method to send is 'emit'.