Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to send a message to all connected clients using Socket.IO.
Node.js
io.[1]('message', 'Hello clients!');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'emit' on the server instance.
Trying to use 'broadcast' directly on io.
✗ Incorrect
The emit method sends an event to all connected clients.
2fill in blank
mediumComplete the code to broadcast a message to all clients except the sender.
Node.js
socket.[1].emit('message', 'Hello others!');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
emit directly on socket sends only to sender.Using
to without specifying a room.✗ Incorrect
The broadcast property on the socket sends to all except the sender.
3fill in blank
hardFix the error in broadcasting a message to a specific room.
Node.js
io.to([1]).emit('message', 'Hello room!');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing
socket.id instead of room name.Using
broadcast instead of to().✗ Incorrect
The to method requires the room name as a string.
4fill in blank
hardFill both blanks to broadcast a message to a room except the sender.
Node.js
socket.[1].to([2]).emit('message', 'Hello others in room!');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
emit instead of broadcast.Passing
socket.id instead of room name.✗ Incorrect
Use broadcast to exclude sender and to('room1') to target the room.
5fill in blank
hardFill all three blanks to send a private message to a specific client.
Node.js
io.sockets.sockets.get([1]).[2]('private', [3]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
broadcast instead of emit.Passing a string instead of an object as message.
✗ Incorrect
Get the socket by ID, then emit the event with the message object.