ChatClient.Builder(apiKey, context)
.config(
ChatCoreConfig(
offlineEnabled = true,
ignoredOfflineChannelTypes = emptySet(),
)
)
.build()Offline Support
Offline support provides a persistence layer that offers the following benefits:
- Decreases initial load times, especially when the network connection is poor.
- Enables the user to see cached channels and messages while offline.
- Enables the user to perform actions such as sending messages and reactions while offline.
Configuration
In most cases we strongly recommend using offline support as it significantly improves the user experience, however, certain types of applications (for example focused on livestreaming) don't require it.
Offline support is built into the stream-chat-android-client library and enabled by default. To configure it, use the config() method on ChatClient.Builder:
new ChatClient.Builder(apiKey, context)
.config(new ChatCoreConfig(
true, // offlineEnabled
Collections.emptySet() // ignoredOfflineChannelTypes
))
.build();The ChatCoreConfig allows you to configure the following offline options:
- offlineEnabled: Controls whether offline support is active. When enabled (default:
true), channels, messages and reactions are persisted to local storage. Set tofalseto disable offline caching entirely. - ignoredOfflineChannelTypes: A set of channel type strings to exclude from offline storage. Use this to skip caching for channel types that don't benefit from offline support (e.g., livestream channels with high message volume).
That's it. From now on, users' channels, messages and reactions will be saved into offline storage, so they can interact with the chat without an Internet connection.
Supported Operations
Besides storing lists of channels, messages or reactions, offline support allows you to perform the following actions without an Internet connection:
- Sending, updating and deleting a message
- Sending and deleting reactions
Clearing Offline Storage
The offline storage is persisted even when the application has been killed. You can clear it during user disconnection by passing the flushPersistence = true flag:
chatClient.disconnect(flushPersistence = true).enqueue { result ->
when(result) {
is Result.Success -> {
// Handle success
}
is Result.Failure -> {
// Handle error
}
}
}boolean flushPersistence = true;
chatClient.disconnect(flushPersistence).enqueue((result) -> {
if (result.isSuccess()) {
// Handle success
} else {
// Handle error
}
});This will wipe all of the user's data from the storage.