Broadcasting and Message Recipients
🎯 Introduction
Previously, when User A sent a message, only A received the reply. In a chat room, User B also needs to see it, while some notifications should go only to other users. Socket.IO provides several ways to send events. This article uses two clients to verify the recipients of each approach.
🧱 What Do io and socket Represent on the Server?
The server's io is the Socket.IO server instance and can send messages to multiple connections it manages. The socket in the connection callback represents the current connection and identifies the current sender.
| Server-side method | Recipients when User A sends | Typical use |
|---|---|---|
socket.emit(...) | A's current connection | Private results and individual error messages |
io.emit(...) | A and all other online connections | Public chat messages |
socket.broadcast.emit(...) | All connections except A's current connection | Telling others that someone is typing |
The exclusion applies to a connection. If the same person opens another client, that separate connection can still receive the broadcast. Broadcasting events
🛠 Turn the Reply into Public Chat
In the previous server-side chat:send listener, replace socket.emit('chat:message', text.trim()) with this line, keeping validation and acknowledgement code.
io.emit('chat:message', text.trim());
Restart the server and run node client.mjs in two terminals. Treat one as User A and the other as User B. When A sends “Hello everyone”, both should print “Received Hello everyone”. Only A prints “Accepted by server”, because the acknowledgement callback belongs to A's specific request.
Adding a message to a list both when Send is clicked and when its broadcast arrives creates duplicates. Our client prints messages only in the chat:message listener, so the sender also sees just one copy.
Notify Only Other Clients
Temporarily replace the same line with the following code, restart the server, and let both clients reconnect.
socket.broadcast.emit('chat:message', text.trim());
When A sends, B receives the text and A receives only the acknowledgement. After comparing the behavior, restore io.emit. The following articles build on this public chat room.
🧰 Include Sender Information
Currently, there is no way to tell who sent each message. Start by identifying the source using its connection ID. Change the server's broadcast data to an object.
io.emit('chat:message', {
senderId: socket.id,
text: text.trim()
});
Replace the client's original message listener with the following function.
socket.on('chat:message', (message) => {
console.log(`[${message.senderId}] ${message.text}`);
});
The object carries both the source and the text. A connection ID is useful for this experiment but usually changes after reconnecting. The authentication article replaces it with a stable application user ID.
❓ Questions and Answers
Q1: Can a broadcast reach an offline client?
By default, broadcasts target connections that are currently online. The application must save messages missed during a disconnection and retrieve them when the client returns.
Q2: Are broadcast acknowledgements the same as one-to-one acknowledgements?
The earlier acknowledgement has only one handler. Requiring acknowledgements from every broadcast recipient involves multiple responses and potentially some timeouts. Our chat room only acknowledges that the server accepted the submission, then broadcasts the message separately, making the application behavior easier to verify.
🧪 Exercise
Start three clients. Have User A send the same text while testing each of the three sending methods in turn. Record the number of recipients: it should be 1, 3, and 2 respectively. Restart the server after each change, then restore io.emit with the object containing senderId.
🎉 You can now choose a sending method based on the intended recipients.
