socketio / socket.io

Realtime application framework (Node.JS server)
https://socket.io
MIT License
60.93k stars 10.1k forks source link

How to calculate count of websockets online using callbacks of “connection” and “disconnect” events (or another events)? #3166

Closed goozmake closed 3 years ago

goozmake commented 6 years ago

I need to get count of websockets online.

I trying to calculate this that:

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

const app = require('express')();
const http = require('http').Server(app);

var on_connect = 0;
var on_disconnect = 0;

var port = 6001;
var io = socketIo(port);

//I have only one NameSpace and root NS is not used
var ns1 = io.of('ns1');

ns1
    .on('connection', function (socket) {
        on_connect += 1;

        socket.on('disconnect', function (reason) {
            on_disconnect += 1;
        });
    });

...

app.get('/', function (req, res) {
    res.send({
        clientsCount: io.engine.clientsCount,
        onConnect: on_connect,
        onDisconnect: on_disconnect
    });
});

http.listen(8000, function () {
    console.log('Web app is listening');
});

I have a problem: why onConnect-onDisconnect not equal "clientsCount"?

What is right way to calculate count of websockets online using events callbacks?

"dependencies": {
    "cluster": "^0.7.7",
    "cluster-hub": "^0.1.2",
    "cookie": "^0.3.1",
    "express": "^4.16.2",
    "log4js": "^1.1.1",
    "redis": "^2.7.1",
    "socket.io": "^2.0.1",
    "socket.io-emitter": "^3.0.1",
    "socket.io-redis": "^5.0.1"
  }

Stackoverflow

halws commented 6 years ago

You are doing well, but you never remove values from yours on_connect/on_dissconnect. Try this:

var clients = null;
var ns1 = io.of('/ns1');
ns1.on("connection", socket => {
  clients++;
  console.log("connected clients", clients);
  console.log("io.engine.clientsCount", io.engine.clientsCount);

  socket.on("disconnect", socket => {
    clients--;
    console.log("user disconnected");
    console.log("io.engine.clientsCount", io.engine.clientsCount);
    console.log("disconnected clients", clients);
  });
})

Or, there is another way, you can use method namespace.clients(callback) - from documentation https://socket.io/docs/server-api/#namespace-clients-callback

var clients = null;
var ns1 = io.of('/ns1');

function getClientsCouts() {
  ns1.clients((error, socketsInRoom) => {
    if (error) throw error;
    console.log("clients in room", socketsInRoom.length);
  });
}

ns1.on("connection", socket => {
  clients++;
  console.log("connected clients", clients);
  console.log("io.engine.clientsCount", io.engine.clientsCount);
  getClientsCouts();

  socket.on("disconnect", socket => {
    clients--;
    getClientsCouts();
    console.log("user disconnected");
    console.log("disconnected clients", clients);
  });
})

And dont forget to add this on Client side const socket = io('/ns1');

goozmake commented 6 years ago

Why do decrement of on_connect/on_dissconnect ? Count of websockets online is difference between on_connect and on_dissconnect. No?

darrachequesne commented 3 years ago

For future readers: please see https://socket.io/docs/v3/server-instance/#Server-engine