Is there a way to see which users are currently logged in and using my app?
Hereās a simple method I was just playing around with⦠it seems to work great, but Iāve only tested it with a handful of online Users, so Iām not sure how it would scale (lots of backend workflows being scheduled and cancelled, so it might use a lot of server capacity with a high volume of users - would need to test it with lots of online users to see how efficient, or not, it is)ā¦
Have a field on the User datatype called āOnlineā of type yes/no
Set up a backend workflow called āSet User Offlineā with a parameter for User, and a single action that sets the Userās Online field to āNoā
On the page you can continually set the Current Userās Online value to yes (every 3, 5, or 10 seconds for example), whilst also scheduling the āSet User Offlineā backend workflow to set it to ānoā, storing the workflow ID (in a field on the User datatype called āOnline Workflow IDā), and cancelling the previous scheduled workflow.
So the on-page (every x seconds) workflow could have 3 steps
- cancel a scheduled workflow (ID=the current userās online workflow ID)
- Schedule a backend workflow (āSet User Offlineā, User = Current User, date=current date/time +15 seconds (longer than the frequency of the on-page every x seconds workflow)
- Make changes to Current User: Online = Yes, Online Workflow ID = Result of Step 2
That will keep the Userās Online field set to yes whilst theyāre on the page, whilst scheduling a backend workflow to set it to ānoā, but continually cancelling and rescheduling that workflow whilst they remain online. Only when they leave your app will the backend workflow eventually run to set their online status to ānoā.
Then, to see whoās online (for example) you can have a repeating group of Users with an Icon inside the cell with a conditional to change, say the icon colour to green, when the current cellās userās Online = Yes
As I say, that works pretty well - but Iāve only tried it with a small number of online Users.
Iām not sure how well that would scale - youād just need to keep an eye on your server logs. If itās an issue you could always run the workflows less frequently (i.e. every 30 seconds , or every minute etc.) if it becomes a problem at scale.
EDIT: @ihsanzainal84ās method below is much simpler and cleaner, and doesnāt rely on backend workflows, so Iād recommend that method instead of this.
Really creative solution! Your right that might not be a good solution for scale though.
Havenāt tried this yet, just thinking from the top of my head.
Instead of using backend capacity you can do it client side. Just have a new User field for a date.
Create a workflow that updates that date field to the current time every minute (or whatever time interval u prefer).
You can define a user is online as long as that date field is no more than 1 minute of the current time. Again the time interval is up to you.
You can put this in a reusable element like a header or an empty group so u donāt have to repeat the workflow for multiple pages.
This uses a lot less capacity.
Perfect⦠great solution, much simpler and works just as wellā¦
Just to add (for anyone reading this in the future), if you want to display a list of users, with those who are āonlineā at the top, youāll need to do two searches with a :merged operator (as you canāt use sorted by the ālast onlineā field unless you want the list to keep changing order every time the field gets updated).
So do a search for Users whoās last online date is less than 1 minute ago (or whatever your cut-off time is), merged with a search for all users, and add unique elements. That will keep the āonlineā users at the top of the list without reordering them all the time.
Would having a workflow that updates 2 fields on the user every 60 seconds hurt capacity, UX, or interfere with other workflows running?
Also⦠if the workflow is in the header⦠and itās set to run every 60 seconds⦠would switching pages start the 60 seconds over? I imagine so.