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 update and user need not refresh to get the recent data.
Live Collection is available for the following in user/community feeds:
- Post Collection
- Comment Collection
- Reactions Collection
- Followers/Following Collection
- Channel Collection
- Message Collection
- Channel Member Collection
- Community Collection
- Community Category Collection
- Community Objects
- Community Members Collection
Live Collection is not supported for global feed and custom post ranking.
Amity.LiveCollection
is a observer function that keeps track of a collection of posts/comments/reactions.Follow the below code for Live Post collection.
Live Posts Collection
Follow the below code for Live Comments collection.
Live Comments Collection
Follow the below code for Live Reaction collection.
Live Reactions Collection
Follow the below code for Live Followers/Following collection.
Live Followers Collection
Live Following Collection
Follow the below code for Live Message collection.
Follow the below code for Live Channel Member collection.
Live Community Members allows for real-time event data changes to a collection of community members, including join/leave events, user bans, and role changes. Users can even query based on specific parameters such as membership status or user search.
Possible parameters:
membership?: ('banned' | 'member')[];
roles?: string[];
sortBy?
:firstCreated
,lastCreated
(i.e. initial date user joined / added to community)search?: Amity.User['displayName'] | Amity.User['userId'];
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.
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.
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.
Last modified 7d ago