Skip to main content

Documentation Index

Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

// Configure presence subscription during init
AppSettings appSettings = (AppSettingsBuilder()
  ..subscriptionType = CometChatSubscriptionType.allUsers  // or .roles, .friends
  ..region = "REGION"
  ..autoEstablishSocketConnection = true
).build();

await CometChat.init("APP_ID", appSettings, onSuccess: (msg) {}, onError: (e) {});

// Listen for presence changes
CometChat.addUserListener("UNIQUE_LISTENER_ID", UserListener(
  onUserOnline: (User user) {
    debugPrint("${user.name} is online");
  },
  onUserOffline: (User user) {
    debugPrint("${user.name} is offline");
  },
));

// Remove listener when done
CometChat.removeUserListener("UNIQUE_LISTENER_ID");
Track whether users are online or offline in real-time.
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? Configure presence subscription in AppSettings during SDK initialization. The AppSettingsBuilder provides three subscription options:
MethodDescription
subscribePresenceForAllUsers()Receive presence updates for all users
subscribePresenceForRoles(List<String> 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. We suggest adding the listener in the init method of the activity or the fragment where you wish to receive these events in.
class Class_Name  with UserListener {

//CometChat.addUserListener("user_Listener_id", this);
    @override
void onUserOnline(User user) {
  // TODO: implement onUserOnline
}
@override
void onUserOffline(User user) {
  // TODO: implement onUserOffline
}


}
ParameterDescription
listenerIDAn ID that uniquely identifies that listener. We recommend using the activity or fragment name.

UserListener Events

EventDescription
onUserOnline(User user)Triggered when a subscribed user comes online
onUserOffline(User user)Triggered when a subscribed user goes offline
Each callback receives a User object with presence information. Relevant fields to access on returned users:
FieldTypeDescription
statusStringOnline status of the user ("online" or "offline")
lastActiveAtintTimestamp when the user was last active
Remove the listener when no longer needed:
String listenerID = "UNIQUE_LISTENER_ID";

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

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 the User object, you will receive 2 keys:
  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 a Last seen for that user.

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

Block Users

Block and unblock users in your app