How to send real time notification to single user using node.js and PHP

I am trying to integrate real time notifications with Node and socket.io in a Symfony Application. I have read a lot of information about this topic and have a working Node application.

nodeClient.js

var socket = io.connect( 'http://192.168.15.106:8080' );


$('a.sendSmile').click(function(){
    socket.emit( 'message', { name: 'something' } );
});

socket.on('message', function(data){
    console.log(data.name);
});

The problem now is with the above which is working perfectly I am able to send real time notification to all the users at once. But what's the best way to target a single user?

For example a user can send a smile to another user so only the second user should receive the notification and not all the users.

Should I make multiple listeners for node? or any other method to do this?

You need some way of identifying which socket that connected to your server is the one you want to send data to and then you can send to just that socket. You can keep track of user names when users connect or if you have some auth system, you can keep track of which socket belongs to which authenticated user.

Your server holds a list of connected sockets. Each connected one at a time and triggered a connection event on your server when they connected. Your application needs to create a way of knowing which of those connected sockets you want to send the data to. This is not something you've described anything about how you want that to work so we can't really help more specifically.

You can dispatch a notification to single user if you can discriminate that user. For example you can get a user_id on client connection to your nodejs server (the user_id is send from client, inside message) and save it in a key-value store (like Redis, memcache, ...). In this way you can correctly dispatch the notification, arrived from the server (like Symfony2 application), to right user.

I suggest you use Redis, both as a key-value store and for its implementation pattern of the publish/subscribe usable as a channel of communication between the server and the application of realtime.