User Presence

The SDK also offers functionality to query and synchronize the presence of other users, enabling the creation of a UI that displays their online status. For example, you might use a green dot to indicate whether a particular user is online or offline.

Within the SDK, a user's presence is represented by the AmityUserPresence object, which contains three properties.



Id of the user


Timestamp when that user last synced its heartbeat with the server.


Convenient property to determine online status. User is considered online if its lastHeartbeat has been within last 60 seconds.

Query User Presence

The SDK enables users to query the presence state for a list of users, with a maximum limit of 220. The getUserPresence method from the AmityUserPresenceRepository class can be used to fetch a list of AmityUserPresence objects.

Sync & Unsync User Presence

In addition to querying user presence, the SDK also offers a method to periodically sync the presence of other users. This functionality keeps users informed about the online status of others. One application of this feature would be to display a list of users and provide an online indicator for those who are currently online.

Sync User Presence

The SDK includes the syncUserPresence(id, viewId) method in the AmityUserPresenceRepository class to synchronize the presence of another user. This method adds the specified ID to the list of user IDs for presence synchronization, occurring periodically at set intervals. To observe the results of this process, use the getSyncingUserPresence() method. When presence information is obtained for the currently syncing user IDs, notifications are sent through the observer returned from the getSyncingUserPresence() method. For further details on observing presence, refer to the Observe User Presence section.

For optimal use, particularly when displaying a list of users, call this method as a list item appears to synchronize the user's presence state. Conversely, utilize the unsyncUserPresence(id, viewId) method to stop synchronization when a list item is no longer visible, ensuring that the maximum sync limit is never reached.

The maximum number of user IDs that can be synced at a time is 20. If the count exceeds this limit, the SDK will log an error message to the console. To remove a user ID from the list, use the unsyncUserPresence(id:viewId:) method.

This syncUserPresence(id, viewId) method also provides an optional viewId parameter. viewId is the unique id of the view that this user id is tied to. In most of the cases, you do not need to specify any value to this parameter. viewId is useful if you want to bind same user id to multiple views in same screen. In that case, if you want to unsync presence for one view, it won't affect presence syncing for same user id in another view.

SDK will log a message in console incase of any error which occurs during syncing process.

Unsync User Presence

The SDK includes the unsyncUserPresence(id, viewId) method to cease syncing the presence of a specific user ID. When a user ID is unsynced, the SDK removes it from the current list of synced IDs, and the observer returned from the getSyncingUserPresence() method will no longer provide presence information for that user. The method also features an optional viewId parameter, functioning similarly to the syncUserPresence(id, viewId) method.

The SDK also provides a convenient method to unsync all user ids which are currently being synced. This is particularly useful when you don’t care about the user ids being synced. One usecase for this is when user navigates to another screen which do not care about syncing previously synced users at all. So you can call unsyncAllUserPresence() method when the previous screen disappears.

Observe User Presence

After adding users for synchronization, the next step involves observing their presence. The SDK offers an observer via the getSyncingUserPresence() method. Whenever presence information is retrieved for users synced through syncUserPresence(id, viewId), this information is published through the observer.

Accessing User Presence:

You can access the presence information for synced users in two ways:

  1. Through the Observer Returned from getSyncingUserPresence(): When the presence information is updated, the observer provides an array of [AmityUserPresence] for the user IDs being synced. Users need to map AmityUserPresence from this array to the view where the information is to be displayed.

  2. Through the AmityUser Object: When presence information is fetched from the server, the lastHeartbeat timestamp is mapped to the lastHeartbeat property of the AmityUser object, if available. Users can directly access the lastHeartbeat property from the AmityUser object to determine whether a user is online or offline.

Online Users Count

The SDK provides the getOnlineUsersCount method in the AmityUserPresenceRepository class to query the count of online users. Please note that this count does not update automatically.

Online Users Snapshot

The SDK provides a method getOnlineUsersSnapshot in AmityUserPresenceRepository to query list of online users in a network. The list of users can be accessed through users property which returns an array of AmityUser.

This snapshot is not auto updating & supports query of up to 1000 online users.

When this snapshot object is initialized, it fetches the snapshot of user presences of all online users in current network. Then it maps those information to AmityUser per page and provides list of AmityUser with pagination. Each page can contain max 20 users. When snapshot is returned, it will contains users for first page. You can still fetch more users if available through loadMore() method. Once more users are fetched, user can access those users through users property. You can also use canLoadMore property to determine if there are more online users which can be fetched.

Last updated