Back

Get a list of active logedin users? Active current sessions?

  • 0
  • Users
  • Accounts
  • Databases
  • Web
Tassos P.
1 Aug, 2023, 17:10

Is there a way to get on a react app with realtime db, all active users' sessions? In simple all users that are currently logedin?

TL;DR
It is suggested to use a heartbeat approach to determine if a user is active or not. This involves having a heartbeats collection where users update a timestamp while they are active. If the timestamp is older than X minutes, the user is considered inactive. Another suggestion is to create a separate collection for profiles and use teams for permissions. Sessions can be obtained using the server API by iterating over each user and listing their sessions.
Drake
1 Aug, 2023, 17:33

that is a server operation so you would need to use the server api. you'd have to iterate over each user and list the sessions:

https://appwrite.io/docs/server/users?sdk=nodejs-default#usersListSessions>

Tassos P.
1 Aug, 2023, 18:19

is there a way to use the server sdk (node.js) in a react native or react app? Alternatively could you suggest me another way to succeed such functionality...

I tried also the realtime database events but unfortunately I cannot get the account.session.create event.... I get only the delete event

Drake
1 Aug, 2023, 18:20

i'ts a security concern running it client side because then end users will have full access to all other users...if you really want to, you can expose the data via an Appwrite Function

Tassos P.
1 Aug, 2023, 18:23

I figured out it to be honest (with a function).... Ok the alternative way via the account realtime events ? Is it a possible solution? I mean if I get realtime session details I get currently users logedin so I can get them in a list... I suppose

Drake
1 Aug, 2023, 18:26

uhh sure....for current user, you could possibly see those events come through...but a user can just fetch their own sessions:

https://appwrite.io/docs/client/account?sdk=web-default#accountListSessions

Tassos P.
1 Aug, 2023, 18:28

actually I mean this:

import { Client } from "appwrite";

const client = new Client() .setEndpoint('https://cloud.appwrite.io/v1') .setProject('[PROJECT_ID]');

client.subscribe('account', response => { // Callback will be executed on all account events. console.log(response); });

and from the response I can get the session.$id of each user....

Drake
1 Aug, 2023, 18:34

why do you need to subscribe and are you asking for a single user to get sessions of a different user?

Tassos P.
1 Aug, 2023, 18:40

The result that I seek is to create a chat app with multiple rooms (one to one user to user) and actually each user to be able to communicate with multiple users those who are in the users list page

Drake
1 Aug, 2023, 19:06

you'll probably need to create a database collection for users and then use teams for permissions

And why do you need sessions?

Tassos P.
1 Aug, 2023, 19:54

I thought I could use session and from there get session id and consequently userid from the sessionid and create an array of currently logged in users ….. something like that!! But ok also the suggestion of creating another collection sounds good …. Could you give me a short walk through on this? I mean how to combine user collection and teams and how am I supposed to get to the desired solution….. if possible of course

Drake
1 Aug, 2023, 20:23

Think of this collection as a Profiles collection that has public data for a user. My only suggestion is you should create the document in the colection when the user creates an account using an Appwrite function. Similar to my Users collection here: https://levelup.gitconnected.com/building-a-location-based-app-with-appwrite-48a2e2b6d4c2

The rest is up to you and how you want to design your app.

Tassos P.
2 Aug, 2023, 06:55

Sounds ok …. The reason that I am thinking sessions is that I need to determine whether a user is currently logged in or not. In other words if a user is live so that another logged in user can text him or not … Another way that I figured it out is that on the users collection I might add an extra attribute named session and whenever a user logs in I might run an update document function so that I can get a realtime event. What do you think? ....my only concern in this case is that whenever I might have multiple sessions from the same user each session will be update the previous one and finally I am thinking that on log out process I might call deleteSessions() instead of deleteSession('current')...

Drake
2 Aug, 2023, 16:08

just because there's a session, doesn't mean they are active. and you're right about the multiple sessions problem too. I wouldn't use sessions as a way to determine whether someone is active or not.

This type of thing is typically done with a presence API (you can read more about it on your own), but Appwrite doesn't have that.

I would suggest using a heartbeat approach to detect if someone is active or not. What you can do is have a heartbeats collection where a document in there is a user's heartbeat. The user would update a timestamp in their heartbeat document while they are active in your app. If the timestamp is older than X minutes, you can consider them inactive.

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more