Answer the question
In order to leave comments, you need to log in
How to store and retrieve information online whether the user in node js express and react?
I am making a website. react js + express js It is necessary to show whether the user is online now or who from the list is online when viewing a user or when listing users. How to organize it?
I only thought of opening a websocket connection with a static id (which does not change and depends on the user) and take the information window.addEventListener online, offline and transfer it over the socket when I get it to record in the user's table in mongo db all the time.
Or just make a put request every time the user clicks, passing the isOnline true field, which in 5 minutes will become false again if it is not extended.
Are there any elegant and long-established solutions? You can share a link to the article\library and how it is usually solved. Can it be done through redis? (I didn't use it)
Answer the question
In order to leave comments, you need to log in
As you wrote - websocket. Create a beforeunload event handler and send an appropriate message to the server. But there is a problem, if the user closes the browser, then this event will not fire. To do this, set a response time on the server, let's say 5 seconds, then after 5 seconds, if there is no response from the client, the necessary method can be called on the server to set the user offline status.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question