Node.js: socket.io close client connection

Javascriptnode.jsExpresssocket.io

Javascript Problem Overview


How can I close the socket connection on the client side?

I am using:

  • socket.io 0.9
  • node.js 0.10.15
  • express 3.3.4

i.e.: call localhost/test
-- server side

var test = io
.of('/test')
.on('connection', function (socket) {

  console.log('open socket: ' + socket);

  socket.on('disconnect', function () {
    console.log('disconnected event');
    //socket.manager.onClientDisconnect(socket.id); --> endless loop with this disconnect event on server side
    //socket.disconnect(); --> same here
  });
});

-- client side

var socket = io.connect('http://localhost:3000/test');
socket.on('disconnect', function () {
   console.log('disconnect client event....');
});

socket.emit('getInitData', function (data) {
  .. do something with data
});

If I load the test-page I need some values from the server (getInitData).
On the first page visit I get the data once, on a reload or second visit I get it twice and so on.

The connection on the server side is beeing closed automatically on page reload and if you leave the page.
But on the client side the connection is still open.
How can I close the connection on the client side or check if there is already a open connection?

UPDATE
I tried now the following: (client side)

window.onbeforeunload = function(e) {
  socket.disconnect();
};

This triggers on the client side the disconnect event, but I still get the twice or tripple response.

Javascript Solutions


Solution 1 - Javascript

Did you try:

socket.disconnect() 

on client?

Solution 2 - Javascript

For socket.io version 1.4.5:

On server:

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

On client:

var socket = io();
socket.emit('end');

Solution 3 - Javascript

There is no such thing as connection on server side and/or browser side. There is only one connection. If one of the sides closes it, then it is closed (and you cannot push data to a connection that is closed obviously).

Now a browser closes the connection when you leave the page (it does not depend on the library/language/OS you are using on the sever-side). This is at least true for WebSockets (it might not be true for long polling because of keep-alive but hopefuly socket.io handles this correctly).

If a problem like this happens, then I'm pretty sure that there's a bug in your own code (on the server side). Possibly you are stacking some event handlers where you should not.

Solution 4 - Javascript

socket.disconnect()

Only reboots the connection firing disconnect event on client side. But gets connected again.

socket.close()

Disconnect the connection from client. The client will keep trying to connect.

Solution 5 - Javascript

socket.disconnect() is a synonym to socket.close() which disconnect the socket manually.

When you type in client side :

const socket = io('http://localhost');

this will open a connection with autoConnect: true , so the lib will try to reconnect again when you disconnect the socket from server, to disable the autoConnection:

const socket = io('http://localhost', {autoConnect: false});
socket.open();// synonym to socket.connect()

And if you want you can manually reconnect:

socket.on('disconnect', () => {
  socket.open();
});

Solution 6 - Javascript

I'm trying to close users connection in version 1.0 and found this method:

socket.conn.close()

The difference between this method and disconnect() is that the client will keep trying to reconnect to the server.

Solution 7 - Javascript

try this to close the connection:

socket.close();

and if you want to open it again:

socket.connect();

Solution 8 - Javascript

Just try socket.disconnect(true) on the server side by emitting any event from the client side.

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
QuestionbaamView Question on Stackoverflow
Solution 1 - JavascriptMilan BabuškovView Answer on Stackoverflow
Solution 2 - JavascriptHimani AgrawalView Answer on Stackoverflow
Solution 3 - JavascriptfreakishView Answer on Stackoverflow
Solution 4 - JavascriptAli AzharView Answer on Stackoverflow
Solution 5 - JavascriptAhmad ZahabiView Answer on Stackoverflow
Solution 6 - JavascriptJakubKnejzlikView Answer on Stackoverflow
Solution 7 - JavascriptLuis FletesView Answer on Stackoverflow
Solution 8 - JavascriptLalit Kumar MauryaView Answer on Stackoverflow