socket.io private message

Solution 1:

No tutorial needed. The Socket.IO FAQ is pretty straightforward on this one:

socket.emit('news', { hello: 'world' });

EDIT: Folks are linking to this question when asking about how to get that socket object later. There is no need to. When a new client connects, save a reference to that socket on whatever object you're keeping your user information on. My comment from below:

In the top of your script somewhere, setup an object to hold your users' information.

var connectedUsers = {};

In your .on('connection') function, add that socket to your new object. connectedUsers[USER_NAME_HERE] = socket; Then you can easily retrieve it later. connectedUsers[USER_NAME_HERE].emit('something', 'something');

Solution 2:

Here's a code snippet that should help:

Client-side (sending message)

socket.emit("private", { msg: chatMsg.val(), to: selected.text() });

where to refers to the id to send a private message to and msg is the content.

Client-side (receiving message)

socket.on("private", function(data) {   
   chatLog.append('<li class="private"><em><strong>'+ data.from +' -> '+ data.to +'</strong>: '+ data.msg +'</em></li>');
});

where chatLog is a div displaying the chat messages.

Server-side

client.on("private", function(data) {       
    io.sockets.sockets[data.to].emit("private", { from: client.id, to: data.to, msg: data.msg });
    client.emit("private", { from: client.id, to: data.to, msg: data.msg });
});

Solution 3:

Although we have nice answers here. However, I couldn't grasp the whole client server unique user identification pretty fast, so I'm posting this simple steps in order to help whoever is struggling as i did.....

At the client side, Get the user's ID, in my case I'm getting the username...

Client side user registration

//Connect socket.io     
var systemUrl = 'http://localhost:4000';
var socket = io.connect(systemUrl);

//Collect User identity from the client side
var username = prompt('Enter your Username');
socket.emit('register',username);

The Listen to register on the server side to register user's socket to connected socket

Serve side code User registration

/*Craete an empty object to collect connected users*/
var connectedUsers = {};

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

/*Register connected user*/
    socket.on('register',function(username){
        socket.username = username;
        connectedUsers[username] = socket;
    });
});

Send Message from the client side

$(document).on('click','.username',function(){
    var username = $(this).text(),
        message = prompt("type your message");

    socket.emit('private_chat',{
        to : username,
        message : message
    });
});

Receive message on server and emit it to private user

/*Private chat*/
socket.on('private_chat',function(data){
    const to = data.to,
            message = data.message;

    if(connectedUsers.hasOwnProperty(to)){
        connectedUsers[to].emit('private_chat',{
            //The sender's username
            username : socket.username,

            //Message sent to receiver
            message : message
        });
    }

}); 

Receive message on client and display it

/*Received private messages*/
socket.on('private_chat',function(data){
    var username = data.username;
    var message = data.message;

    alert(username+': '+message);
});

This is not the best, however you can start from here....