> ## 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 real-time user online/offline status and configure presence subscriptions using the CometChat iOS SDK.

<Accordion title="AI Integration Quick Reference">
  ```swift theme={null}
  // Subscribe to presence during init
  let appSettings = AppSettings.AppSettingsBuilder()
      .subscribePresenceForAllUsers()  // or .subscribePresenceForFriends() / .subscribePresenceForRoles(_:)
      .setRegion(region: "us")
      .build()

  // Listen for presence changes via CometChatUserDelegate
  func onUserOnline(user: User) { }
  func onUserOffline(user: User) { }

  // Stop listening
  CometChat.userdelegate = nil
  ```
</Accordion>

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:

| Method                           | Description                                                  |
| -------------------------------- | ------------------------------------------------------------ |
| `subscribePresenceForAllUsers()` | Receive presence updates for all users                       |
| `subscribePresenceForRoles(_:)`  | 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/ios/setup) for details.
</Note>

## Presence Subscription

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    // Subscribe to ALL Users
    let appSettings = AppSettings.AppSettingsBuilder()
        .subscribePresenceForAllUsers()
        .setRegion(region: "us")
        .build()

    CometChat.init(appId: APP_ID, appSettings: appSettings, onSuccess: { success in
        print("CometChat initialized with presence for all users")
    }, onError: { error in
        print("Error: \(error.errorDescription)")
    })
    ```
  </Tab>
</Tabs>

### Subscribe to Friends Only

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let appSettings = AppSettings.AppSettingsBuilder()
        .subscribePresenceForFriends()
        .setRegion(region: "us")
        .build()
    ```
  </Tab>
</Tabs>

### Subscribe to Specific Roles

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let appSettings = AppSettings.AppSettingsBuilder()
        .subscribePresenceForRoles(roles: ["admin", "moderator"])
        .setRegion(region: "us")
        .build()
    ```
  </Tab>
</Tabs>

## CometChatUserDelegate

Register a `CometChatUserDelegate` to receive real-time presence events:

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    class ViewController: UIViewController, CometChatUserDelegate {
        
        override func viewDidLoad() {
            super.viewDidLoad()
            CometChat.userdelegate = self
        }
        
        // Called when a user comes online
        func onUserOnline(user: User) {
            print("\(user.name ?? "") is now online")
        }
        
        // Called when a user goes offline
        func onUserOffline(user: User) {
            print("\(user.name ?? "") is now offline")
        }
    }
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    @interface ViewController ()<CometChatUserDelegate>

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [CometChat setUserdelegate:self];
    }

    - (void)onUserOfflineWithUser:(User * _Nonnull)user {
        
        NSLog(@"%@ status becomes offline.",[user stringValue]);
    }

    - (void)onUserOnlineWithUser:(User * _Nonnull)user {
        
        NSLog(@"%@ status becomes online.",[user stringValue]);
    }
    ```
  </Tab>
</Tabs>

| Method                 | Parameter                              | Description                                |
| ---------------------- | -------------------------------------- | ------------------------------------------ |
| `onUserOnline(user:)`  | [`User`](/sdk/reference/entities#user) | Called when a subscribed user comes online |
| `onUserOffline(user:)` | [`User`](/sdk/reference/entities#user) | Called when a subscribed user goes offline |

<Warning>
  Always remove your delegate when the view is dismissed to prevent memory leaks and duplicate event handling.

  ```swift theme={null}
  CometChat.userdelegate = nil
  ```
</Warning>

## User List Presence

When fetching users via `UsersRequest`, each [`User`](/sdk/reference/entities#user) object includes presence fields:

| Field          | Description                                                 |
| -------------- | ----------------------------------------------------------- |
| `status`       | `.online` or `.offline`                                     |
| `lastActiveAt` | Timestamp of last activity (useful for "Last seen" display) |

***

## Next Steps

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

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

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

  <Card title="All Real-time Listeners" icon="tower-broadcast" href="/sdk/ios/all-real-time-delegates-listeners">
    Overview of all available real-time listeners.
  </Card>
</CardGroup>
