Quick Reference for AI Agents & Developers
- Subscribe presence:
AppSettings.AppSettingsBuilder().subscribePresenceForAllUsers()or.subscribePresenceForFriends()or.subscribePresenceForRoles(_:) - Listen for presence:
onUserOnline(_:),onUserOffline(_:)inCometChatUserDelegate - User status:
user.status—.onlineor.offline - Last active:
user.lastActiveAt— timestamp of last activity - Related: Retrieve Users · Connection Status · Users Overview
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 usingAppSettings.
- Swift
AppSettingsBuilder Subscription Methods
Sample Payload - AppSettingsBuilder
Sample Payload - AppSettingsBuilder
Builder Configuration:
Subscription Examples:
Subscribe to Friends Only
- Swift
Subscribe to Specific Roles
- Swift
CometChatUserDelegate
Listen for real-time presence updates by conforming toCometChatUserDelegate.
- Swift
- Objective C
Delegate Methods
Sample Payload - onUserOnline Event
Sample Payload - onUserOnline Event
Event Trigger: Received via
CometChatUserDelegate.onUserOnline(user:)User Object Properties:Sample Payload - onUserOffline Event
Sample Payload - onUserOffline Event
Event Trigger: Received via
CometChatUserDelegate.onUserOffline(user:)User Object Properties:Start Listening for Presence
- Swift
Sample Payload - Start Listening
Sample Payload - Start Listening
Setup Configuration:
Listening Status:
Events Waiting:
Stop Listening for Presence
- Swift
Sample Payload - Stop Listening
Sample Payload - Stop Listening
Cleanup Configuration:
Listening Status:
Check User Status
- Swift
Sample Payload - Check User Status
Sample Payload - Check User Status
Get Online Users
- Swift
Sample Payload - Get Online Users
Sample Payload - Get Online Users
Request Parameters:
Success Response (Array of User Objects):
Response Summary:
Error Response (CometChatException):
Get Offline Users
- Swift
Sample Payload - Get Offline Users
Sample Payload - 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 theUser object, you will receive 2 fields:
-
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.
-
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.
Sample Payload - User List with Presence
Sample Payload - User List with Presence
User Object with Presence Data:
Multiple Users Example:
Calculating “Last Seen”
- Swift
Sample Payload - Last Seen Calculation
Sample Payload - Last Seen Calculation
Input Parameters:
Calculation Variables:
Output Examples: