Platform docs
Auth, users, webhooks & more

Chat Integration

Building Video Apps With Chat Support

One of the most common video app use cases is having a chat feature in your app that allows users to engage in text communication along with the video and audio call. This direct integration allows for simple transition between text and images to more complex media.

Stream supports this use case, out-of-the-box. In this guide, you'll walk through all the steps required to integrate our Video and Chat Android SDKs into a cohesive whole. You'll cover the following:

  • Adding Stream dependencies.
  • Creating Stream clients.
  • Authenticating Chat and Video users.
  • Building custom Call attachments and "Start Call" UI.

By the end of this guide, your app will look like this:

Video Chat Integration

You'll have the ability to create messaging conversations, in which you can start send text messages & attachments in the audio/video call.

Let's see how to implement this.

Creating the project

The demo app in the stream-video-android repository integrates both SDKs, and the code in this guide follows it. You can open the repository in Android Studio to explore a complete integration.

In your own project, add the Video and Chat dependencies to your app's build.gradle.kts file:

dependencies {
    // Stream Video Compose SDK
    implementation("io.getstream:stream-video-android-ui-compose:1.37.0")

    // Stream Chat
    implementation("io.getstream:stream-chat-android-compose:6.10.0")
    implementation("io.getstream:stream-chat-android-offline:6.10.0")
    implementation("io.getstream:stream-chat-android-state:6.10.0")
    implementation("io.getstream:stream-chat-android-ui-utils:6.10.0")
}

You'll be integrating two SDKs - Video and Chat. There is only one Video dependency right now, stream-video-android-ui-compose, used to gain access to Jetpack Compose components and the low level client of the Video project.

For chat, you have a few dependencies:

  • stream-chat-android-compose: Similar to Video, gives access to Jetpack Components for Chat.
  • stream-chat-android-offline: Used to integrate offline support in Chat.
  • stream-chat-android-state: Used to gain access to various state and its plugins for Chat.
  • stream-chat-android-ui-utils: A collection of helper functionalities to render custom UI or format data.

Now let's start integrating the SDKs.

Integrating the Chat SDK

The first step of integrating our Stream Chat SDK is initializing the ChatClient. You'll do that in the Application class, as it's recommended to initialize the client as soon as your app is launched. On top of that, you'll have to log in a user, to fetch their information and conversations.

Creating the Client

Initialize the ChatClient in your Application class:

import io.getstream.chat.android.client.ChatClient
import io.getstream.chat.android.client.logger.ChatLogLevel
import io.getstream.chat.android.offline.plugin.factory.StreamOfflinePluginFactory
import io.getstream.chat.android.state.plugin.config.StatePluginConfig
import io.getstream.chat.android.state.plugin.factory.StreamStatePluginFactory
import io.getstream.video.android.model.User

fun initializeStreamChat(
    user: User,
    token: String,
) {
    val offlinePlugin = StreamOfflinePluginFactory(this) // 1
    val statePluginFactory = StreamStatePluginFactory( // 2
        config = StatePluginConfig(
            backgroundSyncEnabled = true,
            userPresence = true,
        ),
        appContext = this
    )

    val logLevel = if (BuildConfig.DEBUG) ChatLogLevel.ALL else ChatLogLevel.NOTHING
    val chatClient = ChatClient.Builder(API_KEY, this) // 3
        .withPlugins(offlinePlugin, statePluginFactory)
        .logLevel(logLevel)
        .build()

    val chatUser = io.getstream.chat.android.models.User( // 4
        id = user.id,
        name = user.name.orEmpty(),
        image = user.image.orEmpty(),
    )

    chatClient.connectUser( // 5
        user = chatUser,
        token = token
    ).enqueue()
}

There are a few parts to this initialization:

  1. You create the StreamOfflinePluginFactory that's used to provide offline support to the ChatClient. This helps users browse cached channels and messages, even if their network connection is missing.
  2. Following that, you set up a StreamStatePluginFactory which builds on top of the offline support, to provide background data sync as well as user online presence data. This is used to enrich the UI with online indicators.
  3. To help you debug, you can provide a ChatLogLevel, which will print information about the API calls and data in the SDK.
  4. Once you're ready, you initialize the ChatClient. You pass in the API_KEY, the Context and the rest of the components. You can pass in the key for your environment, but in this example we'll use a predefined environment instead.

With this client initialization, you can proceed to log in as a user when you login into the signing page.

Note:

There are a few things that you should make sure of, you should use the same API Key, User Id, and Token when you initialize the Video and Chat SDKs.

Implementing ChatDialog

Now implement a ChatDialog Composable, which displays the chat messages over the Video call screen.

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Icon
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.material.ModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import io.getstream.chat.android.compose.ui.messages.MessagesScreen
import io.getstream.chat.android.compose.ui.theme.ChatTheme
import io.getstream.chat.android.compose.viewmodel.messages.MessageListViewModel
import io.getstream.chat.android.compose.viewmodel.messages.MessagesViewModelFactory
import io.getstream.video.android.compose.theme.VideoTheme
import io.getstream.video.android.core.Call

