Best way to find out if the user is online / offline

2

Well, I have the login and home pages, when I log in the user is set as online in the BD and is taken to home, at home, by clicking the logout button the user is set as offline and taken to login.

So far so good, however, when the user clicks to close the window or the browser wants it to be set as offline in the DB. What would be the best way to do this?

    
asked by anonymous 08.07.2018 / 15:46

1 answer

3

Having this type of thing depending on a user interaction is not a good idea, if the computer suddenly hangs up (power failure, shutdown because it crashed, etc.), the data in the database will not be updated

I imagine that in the database you have a online column that is set to true or false, I suggest that you change to a date type ( datetime or timestamp ), when the login is set in this field , the current date plus a 5-minute interval, for example. In the frontend, create a setInterval that makes an ajax request every 5min to refresh the field again. If a setInterval is not possible, this update can be done in the database whenever the user makes a request to the backend, thus the database data is outdated for a maximum of 5min

But beware, depending on how many users have been using the application at the same time, can make the application too cumbersome

Since you did not specify which database you are using, I thought it was a SQL like MySQL

    
08.07.2018 / 17:03