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 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

// Subscribe to community events
// There are 4 events: .community, .posts, .comments, .postsAndComments
let eventTopic1 = AmityCommunityTopic(community: community, andEvent: .community)
subscriptionManager.subscribeTopic(eventTopic1) { success, error in
    // Handle Result
}

AmityPostTopic

  • post - subscription to changes of the post object

  • comments - subscription to changes of comment objects on the post

// Subscribe to post events
let eventTopic1 = AmityPostTopic(post: post, andEvent: .post)
subscriptionManager.subscribeTopic(eventTopic1) { success, error in
    // Handle Result
}

// Subscribe to comment events
let eventTopic2 = AmityPostTopic(post: post, andEvent: .comments)
subscriptionManager.subscribeTopic(eventTopic2) { success, error in
    // Handle Result
}

AmityCommentTopic

  • comment - subscription to changes of the comment object

// Subscribe to comment events
let eventTopic1 = AmityCommentTopic(comment: comment, andEvent: .comment)
subscriptionManager.subscribeTopic(eventTopic1) { success, error in
    // Handle results
}

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

// There are 4 user events: .user, .posts, .comments, and .postsAndComments 

// Subscribe to user events
let eventTopic1 = AmityUserTopic(user: user, andEvent: .user)
subscriptionManager.subscribeTopic(eventTopic1) { success, error in
    // Handle the result
}

// Subscribe to user posts
let eventTopic2 = AmityUserTopic(user: user, andEvent: .posts)
subscriptionManager.subscribeTopic(eventTopic2) { success, error in
    // Handle the result
}

// Subscribe to user comments
let eventTopic3 = AmityUserTopic(user: user, andEvent: .comments)
subscriptionManager.subscribeTopic(eventTopic3) { success, error in
    // Handle the result
}

// Subscribe to user posts and comments
let eventTopic4 = AmityUserTopic(user: user, andEvent: .postsAndComments)
subscriptionManager.subscribeTopic(eventTopic4) { success, error in
    // Handle the result
}

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

// Subscribe to user follow/following events
// There are 2 events: .myFollowers & .myFollowing
let eventTopic1 = AmityFollowTopic(event: .myFollowers)
subscriptionManager.subscribeTopic(eventTopic1) { success, error in
    // Handle results
}

Subscribe and Unsubscribe

Using AmityTopicSubscription class, you can subscribe and unsubscribe to any particular topic.

// construct topic to subscribe subscribe to changes
let topic = AmityFollowTopic(event: .myFollowers)
let subscription = AmityTopicSubscription(client: <client>)
subscription.subscribeTopic(topic) { success, error in
    //...
}

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