Node.js: socket.io close client connection

125,991

Solution 1

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 2

Did you try:

socket.disconnect() 

on client?

Solution 3

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 4

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

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();
});
Share:
125,991
baam
Author by

baam

Updated on August 19, 2021

Comments

  • baam
    baam over 2 years

    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.

  • baam
    baam over 10 years
    Nope, I didnt. I thought that would just trigger the disconnect event on the server side? And when would I trigger this?
  • Milan Babuškov
    Milan Babuškov over 10 years
    I miss that in your question, but when DO you want to close the connection on client?
  • baam
    baam over 10 years
    Every time the server connection is beeing closed. (reload/refresh and page unload/leave)
  • baam
    baam over 10 years
    I updated the main post. The socket.disconnect() on the client side does trigger the disconnect event but it does not solve my problem :/
  • T J
    T J over 8 years
    "If one of the sides closes it, then it is closed" - how to close it programatically from each side..? I couldn't find any documentation
  • chelo_c
    chelo_c over 8 years
    does socket.disconnect() trigers the disconnect event in that same line? for example: if the next line is" console.log('i am after dc'). will that line be executed before the disconnect event, or after? Thanks! @MilanBabuškov
  • user3304007
    user3304007 over 7 years
    Hi. It works, thanks. But does it remove user from connected users array too ?
  • Himani Agrawal
    Himani Agrawal over 7 years
    It should remove but I am not sure. Trying printing users array after closing connection.
  • user3304007
    user3304007 over 7 years
    Done. How can I print users array ? Before this I was using this one to delete disconnected user. delete users[socket.nickname]
  • netpoetica
    netpoetica about 7 years
    What is the true arg supposed to do?
  • Nic
    Nic almost 6 years
    You should mention the parameter; if it's true, the underlying connection will be closed as well, but otherwise it's left open (presumably to speed up reconnection)
  • Chopping
    Chopping about 5 years
    Actually,when u leave the page or direct to another page with the link, the connection of your websocket will automatically close and it is because the worker to support the page has been abort. Make a try on shared worker ,in which u can initialize your connection on it and persist the socket object .
  • Suisse
    Suisse about 3 years
    thx, and what is synonym to socket.destroy() ?
  • Huhngut
    Huhngut almost 3 years
    disconnect(close?: boolean): Socket<DefaultEventsMap, DefaultEventsMap, DefaultEventsMap> if true, closes the underlying connection Disconnects this client.