Finding out if a particular user is logged in

Is there any way to find out if a particular user is logged in or not?

Like we have a system to get the current logged in user. But is there a way to get all logged in users?

I want to add an Online/Offline Status to my website so that the others users can know if the user is online or not.

It’s trickier than it looks. If a user has closed their browser, they’re (technically) not logged in anymore. But closing the browser does not send any signal back to Anvil. As far as Anvil can tell, they’re still connected, just not doing anything.

You could require a “heartbeat” from the Client every few minutes, a confirmation that the user is still there. Log it in a database table.

With this setup, you can consult the table to determine who’s (probably) connected.
Miss two heartbeats, and you consider them to be disconnected. But you’ll have to program these behaviors yourself. They’re not built in.

Logging gets more complicated when the same user can log in from multiple locations, or even multiple browser windows on the same PC. Nothing in Anvil prevents that. You’ll need to decide

  • whether to allow multiple simultaneous log-ins per user
  • if allowed, how to log it, because now the User ID alone is not enough to identify a connection
  • if disallowed, which of the two log-ins to cancel (via anvil.users.logout()). Usually, it’d be the earlier log-in, but you might conceivably prioritize some other way (e.g., by time-of-day or location).
1 Like

I think I’ll just drop the idea then. Showing the Online/Offline status will not be liked by many users anyway.

Maybe I’ll work on a system like this later. First I’ll build the basic website.

I thought that maybe I can run a timer which checks every second whether a particular component is visible to user or not.

But I think that will not work, right?

It can certainly work. That’s the “heartbeat” I mentioned earlier.

That information must be conveyed back to some central location, where heartbeats from each user/connection are tabulated.

Oh, but how exactly? Like I tried it in a form and set a label component as my ‘heartbeat’

The timer checks every second if the label is visible and if it is not, it will set status of the current user to offline.

However, it showed online even after closing

Once the form is closed, the timer component stops firing, so the last thing you sent to the server was that the user was online.

You probably want to track the datetime that they were last online, and judge their online status based on how current that datetime is.

1 Like

Yep but I also wanted something accurate if possible

It is better to not add this system if it is not accurate.

How accurate it would be depends on how often your heartbeat fires. Update the server’s datetime every 5 seconds, and you could safely decide the user was offline if the datetime was over 10 seconds old. You need a bit of leeway to account for transmission delays, etc.

Of course, the more often your heartbeat fires, the more server traffic you have, which takes time away from other processing the app might be doing. You’d need to evaluate if the feature is worth it for your particular app.

1 Like

I think I will go for not adding it then. My website is already being slower.

Tentatively, my App calls the heartbeat function (on the server) every 10 minutes or so.

If you have to call such a function, though, consider what else you can do with it. (Dad always said to “combine trips when you can”.)

It’s also an opportunity to return information about the App’s heath. For example, the copy running in the browser might be out-of-date, e.g., if you’ve recently fixed a critical bug, but the end-user’s copy is older than that.

1 Like

Ten minutes may not slow down my website further but I am not even sure if I want it anymore or not. Users will of course not like their status to be displayed online