Skip to main content
Quick Reference for AI Agents & Developers
// Subscribe to presence during init
val appSettings = AppSettings.AppSettingsBuilder()
    .subscribePresenceForAllUsers()
    .setRegion("REGION")
    .build()
CometChat.init(context, "APP_ID", appSettings, callback)

// Add user listener
CometChat.addUserListener("LISTENER_ID", object : CometChat.UserListener() {
    override fun onUserOnline(user: User?) {
        Log.d(TAG, "${user?.name} is online")
    }
    override fun onUserOffline(user: User?) {
        Log.d(TAG, "${user?.name} is offline")
    }
})

// Remove listener (important!)
CometChat.removeUserListener("LISTENER_ID")
Available via: SDK | REST API | UI Kits
User Presence helps you understand if a user is available to chat or not.

Real-time Presence

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 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() - Informs the logged-in user when any user in the app comes online or goes offline.
  • subscribePresenceForRoles(List<String> roles) - Informs the logged-in user only when users with the specified roles come online or go offline.
  • subscribePresenceForFriends() - Informs 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 where you wish to receive user events, you need to register the UserListener using the addUserListener() method. We suggest adding the listener in the onResume() method of the activity or fragment where you wish to receive these events.
private String listenerID = "UNIQUE_LISTENER_ID";

CometChat.addUserListener(listenerID, new CometChat.UserListener() {
  @Override public void onUserOnline(User user) {
    Log.d(TAG, user.getName() + "is online.");
  }
  @Override public void onUserOffline(User user) {
    Log.d(TAG, user.getName() + "is offline.");
  }
});   
ParameterDescription
listenerIDAn 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 removing the listener once the activity or fragment is not in use. We suggest adding this method in the onPause() method of the activity or fragment where you wish to receive these events.
private String listenerID = "UNIQUE_LISTENER_ID";

CometChat.removeUserListener(listenerID);     
Always remove listeners when they’re no longer needed (e.g., in onPause() or onDestroy()). Failing to remove listeners can cause memory leaks and duplicate event handling.

User List Presence

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 the User object, you will receive 2 keys:
  1. status - This holds either of the two values:
  • online - Indicates that the user is currently online and available to chat.
  • offline - Indicates that the user is currently offline and not available to chat.
  1. lastActiveAt - If the user is offline, this field holds the timestamp of when the user was last online. This can be used to display a Last seen indicator for that user.

Best Practices

Use subscribePresenceForAllUsers() for small communities (under 1000 users), subscribePresenceForFriends() for social apps where users have friend lists, or subscribePresenceForRoles() for role-based presence to optimize performance and reduce unnecessary presence updates.
Batch presence updates in your UI to avoid excessive re-renders when multiple users change status simultaneously. Consider debouncing presence updates if you’re displaying a large user list.
Display a “Last seen” indicator using the lastActiveAt timestamp for offline users to provide better context about user availability.

Next Steps