Sails.js. How to count users online?

Im new with sails.js (node.js). What is the best way to get the number of users online now?

+4
source share
4 answers

I'm not sure if this is the best way, but one way could be to use a web socket.

On the client side, when someone connects, he can send a username or email address so that you can save a list of online users on the server side.

Then, in disabling the web socket, you remove the user from your list of online users.

There you go, you have an online user account.

Take a look at http://socket.io/ for more information on how to use web sockets

Hope this helps. :)

+3
source

Sorry for the late reply, but it appeared a couple of times in # sails.js

I don't know if my path is correct, but it works with 0.9.4, and this is the best way I've found so far:

// if sails is undefined, get it out of global var sails = global.sails, ws = sails.ws, channel = req.param('channel') || '', // by room, or '' for top level current = ws.of(channel).manager.open; // an array of socket ids 

If you do not use ws , then sails.io works (the structure is slightly different from tho.) Call current.length to get the number of connected clients, or you can do something like:

  var status = []; for (var x in current) { if (ws.of(channel).manager.handshaken.hasOwnProperty(x)) { status.push(ws.of(channel).manager.handshaken[x].session.user); } } 
  • to find out who they are. :) hth
+1
source

There will be a pool of active sessions that you can use. I would recommend using socket.io to count users, since it does not have a 100% backup (flash is required in some cases)

0
source

every time a new socket connects to the server, the sails automatically add a new room, and I think the number of them is the number of online users, unless you add new user rooms.

check this out: sails.sockets.rooms ()

but I'm sure this is not a solution, and I'm looking to find a better way.

0
source

Source: https://habr.com/ru/post/1494316/


All Articles