Socket.io: Emitting to old connections on browser refresh and retaining old connections

Created on 1 Feb 2016  ·  4Comments  ·  Source: socketio/socket.io

Hi,

I have a simple socket.io server test setup that is sending the current Date() to a single client.

Every 1 second the server sends (emits) the Date() to one client.

If I refresh the browser window, I still have one connection but the server emits to two connections.

If I refresh the browser window, I still have one connection but the server emits to three connections.

If I refresh the browser window, I still have one connection but the server emits to four connections.

And so on and so on...

Even after several hours the disconnected clients are still being emitted to by the server.

I was expecting the server to drop/close the old connections without a client attached.

Is this expected behavior?

I'm using Socket.io v1.4.5 with Node v5.4.1

Here's a code sample:

io.on('connection', function (socket) {

  // console.log(socket.connected);
  console.log('===================');

  setInterval(() => {
    console.log(socket.conn.id);
    socket.emit('data', { ts: new Date() });
  }, 1000);

});

Here's the console.log output when I refresh the browser every 2 seconds.

You can see the socket.conn.id for each browser refresh is being emitted to.

❯ node app.js                                                                                                                                       ⏎
===================
kkizbfn-82gNOsyAAAAA
kkizbfn-82gNOsyAAAAA
===================
kkizbfn-82gNOsyAAAAA
m_ft38Y4XEY7W97VAAAB
kkizbfn-82gNOsyAAAAA
m_ft38Y4XEY7W97VAAAB
===================
kkizbfn-82gNOsyAAAAA
m_ft38Y4XEY7W97VAAAB
xH09a5Olle4A-HSNAAAC
kkizbfn-82gNOsyAAAAA
m_ft38Y4XEY7W97VAAAB
xH09a5Olle4A-HSNAAAC
===================
m_ft38Y4XEY7W97VAAAB
xH09a5Olle4A-HSNAAAC
kkizbfn-82gNOsyAAAAA
_GmZXIXg8VgUrdKlAAAD
m_ft38Y4XEY7W97VAAAB
xH09a5Olle4A-HSNAAAC
kkizbfn-82gNOsyAAAAA
_GmZXIXg8VgUrdKlAAAD

Even after many hours and no connected clients (browsers) the old connections are still being emitted to.

Most helpful comment

@StevenBock Ah OK .. yep that's all good - thanks!

I have the same issue with this other example.

I'd like to tail a file and send each new line to the client.

Using the npm 'tail' module this code works OK, but has that holding onto old connections problem:

const io = require('socket.io')(8080);

const Tail = require('tail').Tail;
const tailTmpFile = new Tail("./tmp.txt");

io.on('connection', function (socket) {

  console.log(socket.conn.id);

  tailTmpFile.on("line", function (data) {
      console.log('emitting Yes: ', socket.conn.id);
      socket.emit('file', { data: data, ts: Date() });
  });

});

Somehow I need to dispose tail event (tailTmpFile.on) when the socket disconnects.

All 4 comments

You need to clear the interval on disconnect with clearInterval.

var oneSecondInterval = setInterval(() => {
    console.log(socket.conn.id);
    socket.emit('data', { ts: new Date() });
  }, 1000);

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

This should stop it from holding on to the old connections.

@StevenBock Ah OK .. yep that's all good - thanks!

I have the same issue with this other example.

I'd like to tail a file and send each new line to the client.

Using the npm 'tail' module this code works OK, but has that holding onto old connections problem:

const io = require('socket.io')(8080);

const Tail = require('tail').Tail;
const tailTmpFile = new Tail("./tmp.txt");

io.on('connection', function (socket) {

  console.log(socket.conn.id);

  tailTmpFile.on("line", function (data) {
      console.log('emitting Yes: ', socket.conn.id);
      socket.emit('file', { data: data, ts: Date() });
  });

});

Somehow I need to dispose tail event (tailTmpFile.on) when the socket disconnects.

@StevenBock OK great - got it.

This code removes the event listener on disconnect.

io.on('connection', function (socket) {

  console.log(socket.conn.id);

  var emitLine = function () {
    var callback = function (data) {
      socket.emit('file', { data: data, ts: Date() });
    }
    tailTmpFile.on("line", callback);

    // dispose function
    return function() {
      tailTmpFile.removeListener('line', callback);
    }
  }();

  socket.on('disconnect', function () {
    // dispose tail file listener
    emitLine();
  });

});

Thank you kindly for taking the time to reply, it really set me in the right direction.

Cheers.

Glad to help!

Was this page helpful?
0 / 5 - 0 ratings