how to disconnect socket using the global io variable

magic gum

I have tried this code here to disconnect all sockets

let map = io.sockets.sockets,
array = Array.from(map, ([name, value]) => ({ name, value }));
for(i=0;i<array.length;i++){
io.to(array[i].name).disconnect()
}

but io.to().disconnect(); is not a function, unlike socket.dissconect() which is a valid function, is there something that can do what i am trying to do?

Gal Avramov

My solution for that is a global object named 'sockets'

whenever user connects, I'm setting his socket in the sockets object where the key is the socket.id

const sockets = {};

io.on('connection', socket => {
    sockets[socket.id] = socket;
    socket.on('whatever', () => {
        for (let i in sockets) {
            sockets[i].disconnect();
            delete sockets[i];
        }
    });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Socket.IO handling disconnect event

force client disconnect from server with socket.io and nodejs

socket.io client connect disconnect

How to disconnect all sockets serve side using socket.io?

Socket.io send disconnect event with parameter

socket.io + android - disconnect socket and reconnect with different url

How to disconnect/stop scanning of Socket Mobile scanner using API?

How to avoid using a global variable?

How to get the socket id of a disconnected client on the disconnect event in socket.io

Socket.io disconnect not working from componentwillunmount

socket.io chat add user name to disconnect

Detecting Socket Disconnect Using TCP KeepAlive

Disconnect previous client socket.io socket in node.js

Socket.io detect disconnect reason

Node/Socket.IO sometimes not registering disconnect?

Angular-socket-io: how to disconnect the client

Socket.io disconnect client by id

how to avoid that=this on JS using socket.io?

Io socket reset connection upon disconnect or reload

disconnect client from socket.io id

How socket.io disconnect works?

Socket.IO not getting disconnect event on polling

How do i using socket.io?

Socket.io don't emit after disconnect event

How to disconnect a client for being inactive in chat for a specific time (socket.io, javascript)

Socket.io socket.handshake undefined on disconnect

Socket.io disconnect on React client is not triggered on page refresh

How to fetch a variable inside a function in one file and pass it to another file using node js (socket.io)?

How does the disconnect event works in socket.io?