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
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 save the binary data natively in your persistent database.
Your database should have a table to save and load messages, which has columns corresponding to the properties of a message object.
A basic table to store messages contains the following columns:
- By using the
getMessagesByTimestamp:params:completionHandler:method, you can fetch new messages in a channel. With the returned messages, serialize and insert each message into your database. However, we recommend storing the message ID, timestamp, and channel URL in separate columns using
message.channelUrl. This allows you to query the data on a row-by-row basis later on.
- Before loading messages within 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:completionHandler: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.
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
A basic table to store channels contains the following columns:
- After fetching new channels by using a
SBDGroupChannelListQuery, serialize and insert each channel into your database. We recommend storing the channel URL and timestamp of the last message in separate columns by using
channel.lastMessage.createdAt. 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, or 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 Sendbird server, your client app should check information about changes to the channels regularly and apply the changes to the database. You can retrieve change logs of the current user's group channels by using the
getMyGroupChannelChangeLogsByTimestamp:customTypes:completionHandler:method, with which you can manage your local database updates.
Note: A similar process can be applied to the