Skip to main content
Quick Reference for AI Agents & Developers
  • Subscribe presence: AppSettings.AppSettingsBuilder().subscribePresenceForAllUsers() or .subscribePresenceForFriends() or .subscribePresenceForRoles(_:)
  • Listen for presence: onUserOnline(_:), onUserOffline(_:) in CometChatUserDelegate
  • User status: user.status.online or .offline
  • Last active: user.lastActiveAt — timestamp of last activity
  • Related: Retrieve Users · Connection Status · Users Overview
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 the SDK 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 that particular session of the app.

Presence Subscription

Configure presence subscription during SDK initialization using AppSettings.

AppSettingsBuilder Subscription Methods

Builder Configuration:Subscription Examples:
  • If none of the subscription methods are used, NO presence updates will be sent
  • Subscription is set per session (app launch)
  • Must be configured BEFORE CometChat.init()

Subscribe to Friends Only

Subscribe to Specific Roles

CometChatUserDelegate

Listen for real-time presence updates by conforming to CometChatUserDelegate.

Delegate Methods

Event Trigger: Received via CometChatUserDelegate.onUserOnline(user:)User Object Properties:
Event Trigger: Received via CometChatUserDelegate.onUserOffline(user:)User Object Properties:
  • Set delegate in viewDidLoad(): CometChat.userdelegate = self
  • Only receives updates for subscribed users (based on AppSettings)
  • Remove delegate when view is dismissed to avoid memory leaks

Start Listening for Presence

Setup Configuration:Listening Status:Events Waiting:

Stop Listening for Presence

Cleanup Configuration:Listening Status:

Check User Status

Request Parameters:Success Response (User Object):Error Response (CometChatException):

Get Online Users

Request Parameters:Success Response (Array of User Objects):Response Summary:Error Response (CometChatException):

Get Offline Users

Request Parameters:Success Response (Array of User Objects):Response Summary:Sample User Entries:Error Response (CometChatException):

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 fetch the list of users, in the User object, you will receive 2 fields:
  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 the “Last seen” of the user if needed.
User Object with Presence Data:Multiple Users Example:

Calculating “Last Seen”

Input Parameters:Calculation Variables:Output Examples:

UserStatus Enum

Common Error Codes