How to Register User Offline on Browser Close or Inactivity?

I have a question about properly tracking user online/offline status in my PHP/MySQL system.

Scenario:

  • When the user logs in or clicks logout, the system correctly registers their status.
  • If they close only the tab, they remain logged in (this is fine and should not change).
  • If they close the browser, I want to register that they are offline.
  • If they remain inactive for a certain period, I want the system to automatically set them as offline.

Questions:

  1. How can I update the database to mark a user as offline when they close the browser?
  2. Where can I configure the inactivity timeout to automatically change the user’s status to offline?

Note: Based on my research, this only works efficiently with WebSockets, meaning it is only feasible in Node.js projects. Is there an alternative solution for PHP projects?

Thanks in advance for any help!

I can’t comment on closing the browser, but point 4 is actually pretty simple.

In the global step, you can run a conditional for whether the user is logged in, and if they are, update a table somewhere in the database with the current time which would be their last activity time (eg in your user table, add a last_activity column). The global step actions run when any server action is ran.

You can then setup a scheduler to run every X minutes, and if the current time is more than X minutes compared to the one in the users last_activity column, you can update the users status to offline in the database

Sure there is, but you'll need a separate NodeJS or similar app that is responsible for the WebSockets, and then send an HTTP request to your primary app whenever a user goes offline

Edit: Note the app must be smart so it only sends the request when all WebSockets (all browser tabs) of that user are closed