Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Socket reference in Socket.IO

Socket reference in Socket.IO

Problem

I am new to nodejs and was experimenting with Socket.io. I am trying to send message to specific sockets so I thought I will store references to the socket.

var controls = {};
var clients = {};

var control = io
  .of("/control")
  .on("connection", function(socket){
    socket.on("connect_player", function(data){
      var id = data.screen_id;
      controls[socket.id] = id;
    });

    socket.on("msg", function(data){
      id = controls[socket.id]
      player.socket(clients[id]).emit("msg", data);
    });
  });

var player = io
  .of("/player")
  .on("connection", function(socket){
    socket.on("set_id", function(id){
      clients[id] = socket.id
    });
  });

I first start a webpage which connects to the "player" namespace and displays an id(hard coded). Then another webpage which connects to the "control" namespace with the same id as player, now the controller should be able to send messages to that particular player but this works for a while and then fails.

During the period when it works I can send messages from the control page to the specific player page.

I looked at the sockets ids in the player namespace after it stopped working and found the socket ids are different from the initial ones.

I guess I am doing something wrong here or has a wrong understanding of the concepts. How can I fix this problem.

Edits:

I added listeners for "reconnect" event and found that the value of socket.id changes after each the "reconnect" event. So the controls and clients have invalid socket.id values which casues my problem. Is there away to overcome this.

Problem courtesy of: Josnidhin

Solution

Newest socket.io supports rooms. So how about this? For each connection set a unique ID for this connection (for example the unique username - this works great together with session - or whatever you want). Then you can use

io.on('connection', function(socket) {
   // retrieve the ID from socket
   socket.join('/priv/'+ID);
});

Then you can do the following in msg handler:

// get the ID
io.sockets.in('/priv/'+ID).emit("msg", data);

At this point you can be sure that the message went to the user you want. Of course you need make sure that the ID won't change when the client reconnects. Like I mentioned before - this can be done by sending session cookie to the server and retrieving the ID from session.

And I think this is the only acceptable solution, because the client may reconnect many times and you would want other clients to recogonize this particular client by for example (unique) name.

Solution courtesy of: freakish

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Socket reference in Socket.IO

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×