1

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?

Abstract
  • 561
  • 1
  • 8
  • 29
  • This doesn't have anything to do with PHP nor Symfony. It also doesn't have anything to do with Node.js at its core. What you're asking about is specific to Socket.IO. I recommend editing your question, as well as reading one of the many questions very similar to what you're asking: http://stackoverflow.com/a/11356019/362536 It really is as simple as emitting to the socket associated with whatever connection you want to send to. – Brad Oct 05 '14 at 00:44
  • Thanks for pointing me in the right direction. That answer helps and sorry it didn't show up in my research. – Abstract Oct 05 '14 at 00:56
  • No worries, it happens! It's often hard to get started since we don't know what to look for until we've found it. – Brad Oct 05 '14 at 00:57

2 Answers2

0

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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks mate. As you can tell I am really a starter in real time apps so not sure about this. If possible can you point me to the right tutorial or source where all this is explained a bit more in detail? – Abstract Oct 05 '14 at 00:49
  • @KaranKhanna - I don't know of a tutorial on this. If you can add to your question how you know which user you want to send to, then we could help with a way to keep track of the right info to be able to do that. You have to invent your own system of knowing which socket to send to unless you use chatrooms which are built in. The socket.io code is very good, but the doc is very shallow. – jfriend00 Oct 05 '14 at 00:51
  • @KaranKhanna - you can see all connection sockets with the server-side `.on("connection", function(sock) {...})` event. – jfriend00 Oct 05 '14 at 00:54
  • Thanks. Another question helped on this as pointed by Brad in the comment. – Abstract Oct 05 '14 at 00:58
0

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.

Angelo Giuffredi
  • 923
  • 3
  • 13
  • 26