---
source_url: https://www.pubnub.com/docs/chat/community-supported/android/chat-provider
title: Chat Provider for PubNub Chat Components for Android
updated_at: 2026-06-23T11:41:45.067Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Chat Provider for PubNub Chat Components for Android

`ChatProvider` is the general interface for UI and data operations across the component library. It provides default implementations of themes, repositories, services, and utils for the composition tree through [CompositionLocalProvider](https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal).

You can easily configure `ChatProvider` to synchronize data from different services, including messages and channel occupancy, by setting its `synchronize` parameter to `true`.

:::tip Optimized data flow
[Jetpack Compose](https://developer.android.com/jetpack/compose/documentation) passes data through the composition tree explicitly using parameters for composable functions. Using a tree is most often the simplest way to implement a data flow between services in your application.
:::

## Configuration

You can configure `ChatProvider` using the following parameters.

:::note Required parameters
Required parameters don't have default values. If a parameter has a default value, it's optional.
:::

| Parameter | Default value | Description |
| --- | --- | --- |
| pubNub | PubNub | Optional |  | n/a | PubNub instance |
| database | PubNubDatabase<MessageDao<DBMessage, | Optional |  | `Database.initialize(LocalContext.current)` | Database object. It's used by all the repositories for CRUD operations (Create, Read, Update, Delete). |
| channel | ChannelId | Optional |  | `"channel.lobby"` | ID of the channel to subscribe to. |
| synchronize | Boolean | Optional |  | `true` | Parameter for data synchronization. If set to `true`, it will synchronize data in `MessageService` and `OccupancyService`. |
| content | @Composable() | Optional |  | n/a | Composable content to draw. |

## Persistent database

For the repositories to work, `ChatProvider` creates the `SQLite` database. Additionally, it uses the `Room` library to provide an abstraction layer over `SQLite` and allow more robust data access.

To learn more about `Room`, refer to the [official Android docs](https://developer.android.com/jetpack/androidx/releases/room).

### Structure

To be able to use repositories, `ChatProvider` creates a default database structure. The database object must implement the `PubNubDatabase` interface. This interface allows repositories to operate on predefined data access objects (DAOs).

The following DAOs are declared:

* `MessageDao` contains all the chat messages.
* `MessageActionDao` contains all message reactions.
* `ChannelDao` contains all the channels.
* `MemberDao` contains user objects.
* `MembershipDao` contains the relation between members and channels.
* `RemoteTimetokenDao` contains all the synchronization time windows for channels.

```kotlin
interface PubNubDatabase<Message : MessageDao<*, *>, Action : MessageActionDao<*>, Channel : ChannelDao<*, *>, Member : MemberDao<*, *>, Membership : MembershipDao<*>, RemoteKey : RemoteTimetokenDao<*>> {
    fun messageDao(): Message
    fun actionDao(): Action
    fun channelDao(): Channel
    fun memberDao(): Member
    fun membershipDao(): Membership
    fun remoteKeyDao(): RemoteKey
}
```

### Default initialization

To initialize the database with the default structure, you can use the `Database.initialize(context)` helper method. You must call this method with the application context as a parameter.

| Parameter | Default value | Description |
| --- | --- | --- |
| `applicationContext`Type: `Context` | n/a | Application context needed to create a database. |
| `builder`Type: `(RoomDatabase.Builder<DefaultDatabase>) -> RoomDatabase.Builder<DefaultDatabase>` | `{ it }` | Room database builder. You can use it to add callbacks or initialize data. |

```kotlin
fun initialize(
  applicationContext: Context,
  builder: (RoomDatabase.Builder<DefaultDatabase>) -> RoomDatabase.Builder<DefaultDatabase> = { it }
): DefaultDatabase
```

#### Basic example

Call `initialize()` from the `onCreate()` method in `Application()`.

```kotlin
class ChatComponentsApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        Database.initialize(this)
    }
}
```

#### Callback example

To use the `Room` callback, you must pass it as a parameter. In the example, the message is logged after creating and opening the database.

```kotlin
class ChatComponentsApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        Database.initialize(applicationContext) { database ->
            database.addCallback(
                object : RoomDatabase.Callback() {
                    override fun onOpen(db: SupportSQLiteDatabase) {
                        super.onOpen(db)
                        Log.d("Database", "onOpen called")
                    }

                    override fun onCreate(db: SupportSQLiteDatabase) {
                        super.onCreate(db)
                        Log.d("Database", "onCreate called")
                    }
                }
            )
        }
    }
}
```

#### Standalone usage

PubNub Chat Components for Android offer an option to use a local database and access repositories without the Chat Provider dependency and the need to create a PubNub instance. To access the repositories this way, use the `RepositoryProvider` method. This method sets the default values for all the [repositories](#repositories). This functionality is helpful for login screens when you cannot initialize a PubNub instance because the user is yet unknown.

```kotlin
RepositoryProvider(ChatComponentsApplication.database.asPubNub()) {
  Login.View()
}
```

## Local providers

Providers created in `ChatProvider` can be grouped by one of these types:

* [Common](#common)
* [Utils](#utils)
* [Themes](#themes)
* [Repositories](#repositories)
* [Services](#services)

### Common

Providers responsible for storing unspecified data:

* `LocalPubNub` holds a `PubNub` instance.
* `LocalChannel` holds the current chat channel.
* `LocalUser` holds the current user object.

### Utils

Providers responsible for formatting and error handling:

* `LocalMemberFormatter` transforms a user ID into a `member` object.
* `LocalLogger` can be used for handling logs across all the components.

### Themes

List of providers used for styling components:

* `LocalMessageInputTheme` describes the style for the `MessageInput` component.
* `LocalTypingIndicatorTheme` describes the style for the `TypingIndicator` component.
* `LocalChannelListTheme` describes the style for the `ChannelList` component.
* `LocalMemberListTheme` describes the style for the `MemberList` component.
* `LocalMessageListTheme` describes the style for the `MessageList` component.
* `LocalMessageTheme` describes the style for the `Message` object.
* `LocalReactionTheme` describes the style for the `MessageAction` object.
* `LocalIndicatorTheme` describes the style for the `PresenceIndicator` state.
* `LocalProfileImageTheme` describes the style for the `ProfileImage` component.
* `LocalMenuItemTheme` describes the style for the `MenuItem` object.

### Repositories

List of repository providers:

* `LocalChannelRepository` provides an instance of `ChannelRepository`.
* `LocalMessageRepository` provides an instance of `MessageRepository`.
* `LocalMessageActionRepository` provides an instance of `MessageActionRepository`.
* `LocalMemberRepository` provides an instance of `MemberRepository`.
* `LocalMembershipRepository` provides an instance of `MembershipRepository`.
* `LocalRemoteTimetokenRepository` provides an instance of `RemoteTimetokenRepository`.

### Services

List of service providers:

* `LocalChannelService` provides the `ChannelService` implementation.
* `LocalMessageService` provides the `MessageService` implementation.
* `LocalOccupancyService` provides the `OccupancyService` implementation.
* `LocalActionService` provides the `ActionService` implementation.
* `LocalMessageReactionService` provides the `MessageActionService` implementation.
* `LocalTypingService` provides the `TypingService` implementation.