In my experience, I found this the easiest and most useful solution:
Client side:
socket.on("disconnect", function() {
console.log("Disconnected");
});
socket.on("reconnect", function() {
console.log("Reconnecting");
});
socket.on("connect", function() {
socket.emit("registerToRoom", $scope.user.phone);
});
Server side:
io.on('connection', function(socket){
socket.on("registerToRoom", function(userPhone) {
socket.join(userPhone);
});
});
And here it is. Very simple and straightforward.
You can also add a few updates to the connected socket (last function) for the user screen, for example, update its index or something else.
source
share