TypeScript Live Objects/Collections
In the Amity TypeScript SDK, we have the concept of Live Object and Live Collection.
Live Object is represented by an instance of Amity Object. It helps to observe changes in a single object.
Live Collection is represented by an instance of Amity LiveCollection. It helps to observe changes in a list of objects.
For example: Amity.Post or Amity.LiveCollection<Amity.Post>
SDK handles lots of data received from various sources. Data can be present in local cache. It may also be queried from the server or received from real-time event. What this means is that the same data is constantly updating. The data that you are accessing at the moment can get updated and become out of sync.
Live Object and Live Collection helps in syncing data so you will always get the most recent one. New data gets automatically collected everytime when there is an updation and user need not refresh to get the recent data.
Autocollection is available for the following functionalities in user/community feeds:
  • Post Collection
  • Comment Collection
  • Reactions Collection (for Messages)
  • Followers/Following Collection
  • Message Collection
Live collection does not support global feed and custom post ranking.
Amity.LiveCollection is a observer function that keeps track of a collection of posts/comments/reactions.

Post Collections

Follow the below code for Live Post collection.
Note: To learn more about the livePosts parameters and its datatypes, click Posts.
Live Posts Collection

Comment Collections

Follow the below code for Live Comments collection
Note: To learn more about the liveComments parameters and its datatypes, click Comments.
Live Comments Collection

Reaction Collections

Follow the below code for Live Reaction collection.
Note: To learn more about the liveReactions parameters and its datatypes, click Reactions.
Live Reactions Collection

Followers/Following Collections

Follow the below code for Live Followers/Following collection.
Live Followers Collection
Live Following Collection

Message Collection

Follow the below code for Live Message collection.
Both Amity Object and Amity LiveCollection provide methods that help observe changes in objects. TypeScript SDK relies on an internal Cache system. When a user loads the application, the SDK will do an internal query and retrieve cached data. If the data has been fetched recently and has not expired, the query will return the data in cache without fetching it from the server. If the data has expired, the app will query the server to get fresh data and update the cache. By default, data is considered valid if it is residing in the cache memory for not more than 120,000 ms (two minutes). Users can change the storage duration limit by themselves. Live Collection and Live Object events are notified by the network layer rather than the cache layer.

How Change Notifications are Broadcasted

Change notifications may originate from a variety of sources such as MQTT events, HTTP responses and Socket. Regardless of the source of change, however, Live Object and Live Collection must be able to observe data associated with its instance. The data source acts as an event emitter. The observing Live Object or Live Collection will retrieve the change (if any) that was made.
On TS SDK, changes that are occurred by actions of the current user (eg. creating a post, deleting a post), and HTTP server response will be broadcast via “Virtual event“ (VE). MQTT events will be broadcast via “Real-time event” (RTE). Essentially, virtual event, and real-time event broadcasters are equivalent.
To enable the ability to add new items, and modify a collection from the data source on TS SDK, a trigger occurs from the RTE and VE directly. RTE and VE will notify the observing live collection to re-query the current result from the cache and combine it with the new RTE or VE data then persist the updated result to the cache. The Live Collection will automatically observe VE, whilst RTE requires users to manually subscribe to relevant topics by themselves.

How Live Collections are Filtered and Sorted

The Live Collection sorts and filters from the query parameters. Once the Live Collection receives the new event, it will filter and sort the items accordingly.
Export as PDF
Copy link
Outline