Realtime Events
SDK now supports updating values of various models through realtime events. For example, when a user updates his profile, it can be reflected automatically in another user's device. And this update will be reflected through the same live object/collection that the user is currently observing.
Currently, SDK supports realtime events for community, posts, comments, user, and follow/unfollow events.
Requirement
User is required to be a 'Member' of the community in order to subscribe to updates from the community or other updates from content created within the community.
Once user leaves the community, the user will no longer receive realtime events.
Topics
In order to subscribe to particular changes, you need to construct a topic to subscribe to. These are the four different topics:
AmityCommunityTopic
AmityUserTopic
AmityCommentTopic
AmityPostTopic
AmityFollowTopic
Example:
Each topic contains events enum which client can select to subscribe upon business context and interests.
AmityCommunityTopic
community
- subscription to changes of the community objectposts
- subscription to changes of post objects in the communitycomments
- subscription to changes of comment objects in the communitypostAndComments
- subscription to changes of post and comment objects in the community
AmityPostTopic
post
- subscription to changes of the post objectcomments
- subscription to changes of comment objects on the post
AmityCommentTopic
comment
- subscription to changes of the comment object
AmityUserTopic
user
- subscription to changes of the user objectposts
- subscription to changes of post objects in the user feedcomments
- subscription to changes of comment objects in the user feedpostAndComments
- subscription to changes of post and comment objects in the user feed
After topic creation, we need to subscribe to it. SDK provides a dedicated class -AmityTopicSubscription
to handle topic subscription and unsubscription.
AmityFollowTopic
myFollowers
- subscription to changes related users that current user followsmyFollowing
- subscription to changes related users that follows current user
Subscribe and Unsubscribe
Using AmityTopicSubscription
class, you can subscribe and unsubscribe to any particular topic.
SDK also exposes subscribeEvent()
and unsubscribeEvent()
method for convenience in AmityCommunity
, AmityPost
, AmityUser
and AmityComment
models. These methods help you to subscribe to a particular event without creating a topic.
The maximum limit for the number of topics that can be subscribed to is 20. Developers should maintain the list of subscriptions themselves and unsubscribe when it's not needed.
Example
If you have two screens, one is showing a list of communities and another is showing community details, you might want to subscribe when user is viewing community details in details screen and unsubscribe when user moves back to community list screen.
Even if you subscribe to the same topic and event multiple times i.e call subscribeEvent:
or subscribeTopic
with the same value multiple times, it will maintain only one subscription.
Observing Changes
Once you have subscribed using one of the methods mentioned above, SDK will start receiving data related to the event that you subscribed to. If there are any changes to the data or model that you are currently observing, you will be notified through the same observer block.
Unsubscribe
Similarly, for unsubscription, you can use unsubscribeTopic(...)
method from AmityTopicSubscription
class or use unsubscribeEvent(:_)
method from the model itself.
If logout()
method is called anytime, the current session will be destroyed and all existing subscriptions will be removed.
Last updated