How to call the client for its online
presence in node.js + socket.io
, how does this bundle work? I need from time to time to put in the database of those who are online. how to track it?
2 answers
Create the object variable in the global scope of the node.js script:
var online = {};
When establishing a socket connection, we take its id from the connection property and make a link to this connection:
online[id] = connection;
We manipulate the object online as we wish, for example, by searching the properties of the object, find out how many people are online now, etc.
At the end of the connection do this:
delete online[id];
- You misunderstand me a little. All this is understandable. Do I need to update the timestamp of user activity each time? Therefore, a variable object is created once and a connection is created once. And it goes without saying that when the client leaves, the disconnect method is called, which removes the socket identifier. But I need to connect all this with the database by the following principle: lukemelia.com/blog/archives/2010/01/17/… - IOleg
- Exactly the same question was asked here by someone: stackoverflow.com/questions/9610286/… there is one answer. I will dig. but I will also be happy for the Russian-speaking audience - IOleg
- Interesting software solution. I studied socket.io but, apart from the settings, heartbeats did not find anything, maybe some kind of call back is? - IOleg 2:57 pm
- oneI do not see a problem - to work with any base in my answer, paragraph 3 is set forth;) something like this: clientRedis.hset ("online", "user:" + id, "enterTime:" + (new Date ()) ); Well, when disconnecting, delete the corresponding hash key. - deivan_
I came up with the following idea: When a user connects to the server via socket.io, enter his id in the Redis repository with the timestamp of entry. When disconnecting, respectively, remove from radish or add another entry - exit time. Then nodоm poll radishes every 2 minutes and watch who is online and send to the client. On the other hand, why these troubles - if you can remove the id from the user_online object and send it to the client - that the user id is out. what is in the standard case
- Another moment. Each user has his own contact list for correspondence. When disconnecting a user from the contact list - how to send an event (notification) only to those who have it in contacts? T-not to do broadcast? - IOleg
- Each user on the site has its own contact list for correspondence. Suppose I have a user id = 100 in my contacts. When she leaves the chat, the node.js disconnected event is triggered and sends to all clients that the user id = 100 has left the chat. - IOleg
- But I do not want this message to be sent to everyone, but I want only to those who have this contact in the contact list. As an option - when disconnecting a specific user - make a request to the redis database and pull out from there the id of those who have this contact in the list of friends. take their id and send them a notification. But will it be correct and productive? - IOleg