socket.io 1.4.5, node.js 5.x
Go ahead: there has been a lot of reading, effort and failure before this post, so I hope this helps many future readers.
The possibility of using the default room confused me when I consider how to use the rooms to communicate with a specific user (for example, a direct message)
Because intuitively, it looks like a ready-made solution that should potentially discount the need to create a custom room for each user using userID or email, for example, this is a popular solution .
And since it was created automatically, I decided that it should be mine and only a unique listener for each user.
What role do I consider 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 installed userID
in order to live inside the parent area connection
to avoid the need to create a map between users and sockets, because presumably this should be the advantage of using the solution for the room as a whole.
Now I didn’t see anyone doing it this way, so I have no idea completely directing a direction not intended for the default rooms, or if for some reason this does not scale or work well in the wild.
Question:
( , ) ?