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

# User Presence

> Track CometChat user presence in Flutter apps with subscriptions for all users, roles, friends, and real-time user listeners.

User Presence helps us understand if a user is available to chat or not.

## 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 initializing CometChat 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 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(List<String> 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 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 you wish to receive user events in, you need to register the `UserListener` using the `addUserListener()` method.

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

You will receive an object of the `User` class in the listener methods.

We recommend you remove the listener once the listener is not in use.

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

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

## 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#user-class) 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.
