V
V
vetsmen2017-02-09 18:14:57
JavaScript
vetsmen, 2017-02-09 18:14:57

Number of connections to socket.io?

How do you count connections to socket.io?
Tried like this: Object.keys(io.sockets.connected).length
And like this: io.sockets.sockets.length
And also like this:

var count = 0;
io.on('connection', function(socket) {
  console.log(++count);
  socket.on('disconnect', function() {
    console.log(--count);
  });
});

Everywhere there is one problem: with a very frequent page refresh, the number of connections increases (disconnect does not have time to work, as I understand it) and all left connections are eliminated after about a minute, how can it be in this case?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vetsmen, 2017-02-09
@vetsmen

Something one subscription to the topic, no answers, no one knows how to solve this problem?
There is an idea to look at the minimum data exchange through sockets and navigate according to this data, but maybe there are much more profitable solutions?

R
Rossomah, 2017-02-10
@Rossomah

Maybe this solution will work:
https://coderwall.com/p/ekrcyw/socket-io-managing-...

K
Kiryushka Tsisar, 2017-02-17
@carlcox

Writing to the session or database, and when disconnecting, delete the required sid accordingly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question