Sending messages to certain clients with

I recently started playing with and it’s really easy to get up and running. Just copy and paste their example and run the server. Open the page and open the console to see your messages being passed back and forth. I wanted to make a small chat app to play with sending messages to specific users, and it was quite difficult to find out how to do it so I decided to create a small demo app on Github.

I started with their example to prove that everything should work. It all worked fine, so I moved on to keeping track of individual clients. It took me ages to realise that events are bound per socket. Once I realised that, I started digging into the socket object and realised that is the important bit. Push it into an array and you have an array of socket objects that you can write to.

var clients = [];
io.sockets.on('connection', function (socket) {

You write to each socket using io.sockets.connected("topic", data), so I decided to send a different message to the first two users connected after five seconds:

    io.sockets.connected[clients[0]].emit("greeting", "Howdy, User 1!");
    io.sockets.connected[clients[1]].emit("greeting", "Hey there, User 2");

    // If you're using < 1.0, you need to use io.sockets.socket instead
    // io.sockets.socket(clients[0]).emit("greeting", "Howdy, User 1!");
}, 5000);

This worked fine (you can see the full commit here).

Finally, we needed to route messages to specific clients. I decided to use a username as the unique identifier for a socket, so store the data in an object like so:

var clients = {
    "mheap": {
        "socket": "1hu189dnfd"

This means that I can now use io.sockets.connected[clients.mheap.socket].emit() to send messages just to myself. As the connection is established instantly when the page loads, there's no way to add metadata to a socket connection on connect (without using socket.auth and a session, which I'll cover some other time). As a result, I ended up listening for a "add-user" message and creating the client when I received it:

socket.on('add-user', function(data){
    clients[data.username] = {

You can see the entire thing in the github repo. The frontend depends on a CDN version of jQuery and is horribly written, but it works for demonstrating client management with

If you want to give it a go, you can run the following commands:

$ git clone && cd socketio-chat-example && npm install && node app.js

Then open http://localhost:3000 in two browser windows, join the chat with a different name in each window and chat away

15 thoughts on “Sending messages to certain clients with

  1. article is good just wondering why you gave example of sending message to yourself with such long way long like


    you could use


    to send message to yourself directly. Or is there anything wrong with this?

    1. It depends what context you’re in, and what the value of `socket` is. If you can guarantee that `socket` is the one you definitely want to write to, `socket.emit()` is fine. Personally, I prefer the lookup as it’s more explicit

  2. Hi there!

    I´m just starting with and I was wondering why you didn´t use namespaces or rooms instead of what you explain on your post.

    Would we achieve the same result using rooms or namespaces that using your method? I guess yes.

    Thanks! btw good post :)

      1. I will be using rooms in my WebRTC project, for chat and an abstract channel for signaling. Even with rooms, knowing how to create rooms, I want to use those as ‘actual’ rooms (ie: not just 2 clients(usually)) therefore, and my point is…. I still found this part on sending to a specific socket extremely useful, its precisely what i was googling for.

        The reason I still need this even with namespaces, is I am using it for RTC video Signaling as well as chat on(“chat message”) vs on(“vChatInvite”) etc..

        Currently, every client joins the default namespace, which I am calling the ‘lobby’ but If they want to video chat each other, I want them to be able to do it without even myself (or especially the user) worrying about what room they are in, or putting them in a different room before starting the signaling. Therefore I needed a way in a room with Alice, bob, Jim and Gilbert for Alice to chat with jim and gilbert while dancing for tips with bob. Its actually for an educational site but you get the idea. Long story short… THANKS for this useful info, even with rooms, its needed.

  3. did not work with new version of I got an error at io.sockets.on(‘connection’, …) -> “Cannot call method ‘on’ of undefined”. I dont know why

  4. Great post man. I’m from Brazil and I’m starting with and i have a question. When the browser refreshes, the socket is removed and a new socket is created. Is there no problem with this?

  5. Hello Michael. Thank you for your post – it is very useful. It’ll be great if you write a post about using rooms functionality or even rewrite this chat application with using of rooms.


  6. Now you can add metadata to a socket connection on connect (without using socket.auth and a session)
    iosocket = io.connect(‘//Host:Port/NameSpaceorBlank’, {query: ‘param1=1245&param2=something’} );

    on Server you can read params from
    socket.handshake.query object

  7. I kept thinking heap was some sort of data structure you were referring to.Only to find out that it was your last name. Haha.. Great article, helped me so much on my WebRTC project. Thank You so much.

Leave a Reply