force client disconnect from server with socket.io and nodejs

socket.io

socket.io Problem Overview


Is there any way to disconnect a client with SocketIO, and literally close the connection? So if someone is connected to my server, and I want to close the connection between them and my server, how would I go about doing that?

socket.io Solutions


Solution 1 - socket.io

Edit: This is now possible

You can now simply call socket.disconnect() on the server side.

My original answer:

This is not possible yet.

If you need it as well, vote/comment on this issue.

Solution 2 - socket.io

socket.disconnect() can be used only on the client side, not on the server side.

Client.emit('disconnect') triggers the disconnection event on the server, but does not effectively disconnect the client. The client is not aware of the disconnection.

So the question remain : how to force a client to disconnect from server side ?

Solution 3 - socket.io

Any reason why you can't have the server emit a message to the client that makes it call the disconnect function?

On client:

socket.emit('forceDisconnect');

On Server:

socket.on('forceDisconnect', function(){
    socket.disconnect();
});

Solution 4 - socket.io

Checking this morning it appears it is now:

socket.close()

https://socket.io/docs/client-api/#socket-close

Solution 5 - socket.io

For those who found this on google - there is a solution for this right now:

Socket.disconnect() kicks the client (server-side). No chance for the client to stay connected :)

Solution 6 - socket.io

Assuming your socket's named socket, use:

socket.disconnect()

Solution 7 - socket.io

This didn't work for me:

`socket.disconnect()` 

This did work for me:

socket.disconnect(true)

Handing over true will close the underlaying connection to the client and not just the namespace the client is connected to Socket IO Documentation.


An example use case: Client did connect to web socket server with invalid access token (access token handed over to web socket server with connection params). Web socket server notifies the client that it is going to close the connection, because of his invalid access token:

// (1) the server code emits
socket.emit('invalidAccessToken', function(data) {
    console.log(data);       // (4) server receives 'invalidAccessTokenEmitReceived' from client
    socket.disconnect(true); // (5) force disconnect client 
});

// (2) the client code listens to event
// client.on('invalidAccessToken', (name, fn) => { 
//     // (3) the client ack emits to server
//     fn('invalidAccessTokenEmitReceived');
// });

Solution 8 - socket.io

In my case I wanted to tear down the underlying connection in which case I had to call socket.disconnect(true) as you can see is needed from the source here

Solution 9 - socket.io

client._onDisconnect() should work

Solution 10 - socket.io

Socket.io uses the EventEmitter pattern to disconnect/connect/check heartbeats so you could do. Client.emit('disconnect');

Solution 11 - socket.io

I'm using client.emit('disconnect') + client.removeAllListeners() for connected client for ignore all events after disconnect

Solution 12 - socket.io

I am using on the client side socket.disconnect();

client.emit('disconnect') didnt work for me

Solution 13 - socket.io

You can do socket = undefined in erase which socket you have connected. So when want to connected do socket(url)

So it will look like this

const socketClient = require('socket.io-client');

let socket;

// Connect to server
socket = socketClient(url)

// When want to disconnect
socket = undefined;

Solution 14 - socket.io

I have using the socket client on React Native app, when I called socketIOClient.disconnect() this disconnects from the server but when I connect to the socket again the previous events were connected again, and the below code works for me by removing all existing events and disconnecting socket conneciton.

socketIOClient.removeAllListeners();
socketIOClient.disconnect(true);

Solution 15 - socket.io

To disconnect socket forcefully from server side

socket.disconnect(true)

OR

To disconnect socket by client side event

On client:

socket.emit('forceDisconnect');

On Server:

socket.on('forceDisconnect', function(){
    socket.disconnect(true);
});

Solution 16 - socket.io

You can call socket.disconnect() on both the client and server.

Solution 17 - socket.io

Add new socket connections to an array and then when you want to close all - loop through them and disconnect. (server side)

var socketlist = [];

   io.sockets.on('connection', function (socket) {
        socketlist.push(socket);  
        //...other code for connection here..
   });


    //close remote sockets
    socketlist.forEach(function(socket) {        
        socket.disconnect();                      
    });    

Solution 18 - socket.io

use :

> socket.Disconnect() //ok

do not use :

> socket.disconnect()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel KenneyView Question on Stackoverflow
Solution 1 - socket.ionh2View Answer on Stackoverflow
Solution 2 - socket.ioFabienView Answer on Stackoverflow
Solution 3 - socket.ioquahadaView Answer on Stackoverflow
Solution 4 - socket.ioHAS-JackView Answer on Stackoverflow
Solution 5 - socket.ioSascha GehlichView Answer on Stackoverflow
Solution 6 - socket.ioErikView Answer on Stackoverflow
Solution 7 - socket.ioBaran EmreView Answer on Stackoverflow
Solution 8 - socket.ioDuncanView Answer on Stackoverflow
Solution 9 - socket.ioEmmermanView Answer on Stackoverflow
Solution 10 - socket.ioAnthony WlodarskiView Answer on Stackoverflow
Solution 11 - socket.ioskymanView Answer on Stackoverflow
Solution 12 - socket.ioyoniaView Answer on Stackoverflow
Solution 13 - socket.ioSaurabh GuptaView Answer on Stackoverflow
Solution 14 - socket.ioU.AView Answer on Stackoverflow
Solution 15 - socket.ioGAURAV MOKASHIView Answer on Stackoverflow
Solution 16 - socket.ioCoder Gautam YTView Answer on Stackoverflow
Solution 17 - socket.ioPodTech.ioView Answer on Stackoverflow
Solution 18 - socket.iom-techView Answer on Stackoverflow