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.
You must configure presence subscription in AppSettings during CometChat.init() before any presence events will be delivered. See Setup SDK for details.
// Subscribe to ALL Userslet 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)")})
Register a CometChatUserDelegate to receive real-time presence events:
Swift
Objective C
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") }}