How do I disable all socket.io enabled sockets?

How can I disable / close all server side sockets?

Maybe restart socket.io module from server side?

(using the latest socket.io version)

+3


source to share


2 answers


Assuming io

is your socket.io server object, you can do this from the server side to disconnect all currently connected clients:

io.sockets.sockets.forEach(function(s) {
    s.disconnect(true);
});

      

Clients may try to reconnect, depending on the client configuration.




FYI, there are many different answers to similar problems in this question: Socket.IO - how do I get a list of connected sockets / clients? but you should only pay attention to the answers that refer to versions of socket.io greater than 1.0, since this all changed with v1.0.

+3


source


For me, jfriend00's solution doesn't work (as of today).

I had to do this:



Object.keys(io.sockets.sockets).forEach(function(s) {
  io.sockets.sockets[s].disconnect(true);
});

      

+2


source







All Articles