Skip to content
tezvyn:
Menu

Socket.IO emit methods: socket, io, broadcast differences?

Source: interviewEasyHow cards are made

Summary

Socket.IO API design and message scope understanding.

Key points

socket.emit() sends to one client, broadcast.emit() to all except sender, io.emit() to all.

SOCKET.EMIT FOR SINGLE CLIENT

socket.emit('eventName', data) sends a message to only that one client. This is unicast. Use this for server responses: Acknowledging a button click, confirming a login, sending private notifications to a specific user. Example: Server receives a user's profile update request, validates it, then socket.emit() a confirmation only to that user.

IO.EMIT FOR ALL CLIENTS

io.emit('eventName', data) broadcasts to every connected socket in the namespace. This is one-to-all. Use this for global state changes that affect everyone: A new user joined the system, a product went out of stock, a scheduled announcement. Example: Admin triggers a system maintenance alert, all 10000 connected users receive it simultaneously via io.emit().

BROADCAST.EMIT FOR EVERYONE EXCEPT SENDER

socket.broadcast.emit('eventName', data) sends to all sockets in the namespace except the one that triggered it. This is selective one-to-many. Use this for social notifications: User posts a comment, all other users in the thread see it, but the poster themselves already know they sent it. Example: In a multiplayer game, one player moves; broadcast.emit() tells all other players the new position, but not back to the mover.

Concrete scenarios

A chat room has users Alice, Bob, Charlie. Alice types and sends a message. Use socket.emit() to send a confirmation to Alice immediately (local echo). Use broadcast.emit() to notify Bob and Charlie of the new message. If a server maintenance window opens, use io.emit() so all users globally see the warning, regardless of which chat they are in.

Common pitfall

Developers sometimes use io.emit() for notifications that should only go to specific users, flooding the system. Or they forget broadcast.emit() exists and use socket.emit() in a loop for each recipient, which is less efficient. Understanding the three patterns prevents both errors.

Interview question

In a multiplayer game, when a player moves their character, which Socket.IO method should the server use to notify other players of the movement?

  • a.socket.broadcast.emit(), so all players except the mover see the new position.Correct
  • b.socket.emit() called in a loop for each other player's socket.
  • c.io.emit(), so every connected player globally receives the movement.
  • d.socket.emit(), so only that player sees their own position.
Why?

The moving player already knows their own position. broadcast.emit() sends the update to everyone else without redundantly notifying the mover. io.emit() would broadcast to unrelated game lobbies. Looping socket.emit() is inefficient compared to a single broadcast.

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

Read the original → socket.io

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

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