Storing chat data locally within devices helps users retrieve their channels and messages even while offline. It can also prevent the inefficiency of repeating queries upon each connection or when device state changes, as well as provide a desirable user experience by reducing data loading delays.
In this File caching: Basic page and Database caching: Advanced page, you can see how to build a local cache by using object serialization and deserialization, which is provided through the Chat SDK. In the page, we provide instructions on how to build a simple cache that stores the most recent messages and channels in a file. In the Database caching: Advanced page, you can find instructions on caching chat data in an internal database, which enables you to store structured and queryable data.
Sendbird SyncManager is a library for chat data synchronization, which allows you to cache channels and messages of those channels in your client app reliably with a little effort. It listens to the events which happen on the channels in real-time and executes the background tasks for updating the cached data accordingly. You can use the cached data for a better user experience, such as retrieving old messages in a channel view without requesting to Sendbird server.
Note: The SyncManager includes
SQLiteas an internal database and operates based on it, which saves and stores the chat data synced with Sendbird server for caching.
This page shows you how to build a simple cache that stores a user's most recent channels and messages. A cache can be used to load data when an offline user attempts to view a list of channels, or enters a channel to view their chat history. Implementing just a basic cache like the following can improve user experience, as users get to see empty lists of channels or messages due to their unstable connection.
In the steps described below, we create a file per channel in your client app's cache directory, write serialized data into the file to store a set amount of recent messages, configure the app to first load messages from the cache, and then finally replace them when the newest results are successfully fetched from Sendbird server.
In order to enable you to store Sendbird objects such as messages, channels, and users in a local storage, we provide serialization and deserialization methods through our Chat SDK.
By using the
serialize() method to convert a Sendbird object to binary data like the following, you can store the data natively in a file.
With serialization, you can store a channel and its most recent messages in a file. In this case, we are encoding the binary serialized data into a
Base64 string. then storing each item in a new line. Normally, save data when
onStop() is called in your user's chat screen.
Note: Because messages in a channel can be updated or deleted, to keep your cache synced with data in Sendbird server, your client app should check information about changes to the messages regularly and apply the changes to the cache. You can retrieve change logs of messages in a channel by using the
getMessageChangeLogsByTimestamp()method, with which you can manage cache updates.
Note: In this case,
MD5 hashingis used to generate a hash file for each stored data file. Using this hash file, you can check if the newly generated data differs from the corresponding one already stored in the cache, preventing an unnecessary overwriting.
When your user enters a chat to view their message history, load saved messages from the cache.
After receiving an updated message list from Sendbird server, clear the current message list and replace it with the updated list. In effect, messages from the cache are overwritten almost instantly if the user's connection is normal.
We recommend that you only cache group channels in a local file rather than open channels. The property values and participant status of open channels can be frequently changed depending on your use case, and you might go through difficulty in syncing the local file with the changes.
The process of caching channels is similar to caching messages. For the sake of brevity, the following code is provided without additional explanations.
Note: In group channels, information associated with them can be updated or the current user might leave them anytime. To keep your cache synced with data in Sendbird server, your client app should check changes to the channels regularly and apply the changes to the cache. You can retrieve change logs of the current user's group channels by using
getMyGroupChannelChangeLogsByTimestamp()method, with which you can manage cache updates.