Nick Pineda
Nick Pineda

Reputation: 6462

socket.io 1.0.0^ : Using the default room vs a custom room for user specific communications

socket.io 1.4.5 , node.js 5.x

Forward : A lot of reading, effort, and failure came before this post - so I hope it helps out a lot of future readers.


The option of using the default room has confused me when I consider how to use rooms for user specific communication.(like a direct message)

Because intuitively, it seems like an out-of-the-box solution that potentially should deprecate the need of creating a custom room for each user using their userID or email like this popular solution.

And since it's created automatically, I figured it should be my one and only unique listener for each user.

Which lead my due diligence to create this solution:

io.on('connection', function(socket){
  var curretUserID = null

  socket.on('set user',function(user){
    userID = curretUserID.id
  });

  io.to(socket.id).emit('welcome','welcome to the room!)

  socket.on('chat message', function(msg){//Makes Sense
    io.emit('chat message', msg);
  });

  socket.on('private message', function(id, msgData){
    if(userID === msg.recipientID){
        socket.to(id).emit('private message', 'for your eyes only');
    }
  });

});

I set userID to live inside the parent scope connection to the avoid need of creating a map between users and sockets because supposedly that is suppose to be the advantage of using the room solution altogether.

Now I haven't seen anyone do it this way, so I have no idea if I'm completely going a direction unintended with default rooms, or if for some reason, this doesn't scale or work well in the wild.

Question:

Is this one of the intended uses of the default room, or is creating a custom room(like in the popular answer mentioned above) still the current way to handle user specific communications?

Upvotes: 4

Views: 681

Answers (1)

Karolis Šinas
Karolis Šinas

Reputation: 76

It's not easy to understand your real problem, especially when you mix up all variables... But I will try to answer your question.

I think the problem hides in socket ids on the server side. Socket.io prepends namespace on each socket.id. So let's say your socket.id on client side is 4zIISeXsSvKL6VGyAABe, then on the server side it's /#4zIISeXsSvKL6VGyAABe.

I hope socket.io team will fix this issue, because they also created it in recent release.

The way I fix it, I update user.id on client side after connection.

Server:

io.on('connection', function(socket) {
  socket.emit('register id', socket.id)
}

Client:

socket.on('register id', function(id){
  socket.id = id
})

Then, a private message can be sent like this

Server:

io.on('connection', function(
  socket.emit('register id', socket.id)

  io.to(socket.id).emit('welcome','welcome to the room!)

  socket.on('chat message', function(msg){//Makes Sense
    io.emit('chat message', msg);
  });

  socket.on('private message', function(msgData){
        socket.to(msg.recipientID).emit('private message', 'for your eyes only');
  });

});

Upvotes: 1

Related Questions