User Presence
User Presence helps us understand if a user is available to chat or not.
Real-time Presence
In other words, as a logged-in user, how do I know if a user is online or offline?
Based on the settings provided in the AppSettings
class while initializing CometChat using the init()
method, the logged-in user will receive the presence for the other users in the app.
In the AppSettings
class, you can set the type of presence you wish to receive.
For presence subscription, the AppSettingsBuilder
provides 3 methods :
subscribePresenceForAllUsers()
- This will inform the logged-in user when any user in the app comes online or goes offline.subscribePresenceForRoles(List<String> roles)
- This will inform the logged-in user, only when the users with the specified roles come online or go offline.subscribePresenceForFriends()
- This will inform the logged-in user when any of their friends come online or go offline.
If none of the above methods are set, no presence will be sent to the logged-in user.
For every activity or fragment you wish to receive user events in, you need to register the UserListener
using the addUserListener()
method.
We suggest adding the listener in the init
method of the activity or the fragment where you wish to receive these events in.
- Dart
class Class_Name with UserListener {
//CometChat.addUserListener("user_Listener_id", this);
void onUserOnline(User user) {
// TODO: implement onUserOnline
}
void onUserOffline(User user) {
// TODO: implement onUserOffline
}
}
Parameter | Description |
---|---|
listenerID | An ID that uniquely identifies that listener. We recommend using the activity or fragment name. |
You will receive an object of the User
class in the listener methods.
We recommend you remove the listener once the listener is not in use.
- Dart
String listenerID = "UNIQUE_LISTENER_ID";
CometChat.removeUserListener(listenerID);
User List Presence
In other words, as a logged-in user, when I retrieve the user list, how do I know if a user is online/offline?
When you retrieve the list of users , in theUser object, you will receive 2 keys:
status
- This will hold either of the two values :
online
- This indicates that the user is currently online and available to chat.offline
- This indicates that the user is currently offline and is not available to chat.
lastActiveAt
- In case the user is offline, this field holds the timestamp of the time when the user was last online. This can be used to display a Last seen for that user.