Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emitting to old connections on browser refresh and retaining old connections #2424

Closed
rudijs opened this issue Feb 1, 2016 · 4 comments
Closed

Comments

@rudijs
Copy link

rudijs commented Feb 1, 2016

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.

@StevenBock
Copy link

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.

@rudijs
Copy link
Author

rudijs commented Feb 1, 2016

@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.

@rudijs
Copy link
Author

rudijs commented Feb 1, 2016

@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.

@rudijs rudijs closed this as completed Feb 1, 2016
@StevenBock
Copy link

Glad to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants