Database caching: Advanced
This page shows you how to build a local cache using a database. This has several advantages like storing raw data in a file and enabling particular queries on stored channels and messages. The content is provided on the assumption that you use
SQLite database as a local storage, you can easily follow these steps with any other database like
Serialize and deserialize Sendbird objects
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 your persistent database.
Save and load messages with serialization and deserialization
Your database should have a table to save and load messages, which has columns corresponding to the properties of a message object.
Design a message table
A basic table to store messages should contain the following columns:
- After fetching new messages by using a
MessageListQuery, serialize and insert each message into your database. However, we recommend that you store the message ID, timestamp, and channel URL in separate columns by using
message.getChannelUrl(). This allows you to query the data on a row-by-row basis later on.
- Before loading messages in a channel, sort rows in the chronological order by
message_ts. Then, deserialize each message and display them in your UI.
- When loading previous messages that are not currently stored in the local database, obtain the timestamp of the earliest stored message. Then, query for messages created before that value.
- Likewise, when loading new messages, query for messages with a later timestamp than the most recent message.
Note: To keep your local database in sync with Sendbird server’s data, your app should regularly check for changes made to messages in the server and apply those changes to the local cache accordingly. Because the
getMessageChangeLogsByTimestamp()methods are deprecated as of August 2021, you can manage local database updates by retrieving change logs of messages using the
Currently, it is difficult to sync deleted or edited messages. We are working to provide this feature in both our Chat SDKs and APIs, and hope to release it soon.
Save and load channels with serialization and deserialization
Your database should have a table to save and load channels, which has columns corresponding to the properties of a channel object.
Note: The following examples are based on a group channel. To cache an open channel, slightly improvise from the directions below (such as changing
Design a channel table
A basic table to store channels contains the following columns:
- After fetching new channels by using a
GroupChannelListQuery, serialize and insert each channel into your database. As with messages, we recommend storing the channel URL and timestamp of the last message in separate columns by using the
channel.getLastMessage().getCreatedAt(). This allows you to query the data on a row-by-row basis later on.
- Before loading a list of channels, sort rows in the chronological order by
last_message_ts. Then, deserialize each channel and display them in your UI.
- Unlike messages, channels are relatively few in number and go through frequent property changes, such as cover URL and name changes, their deletions. We recommend updating a cache by completely replacing with new data when possible.
- When real-time changes are made to a channel list, update your cache.
Note: In group channels, information associated with them can be updated or the current user might leave them anytime. To keep your local database synced with data in Sendbird server, your client app should check changes to the channels regularly and apply them to the database. You can retrieve change logs of the current user's group channels using
getMyGroupChannelChangeLogsByTimestamp()method, with which you can manage your local database updates.
Note: A similar process can be applied to the