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