12

I'm new to nodejs and trying to write a chat room as so many people have. The chat consists of multiple rooms and clients. Commands such as /nick /join /help /ls users /ls rooms work as you would expect although I'm having trouble with getting a /kick command to work.

I'm just not sure how you disconnect a client by id, so far /kick client is able to present the respective clients socket.id although I'm stuck for the code to kick via socket.id.

Code so far:

Disconnect client who sent /kick: socket.disconnect();

Delete client from arg /kick client: delete io.sockets.sockets[client];

Deleting the client doesn't disconnect them though, they can still receive data just not send it.

Solved

CuriousGuy's 0.9 worked flawlessly, for those interested - here is the code I'm using.

Server side:

handleClientKick(socket);

...

function handleClientKick(socket) {
  socket.on('kick', function(client) {
    if (typeof io.sockets.sockets[client] != 'undefined') {
      socket.emit('message', {text: nickNames[socket.id] + ' kicked: ' + nickNames[client]});
      io.sockets.sockets[client].disconnect();
    } else {
      socket.emit('message', {text: 'User: ' + name + ' does not exist.'});
    }
  });
}

Client side:

kickClient = function(client) {
  this.socket.emit('kick', client);
};
Community
  • 1
  • 1
rwxes
  • 285
  • 1
  • 2
  • 10

4 Answers4

16

The following code works with Socket.IO 1.0, however I'm not sure that this is the best solution:

if (io.sockets.connected[socket.id]) {
    io.sockets.connected[socket.id].disconnect();
}

Update:

With Socket.IO 0.9 the code would be slightly different:

if (io.sockets.sockets[socket.id]) {
    io.sockets.sockets[socket.id].disconnect();
}
Oleg
  • 22,300
  • 9
  • 68
  • 84
  • hmm, I get an undefined when I run `console.log(io.sockets.connected[socket.id])` any idea? – rwxes Jun 28 '14 at 08:12
  • It means that either this socket is disconnected already, or `socket.id` value is invalid. What do you get if you run `console.log(socket.id);`? – Oleg Jun 28 '14 at 08:14
  • I tested my solution with Socket.IO 1.0 – Oleg Jun 28 '14 at 08:17
  • `socket.id` works as expected returning `BGGkTQOOBJTcO2YukQLl` etc.. - just checked and I'm using 0.9.16, I'll try with 1.0 – rwxes Jun 28 '14 at 08:24
  • running into all sorts of other errors trying my pre-1.0 code running on 1.0 (sort of expected that). I find it strange how hard it is for me to disconnect a client by the clients `socket.id` yet so simple in general to setup sockets.io. – rwxes Jun 28 '14 at 08:38
  • Maybe you will find [this article](http://socket.io/docs/migrating-from-0-9/) useful. – Oleg Jun 28 '14 at 08:44
  • BTW, I've edited my answer adding a solution for 0.9 that seems to work for me – Oleg Jun 28 '14 at 08:45
  • the 0.9 code worked perfectly, so simple. Thank you for the article, really appreciate the help! – rwxes Jun 28 '14 at 08:58
  • How to can I reconnect, without refresh browser? I tried execute io() by client side, but is not work – Lai32290 Feb 03 '15 at 11:56
  • tested on current socket.io (1.3.something? anyway you can see a date of my msg) io.sockets.connected is still in place, thank you. – Max Yari May 07 '15 at 22:35
5

This is an old question but if anyone wonders for newer versions;

In Socket.IO v4.X io.sockets.connected[socket.id] or io.sockets.sockets[socket.id] is not working.

So we need to do like this;

io.sockets.sockets.forEach((socket) => {
    // If given socket id is exist in list of all sockets, kill it
    if(socket.id === givenSocketID)
        socket.disconnect(true);
});
Closery
  • 828
  • 9
  • 14
4

Alternate solution In Socket.IO v4.X

For all sockets

const sockets = await io.fetchSockets();

For particular socket

const sockets = await io.in(theSocketId).fetchSockets();

Iterate sockets

for (const socket of sockets) {
     console.log("socket id",socket.id);
     socket.disconnect(true);
}

Reference link

Yadav Chetan
  • 1,874
  • 2
  • 23
  • 43
3

Here's another option for Socket.IO v4 that doesn't require async syntax:

io.sockets.sockets.get(socket.id)

Someone can correct this if it's wrong, but I think each socket has a unique ID, so there should be no need for iterating.

SuperCodeBrah
  • 2,874
  • 2
  • 19
  • 33