Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 36 ยท Connections and Events

Broadcasting to Many Clients

Learn every way to send a Socket.IO event to more than one client. Compare io.emit, socket.emit, socket.broadcast.emit, except, volatile and compress, with a chat and typing-indicator example.

One client talking to one server is not very interesting. The power of Socket.IO shows when a single event on the server reaches many clients at once: a chat message to everyone in the conversation, a new score to every viewer, a "typing..." hint to everyone but the typist. This lesson lists the sending options and when to use each.

Who gets the message?

Imagine three connected clients: Ana, Ben and Cleo. Ana sends a message and the server handles it in a socket.on handler where socket is Ana's socket. The server's choices:

Call Receivers
socket.emit('x', data) only Ana (the sender)
socket.broadcast.emit('x', data) everyone except Ana
io.emit('x', data) everyone, including Ana
io.except(ana.id).emit('x', data) everyone except Ana (same as broadcast, by id)
io.to(ben.id).emit('x', data) only Ben (every socket is in a room named after its id)

The word broadcast in Socket.IO always means "to all other sockets"; socket.broadcast carries that exclusion with it. Rooms (next chapter) add io.to('room') to narrow the audience. The sender is the socket you call it on, which is why socket.broadcast makes sense inside a handler and io.emit works anywhere, even outside any connection (a timer, an HTTP route, a queue consumer).

Trying all five

Node.js Who receives what

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Read the output against the table: socket.emit reaches only Ana, socket.broadcast.emit and io.except reach Ben and Cleo, and io.emit reaches all three.

Broadcasting from outside a handler

You do not need a socket to broadcast. Anything that has the io object can push data, which is how many production systems work: an HTTP webhook arrives and the server forwards a notification to all dashboards; a cron job emits a minute tick; a database listener announces a changed row.

JavaScript
// Express route (read-only example)
app.post('/webhooks/payment', (req, res) => {
  io.emit('payment:received', { id: req.body.id, amount: req.body.amount });
  res.sendStatus(204);
});

// timer
setInterval(() => io.emit('server:time', Date.now()), 1000);

A chat with a typing indicator

A realistic chat uses two different audiences: the message goes to everyone (so the sender also sees it confirmed in the shared list), while "is typing" goes to everyone but the typist, because it would be silly to tell Ana that Ana is typing.

Node.js Chat messages and typing hints

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Ana is told when Ben joins (nobody was there to hear about Ana), each person sees the other typing but never themselves, and every message appears for both people.

Options that change how events travel

Two modifiers are worth knowing (read-only snippets):

JavaScript
// volatile: may be dropped if the client is not ready; ideal for streams where only the latest value matters
socket.volatile.emit('cursor', { x: 120, y: 80 });

// compress: toggle per-message compression for the WebSocket transport
socket.compress(false).emit('already-compressed', buffer);

volatile fits mouse positions, live positions in a game, or a sensor reading: if one is lost, the next one arrives a moment later. Do not use it for chat messages or payments.

There is also io.local.emit(...), which only reaches clients connected to the current server process when you run several processes (see the scaling chapter), and io.timeout(ms).emit(...) to collect acknowledgements from many clients.

Broadcasting is a server-side concept. A client cannot say "send this to everyone else". It emits to the server, and your server handler decides who receives what. That is a feature: it is where validation, permissions and filtering belong.

Performance thoughts

io.emit serializes the event once and writes it to every socket, which is efficient, but the cost still grows with the number of clients. Keep payloads small, send deltas rather than whole states when you can, and use rooms so a message reaches only those who need it. Throttle high-frequency events (cursor moves at 60 per second per user adds up quickly) by batching them on the server every 50 to 100 ms.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Inside a handler for Ana's socket, which call sends to everyone EXCEPT Ana?

    socket.broadcast excludes the socket it is called on. io.to(socket.id) would reach only Ana.
  2. 2. Can the server broadcast without any client having sent an event first?

    io.emit does not depend on a socket, so timers, webhooks and queue consumers can push to clients.
  3. 3. Which events suit socket.volatile.emit?

    Volatile events may be dropped; that is fine when the next update replaces the lost one.
  4. 4. Why is 'X is typing' sent with broadcast instead of io.emit?

    Broadcast excludes the sender, which is exactly the desired audience for a typing hint.

Summary

  • socket.emit reaches one client, socket.broadcast.emit everyone else, io.emit everyone, io.to(id) a specific socket.
  • Broadcasting is decided by the server; clients never address other clients directly.
  • Any code with the io object can broadcast, including timers and HTTP routes.
  • volatile allows dropping stale updates; batching and small payloads keep large broadcasts cheap.
  • Rooms narrow the audience further, which is the next chapter's topic.

Next: Designing Events and Payloads