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:
Subscription Topic
Each topic contains events enum which client can select to subscribe upon business context and interests.

AmityCommunityTopic

  • community - subscription to changes of the community object
  • posts - subscription to changes of post objects in the community
  • comments - subscription to changes of comment objects in the community
  • postAndComments - subscription to changes of post and comment objects in the community
1
// Subscribe to community events
2
// There are 4 events: .community, .posts, .comments, .postsAndComments
3
let eventTopic1 = AmityCommunityTopic(community: community, andEvent: .community)
4
subscriptionManager.subscribeTopic(eventTopic1) { success, error in
5
// Handle Result
6
}
Copied!

AmityPostTopic

  • post - subscription to changes of the post object
  • comments - subscription to changes of comment objects on the post
1
// Subscribe to post events
2
let eventTopic1 = AmityPostTopic(post: post, andEvent: .post)
3
subscriptionManager.subscribeTopic(eventTopic1) { success, error in
4
// Handle Result
5
}
6
7
// Subscribe to comment events
8
let eventTopic2 = AmityPostTopic(post: post, andEvent: .comments)
9
subscriptionManager.subscribeTopic(eventTopic2) { success, error in
10
// Handle Result
11
}
Copied!

AmityCommentTopic

  • comment - subscription to changes of the comment object
1
// Subscribe to comment events
2
let eventTopic1 = AmityCommentTopic(comment: comment, andEvent: .comment)
3
subscriptionManager.subscribeTopic(eventTopic1) { success, error in
4
// Handle results
5
}
Copied!

AmityUserTopic

  • user - subscription to changes of the user object
  • posts - subscription to changes of post objects in the user feed
  • comments - subscription to changes of comment objects in the user feed
  • postAndComments - subscription to changes of post and comment objects in the user feed
1
// There are 4 user events: .user, .posts, .comments, and .postsAndComments
2
3
// Subscribe to user events
4
let eventTopic1 = AmityUserTopic(user: user, andEvent: .user)
5
subscriptionManager.subscribeTopic(eventTopic1) { success, error in
6
// Handle the result
7
}
8
9
// Subscribe to user posts
10
let eventTopic2 = AmityUserTopic(user: user, andEvent: .posts)
11
subscriptionManager.subscribeTopic(eventTopic2) { success, error in
12
// Handle the result
13
}
14
15
// Subscribe to user comments
16
let eventTopic3 = AmityUserTopic(user: user, andEvent: .comments)
17
subscriptionManager.subscribeTopic(eventTopic3) { success, error in
18
// Handle the result
19
}
20
21
// Subscribe to user posts and comments
22
let eventTopic4 = AmityUserTopic(user: user, andEvent: .postsAndComments)
23
subscriptionManager.subscribeTopic(eventTopic4) { success, error in
24
// Handle the result
25
}
Copied!
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 follows
  • myFollowing - subscription to changes related users that follows current user
1
// Subscribe to user follow/following events
2
// There are 2 events: .myFollowers & .myFollowing
3
let eventTopic1 = AmityFollowTopic(event: .myFollowers)
4
subscriptionManager.subscribeTopic(eventTopic1) { success, error in
5
// Handle results
6
}
Copied!

Subscribe and Unsubscribe

Using AmityTopicSubscription class, you can subscribe and unsubscribe to any particular topic.
Create subscription manager
Subscribe on a topic
1
// construct topic to subscribe subscribe to changes
2
let topic = AmityFollowTopic(event: .myFollowers)
3
let subscription = AmityTopicSubscription(client: <client>)
4
subscription.subscribeTopic(topic) { success, error in
5
//...
6
}
Copied!
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.
Subscribe via object-based API
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.
Subscribe and observe changes

Unsubscribe

Similarly, for unsubscription, you can use unsubscribeTopic(...) method from AmityTopicSubscription class or use unsubscribeEvent(:_) method from the model itself.
Unsubscribe a topic
If logout() method is called anytime, the current session will be destroyed and all existing subscriptions will be removed.