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?