Skip to main content
// 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");
Track whether users are online or offline in real-time.

Real-time Presence

Configure presence subscription in AppSettings during SDK initialization. The AppSettingsBuilder provides three subscription options:
MethodDescription
subscribePresenceForAllUsers()Receive presence updates for all users
subscribePresenceForRoles(roles)Receive presence updates only for users with specified roles
subscribePresenceForFriends()Receive presence updates only for friends
If none of these methods are called, no presence events will be delivered.
You must configure presence subscription in AppSettings during CometChat.init() before any presence events will be delivered. See Setup SDK for details.
Register a UserListener to receive presence events:
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
listenerIDUnique identifier for the listener
Each callback receives a User object with presence information. The listener callbacks provide a User object with presence fields populated. Access the response data using getter methods:
FieldGetterReturn TypeDescription
uidgetUid()stringUnique user ID
namegetName()stringDisplay name of the user
statusgetStatus()stringOnline status of the user ("online" or "offline")
lastActiveAtgetLastActiveAt()numberTimestamp when the user was last active
Remove the listener when no longer needed:
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

When fetching users via UsersRequest, each User object includes presence fields:
FieldDescription
status"online" or "offline"
lastActiveAtTimestamp of last activity (useful for “Last seen” display)

Next Steps

Retrieve Users

Fetch user lists with filtering and pagination.

User Management

Create and update users programmatically.

Connection Status

Monitor SDK connection to CometChat servers.

All Real-time Listeners

Overview of all available real-time listeners.