Skip to content
tezvyn:

Socket.IO: Broadcasting Events to Clients

Source: socket.ioMediumHow cards are made

Socket.IO: Broadcasting Events to Clients

Broadcasting sends a server-side event to multiple clients at once, like a public announcement system. Use it for live notifications or game state updates. The footgun: by default, it only reaches clients on the same server; use an adapter for multi-server…

Why it exists

Broadcasting solves the problem of efficiently sending a single piece of information from the server to many connected clients at once. Instead of manually iterating through every client, you can send one message that the server relays to all, which is fundamental for real-time, multi-user applications.

The mental model

Think of broadcasting as a radio tower. The server is the station, and all connected clients are radios tuned in. Using io.emit() is a public service announcement for everyone listening. Using socket.broadcast.emit() is like when one listener calls into the show, and the station plays their comment for everyone else to hear.

How it works

Broadcasting is a server-only feature. The two main methods are io.emit("event", data), which sends to every connected client, and socket.broadcast.emit("event", data), which sends to every client except the one represented by the socket object. You can also request acknowledgements from clients using io.timeout(ms).emit(...), which provides a callback with their responses or an error if some don't reply in time.

When to use it

Use broadcasting for information relevant to all users or all users except the sender. This is perfect for sending a new chat message to a public room, pushing a live score update in a sports app, or broadcasting a system-wide maintenance notification.

When not to use it

Don't use global broadcasting when you need to target a specific subset of users; for that, group clients into "Rooms" and emit to the room instead. Also, remember that broadcasting doesn't guarantee delivery to disconnected clients. If you need message persistence, you must implement it separately, for example in a database. Finally, for multi-server deployments, broadcasting requires a special adapter (like the Redis Adapter) to sync events across all nodes; otherwise, events are confined to a single server instance.

One canonical example

To send a "user-connected" event to all existing clients when a new one joins (but not to the new user themselves), you would use socket.broadcast.emit:

io.on("connection", (socket) => {
socket.broadcast.emit("user-connected", { userId: socket.id });
});

This tells everyone else, "A new user just joined."

Interview question

A new user connects to a chat application. Which Socket.IO method should be used to notify *all existing users* (but not the new user) about this event?

  • a.socket.emit("user-joined", data)
  • b.io.emit("user-joined", data)
  • c.socket.broadcast.emit("user-joined", data)Correct
  • d.io.to("room").emit("user-joined", data)
Why?

socket.broadcast.emit() sends an event to all connected clients except the one represented by the socket object, which perfectly fits notifying existing users without including the new sender. io.emit() would incorrectly send the event to the new user as well.

Just read this? Test yourself on what you have been reading.

Read the original → socket.io

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles