How to send a message to a particular client with

I'm starting with + node.js, I know how to send a message locally and to broadcast socket.broadcast.emit() function:- all the connected clients receive the same message.

Now, I would like to know how to send a private message to a particular client, I mean one socket for a private chat between 2 person (Client-To-Client stream). Thanks.

When a user connects, it should send a message to the server with a username which has to be unique, like an email.

A pair of username and socket should be stored in an object like this:

var users = {
    '[email protected]': [socket object],
    '[email protected]': [socket object],
    '[email protected]': [socket object]

On the client, emit an object to the server with the following data:

    to:[the other receiver's username as a string],
    from:[the person who sent the message as string],
    message:[the message to be sent as string]

On the server, listen for messages. When a message is received, emit the data to the receiver.

users[].emit('receivedMessage', data)

On the client, listen for emits from the server called 'receivedMessage', and by reading the data you can handle who it came from and the message that was sent.

You can use rooms. From the client side emit an event ("join" in this case, can be anything) with any unique identifier (email, id).

Client Side:

var socket = io.connect('http://localhost');
socket.emit('join', {email: [email protected]});

Now, from the server side use that information to create an unique room for that user

Server Side:

var io = require('').listen(80);

io.sockets.on('connection', function (socket) {
  socket.on('join', function (data) {
    socket.join(; // We are using room of socket io

So, now every user has joined a room named after user's email. So if you want to send a specific user a message you just have to

Server Side:'[email protected]').emit('new_msg', {msg: 'hello'});

The last thing left to do on the client side is listen to the "new_msg" event.

Client Side:

socket.on("new_msg", function(data) {

I hope you get the idea.

SURE: Simply,

This is what you need :"event", data);

whenever a user joined to the server, socket details will be generated including ID. This is the ID really helps to send a message to particular people.

first we need to store all the socket.ids in array,

var people={};

people[name] =;

here name is the receiver name. Example:


So, now we can get that with the receiver name whenever we are sending message:

for this we need to know the receivername. You need to emit receiver name to the server.

final thing is:

 socket.on('chat message', function(data){[data.receiver]).emit('chat message', data.msg);

Hope this works well for you.

Good Luck!!

You can refer to rooms. When you handshaked socket - you can join him to named room, for instance "user.#{userid}".

After that, you can send private message to any client by convenient name, for instance:'user.125').emit('new_message', {text: "Hello world"})

In operation above we send "new_message" to user "125".


In a project of our company we are using "rooms" approach and it's name is a combination of user ids of all users in a conversation as a unique identifier (our implementation is more like facebook messenger), example:

|id | name |1 | Scott |2 | Susan

"room" name will be "1-2" (ids are ordered Asc.) and on disconnect automatically cleans up the room

this way you send messages just to that room and only to online (connected) users (less packages sent throughout the server).

Let me make it simpler with rooms. request a server with a unique identifier to join a server. here we are using an email as a unique identifier.


socket.on('connect', function () {
  socket.emit('join', {email: [email protected]});

When the user joined a server, create a room for that user


io.on('connection', function (socket) {
   socket.on('join', function (data) {    

Now we are all set with joining. let emit something to from the server to room, so that user can listen.

Server'[email protected]').emit('message', {msg: 'hello world.'});

Let finalize the topic with listening to message event to the client side

socket.on("message", function(data) {

The reference from rooms