@OptIn(ExperimentalMaterialApi::class)
@Composable
internal fun ChatDialog(
    call: Call,
    state: ModalBottomSheetState,
    content: @Composable () -> Unit,
    updateUnreadCount: (Int) -> Unit,
    onDismissed: () -> Unit
) {
    val context = LocalContext.current
    val viewModelFactory = remember {
        MessagesViewModelFactory(
            context = context,
            channelId = "videocall:${call.id}",
        )
    }
    val listViewModel = viewModel(MessageListViewModel::class.java, factory = viewModelFactory)
    val unreadCount = listViewModel.currentMessagesState.unreadCount

    LaunchedEffect(key1 = unreadCount) {
        updateUnreadCount.invoke(unreadCount)
    }

    ChatTheme(isInDarkMode = true) {
        ModalBottomSheetLayout(
            modifier = Modifier.fillMaxWidth(),
            sheetShape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp),
            sheetState = state,
            sheetBackgroundColor = VideoTheme.colors.baseSheetPrimary,
            sheetContent = {
                if (state.isVisible) {
                    Column(
                        modifier = Modifier
                            .background(ChatTheme.colors.appBackground)
                            .fillMaxWidth()
                            .height(500.dp),
                    ) {
                        Icon(
                            modifier = Modifier
                                .align(Alignment.End)
                                .padding(16.dp)
                                .clickable { onDismissed.invoke() },
                            tint = ChatTheme.colors.textHighEmphasis,
                            painter = painterResource(id = io.getstream.video.android.ui.common.R.drawable.stream_video_ic_close),
                            contentDescription = null,
                        )

                        MessagesScreen(
                            showHeader = false,
                            viewModelFactory = viewModelFactory,
                            onBackPressed = { onDismissed.invoke() },
                        )
                    }
                }
            },
            content = content,
        )
    }
}

The block of code here is mostly straightforward, as you're just composing the Chat MessagesScreen, with some custom UI. Let's go over it:

  1. You create a MessagesViewModelFactory for the channel of the call, and observe the unread count from the MessageListViewModel.
  2. You initialize the ChatTheme for the messaging UI.
  3. You add a close icon on top of the sheet.
  4. Finally, you show the MessagesScreen, which renders the message list and the composer, without its header.

You have a full integration of chat features in your video app now. You can see the messages with the give video call id. Now, let's display the chat dialog by creating a custom control action.

Displaying the ChatDialog

So we've implemented the ChatDialog Composable function, and now you need to display the dialog by clicking an action button. Let's add an action button that shows the chat dialog on your call screen:

import androidx.compose.foundation.background
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material.rememberModalBottomSheetState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import io.getstream.video.android.compose.theme.VideoTheme
import io.getstream.video.android.compose.ui.components.call.activecall.CallContent
import io.getstream.video.android.compose.ui.components.call.controls.ControlActions
import io.getstream.video.android.compose.ui.components.call.controls.actions.CancelCallAction
import io.getstream.video.android.compose.ui.components.call.controls.actions.ChatDialogAction
import io.getstream.video.android.compose.ui.components.call.controls.actions.FlipCameraAction
import io.getstream.video.android.compose.ui.components.call.controls.actions.ToggleCameraAction
import io.getstream.video.android.compose.ui.components.call.controls.actions.ToggleMicrophoneAction
import kotlinx.coroutines.launch

val isCameraEnabled by call.camera.isEnabled.collectAsState()
val isMicrophoneEnabled by call.microphone.isEnabled.collectAsState()
var unreadCount by remember { mutableIntStateOf(0) }

val chatState = rememberModalBottomSheetState(
    initialValue = ModalBottomSheetValue.Hidden,
    skipHalfExpanded = true,
)
val scope = rememberCoroutineScope()

VideoTheme {
    ChatDialog(
        state = chatState,
        call = call,
        content = {
            CallContent(
                modifier = Modifier.background(color = VideoTheme.colors.baseSheetPrimary),
                call = call,
                onBackPressed = {
                    if (chatState.isVisible) {
                        scope.launch { chatState.hide() }
                    } else {
                        onLeaveCall.invoke()
                    }
                },
                controlsContent = {
                    ControlActions(
                        call = call,
                        actions = listOf(
                            {
                                ChatDialogAction(
                                    messageCount = unreadCount,
                                    onCallAction = { scope.launch { chatState.show() } }
                                )
                            },
                            {
                                ToggleCameraAction(
                                    isCameraEnabled = isCameraEnabled,
                                    onCallAction = { call.camera.setEnabled(it.isEnabled) }
                                )
                            },
                            {
                                ToggleMicrophoneAction(
                                    isMicrophoneEnabled = isMicrophoneEnabled,
                                    onCallAction = { call.microphone.setEnabled(it.isEnabled) }
                                )
                            },
                            {
                                FlipCameraAction(
                                    onCallAction = { call.camera.flip() }
                                )
                            },
                            {
                                CancelCallAction(
                                    onCallAction = { onLeaveCall.invoke() }
                                )
                            },
                        ),
                    )
                }
            )
        },
        updateUnreadCount = { unreadCount = it },
        onDismissed = { scope.launch { chatState.hide() } }
    )
}

Here onLeaveCall is your own function that leaves the call, for example by calling call.leave() and closing the screen.

As you can see in the code above, the CallContent implements a custom controlsContent, which includes action buttons, such as toggling the camera/microphone, and displaying a chat dialog with an unread message badge. If you join a call, you'll see the call screen like the one below:

Video Chat Integration

If you click the "chat" action button, you'll see the chat dialog below:

Video Chat Integration