Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to send a message to all clients in a specific room.
Express
io.to('[1]').emit('message', 'Hello room!');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'socket' or 'client' instead of the room name.
Using 'broadcast' which sends to all except the sender.
✗ Incorrect
The to method targets a specific room by its name, here 'room1'.
2fill in blank
mediumComplete the code to join a client socket to a room named 'chatroom'.
Express
socket.[1]('chatroom');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'leave' which removes the socket from a room.
Using 'emit' which sends messages but does not join rooms.
✗ Incorrect
The join method adds the socket to the specified room.
3fill in blank
hardFix the error in broadcasting a message to all clients except the sender.
Express
socket.[1].emit('message', 'Hello everyone!');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'to' which targets a specific room.
Using 'emit' directly which sends only to the sender.
✗ Incorrect
The broadcast property sends to all clients except the sender.
4fill in blank
hardFill both blanks to broadcast a message to a specific room excluding the sender.
Express
socket.[1].to('[2]').emit('message', 'Hi room!');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'join' instead of 'broadcast' for the first blank.
Using 'emit' as a property instead of a method.
✗ Incorrect
Use socket.broadcast.to('room1') to send to all in 'room1' except the sender.
5fill in blank
hardFill all three blanks to create a dictionary of room names and their client counts.
Express
const counts = { [1]: await io.in('[2]').allSockets().then(sockets => sockets.size), [3]: 0 }; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing room names between keys and values.
Using non-string keys or values.
✗ Incorrect
This code counts clients in 'roomA' and initializes 'roomB' count to zero.