I have a socket id of a connection. Can I get the status of that connection, inside the function handler of another one?
Something like this:
io.sockets.on('connection', function(socket) {
/* having the socket id of *another* connection, I can
* check its status here.
*/
io.sockets[other_socket_id].status
}
Is there a way to do so?
You can access any connected socket with io.sockets.sockets[a_socket_id], so if you've set a status variable on it, io.sockets.sockets[a_socket_id].status will work.
First you should check if the socket really exists, and it can also be used to check connected/disconnected statuses.
if(io.sockets.sockets[a_socket_id]!=undefined){
console.log(io.sockets.sockets[a_socket_id]);
}else{
console.log("Socket not connected");
}
As of today, Feb, 2015, none of the methods listed here work on the current version of Socket.io (1.1.0). So on this version, this is how it works for me :
var socketList = io.sockets.server.eio.clients;
if (socketList[user.socketid] === undefined){
the io.sockets.server.eio.clients is an array containing a list of all the live socket id's. So use the code in the if statement to check if a particular socket ID is in this list.
Socket.io >= 1.0, as Riwels answer, first you should check if the socket exists
if(io.sockets.connected[a_socket_id]!=undefined){
console.log(io.sockets.connected[a_socket_id].connected); //or disconected property
}else{
console.log("Socket not connected");
}
if (io.sockets.connected[socketID]) {
// do what you want
}