> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-angular-v5-docs-update.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# User Presence

> Track when users come online or go offline in real-time using CometChat's presence subscription system.

<Accordion title="AI Integration Quick Reference">
  ```dart theme={null}
  // 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");
  ```
</Accordion>

Track whether users are online or offline in real-time.

<Note>
  **Available via:** SDK | [REST API](https://api-explorer.cometchat.com) | [UI Kits](/ui-kit/flutter/overview)
</Note>

## 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:

| Method                                          | Description                                                  |
| ----------------------------------------------- | ------------------------------------------------------------ |
| `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.

<Note>
  You must configure presence subscription in `AppSettings` during `CometChat.init()` before any presence events will be delivered. See [Setup SDK](/sdk/flutter/setup) for details.
</Note>

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.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    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
    }


    }
    ```
  </Tab>
</Tabs>

| Parameter    | Description                                                                                     |
| ------------ | ----------------------------------------------------------------------------------------------- |
| `listenerID` | An ID that uniquely identifies that listener. We recommend using the activity or fragment name. |

### UserListener Events

| Event                      | Description                                   |
| -------------------------- | --------------------------------------------- |
| `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`](/sdk/reference/entities#user) object with presence information.

Relevant fields to access on returned users:

| Field          | Type     | Description                                           |
| -------------- | -------- | ----------------------------------------------------- |
| `status`       | `String` | Online status of the user (`"online"` or `"offline"`) |
| `lastActiveAt` | `int`    | Timestamp when the user was last active               |

Remove the listener when no longer needed:

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    String listenerID = "UNIQUE_LISTENER_ID";

    CometChat.removeUserListener(listenerID);
    ```
  </Tab>
</Tabs>

<Warning>
  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.
</Warning>

## 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](/sdk/flutter/retrieve-users), in the [User](/sdk/flutter/user-management) 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.

2. `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

<CardGroup cols={2}>
  <Card title="Retrieve Users" icon="users" href="/sdk/flutter/retrieve-users">
    Fetch user lists with filtering and pagination
  </Card>

  <Card title="User Management" icon="user-plus" href="/sdk/flutter/user-management">
    Create and update users programmatically
  </Card>

  <Card title="Connection Status" icon="wifi" href="/sdk/flutter/connection-status">
    Monitor SDK connection to CometChat servers
  </Card>

  <Card title="Block Users" icon="ban" href="/sdk/flutter/block-users">
    Block and unblock users in your app
  </Card>
</CardGroup>
