Skip to main content
Quick Reference for AI Agents & Developers
// Subscribe to presence during init
const appSettings = new CometChat.AppSettingsBuilder()
  .subscribePresenceForAllUsers()  // or .subscribePresenceForRoles([]) / .subscribePresenceForFriends()
  .setRegion("REGION").build();

// Listen for presence changes
CometChat.addUserListener("LISTENER_ID", new CometChat.UserListener({
  onUserOnline: (user) => console.log("Online:", user),
  onUserOffline: (user) => console.log("Offline:", user)
}));

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

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 initialising the SDK 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 that particular session of the app. 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(Array 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, only when either of his friends come online or go offline.
If none of the above methods are used, no presence will be sent to the logged-in user.
You must configure presence subscription in AppSettings during CometChat.init() before any presence events will be delivered. See Setup SDK for details.
You need to register the UserListener using the addUserListener() method where ever you wish to receive these events in.
let listenerID = "UNIQUE_LISTENER_ID";

CometChat.addUserListener(
  listenerID,
  new CometChat.UserListener({
    onUserOnline: onlineUser => {
      console.log("On User Online:", { onlineUser });
    },
    onUserOffline: offlineUser => {
      console.log("On User Offline:", { offlineUser });
    }
  })
);
ParameterDescription
listenerIDAn ID that uniquely identifies that listener.
You will receive an object of the User class in the listener methods.
Listener Cleanup: Always remove the listener when the component or view is unmounted/destroyed to prevent memory leaks and duplicate callbacks. Use CometChat.removeUserListener(listenerID) in your cleanup logic.
let listenerID = "UNIQUE_LISTENER_ID";
CometChat.removeUserListener(listenerID);
Always remove your UserListener when the component or view unmounts to prevent memory leaks and duplicate event handling.
CometChat.removeUserListener("LISTENER_ID");

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 fetch the list of users, in the User object, you will receive 2 fields
  1. 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.
  1. 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 the Last seen of the user if need be.
PracticeDetails
Choose the right subscriptionUse subscribePresenceForFriends() or subscribePresenceForRoles() instead of subscribePresenceForAllUsers() in apps with many users to reduce unnecessary events
Unique listener IDsUse unique, descriptive listener IDs (e.g., "chat-screen-presence") to avoid accidentally overwriting other listeners
Cleanup on unmountAlways call removeUserListener() when the component/view is destroyed
Combine with user listUse the status field from UsersRequest results for initial state, then update via UserListener for real-time changes
SymptomCauseFix
No presence events receivedPresence subscription not configured in AppSettingsAdd subscribePresenceForAllUsers(), subscribePresenceForRoles(), or subscribePresenceForFriends() to your AppSettingsBuilder
Presence events stop after navigationListener was removed or component unmountedRe-register the listener when the component mounts again
Duplicate presence eventsMultiple listeners registered with the same or different IDsEnsure you remove old listeners before adding new ones
lastActiveAt is 0 or nullUser has never been online or data not yet availableHandle this case in your UI with a fallback like “Never active”

Next Steps