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

Asked

Viewed 327 times

2

Well, I am with the login and home pages, when logging in the user is set as online in BD and is taken to home, at home, when 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 I want it set as offline in BD. What would be the best way to do that?

1 answer

3


Having this kind of thing depending on a user interaction is not a good idea, if the computer shuts down suddenly (power out, force shut down because it has crashed, etc) the data in the database will not be updated

I imagine, on the bench, you have a column online where it is set true or false, I suggest you change to a date type (datetime or timestamp), when the user logs in is set, in this field, the current date plus an interval of 5min, for example. In the frontend create a setInterval make an ajax request every 5min to refresh the field again. If a setInterval not possible, can be made this update in the database whenever the user performs some request for the backend, thus, the database data were outdated by, no more, 5min

But beware, depending on how many users have using the application at the same time, it can make the application very heavy

Since you didn’t specify which database you’re using, I figured it’s an SQL like Mysql

Browser other questions tagged

You are not signed in. Login or sign up in order to post.