For AI agents: Find a documentation index at https://talkjs.com/llms.txt (full content at https://talkjs.com/llms-full.txt). Get a markdown version of any page by appending .md to its URL path.

ConversationList

Display a list of conversations.

View as Markdown

The ConversationList component represents a conversation list UI, which displays a list of conversations that the current user is a participant of.

ConversationList

fun ConversationList(appId, userId, selectedConversationId, token, baseTheme, themePath, themeCustom, customCssPath, onSelectConversation, modifier, key, controller)

Parameters

appId
: String

Your app's unique TalkJS ID. You can find it on the Settings page of the TalkJS dashboard.

userId
: String

The ID of the current user.

If the given user ID doesn't exist, a "Something went wrong" message will be displayed.

selectedConversationId (optional)
: String?

The ID of the conversation currently selected in the list.

Changing the value of this parameter will cause the ConversationList UI to update.

Also update your local selectedConversationId state in onSelectConversation so that it stays in sync with the UI's internals.

Note that updating the selected conversation through this parameter will not trigger the onSelectConversation callback.

token (optional)
: String?

A token to authenticate the session with.

See the Authentication guide and Token reference for details and examples.

Required when authentication is enabled, otherwise optional.

baseTheme (optional)
: BaseTheme?

The base theme to start from.

Defaults to DEFAULT.

themePath (optional)
: String?

Path to a custom theme file, relative to the app's assets folder.

themeCustom (optional)
: JsonElement?

Arbitrary custom data passed down to the theme.

Whatever data you pass here will be made available for use in the theme module loaded via themePath.

customCssPath (optional)
: String?

Path to a custom CSS file, relative to the app's assets folder.

onSelectConversation (optional)
: ((event: SelectConversationEvent) -> Unit)?

Callback that triggers when the user selects a conversation from the list.

event is the event object containing the selected conversation.

Note that updating the selected conversation through the selectedConversationId parameter will not trigger this callback.

Use this callback to have an adjacent chatbox show the correct conversation. See Respond to a select conversation event.

modifier
: Modifier
key (optional)
: String?

A reference to the ConversationListController.

Returns

Unit

rememberConversationListController

fun rememberConversationListController(): ConversationListController

Returns

ConversationListController

class ConversationListController

A collection of actions available on the ConversationList UI.

Method Overview

constructor
selectConversation

Selects a conversation with the given ID, or de-select a conversation by passing null.

constructor

ConversationListController()

selectConversation

suspend fun conversationListController.selectConversation(conversationId, focusedMessageId)

Selects a conversation with the given ID, or de-select a conversation by passing null.

This method is called from the theme's ConversationListItem component when the user clicks on the given conversation. You can also call it programmatically from elsewhere to simulate a user click.

This method will trigger the onSelectConversation callback of ConversationList. In most cases, if you want to change the selected conversation programmatically from outside the conversation list, it's better to pass a different value to the selectedConversationId parameter of ConversationList instead, as that lets you keep your local state in sync with the parameters passed to the conversation list.

You can also optionally pass a focusedMessageId to signify the intent of scrolling to a particular message within that conversation.

Parameters

conversationId (optional)
: String?
focusedMessageId (optional)
: String?

Returns

Unit

Examples

The following examples show how to use the ConversationList component.

Create a conversation list with an existing user

To create a conversation list and pass it an existing user, use the following:

ConversationList(
    appId = "<APP_ID>",
    userId = "sample_user_alice",
)

Create a conversation list with a new user

The Kotlin Data API, which lets you read, subscribe to, and update your chat data, is included with Android Components.

Import getTalkSession into the file where you want to use it:

import com.talkjs.core.getTalkSession

Then create the session and user:

class ChatViewModel : ViewModel() {
    val appId = "<APP_ID>"
    val userId = "sebastian"

    init {
        val session = getTalkSession(appId = appId, userId = userId)

        viewModelScope.launch {
            session.currentUser.createIfNotExists(name = "Sebastian")
        }
    }
}

Pass the new user to your conversation list:

val viewModel = viewModel<ChatViewModel>()

ConversationList(
    appId = viewModel.appId,
    userId = viewModel.userId,
)

Respond to a select conversation event

Respond to a select conversation event by switching the chatbox to show the selected conversation.

When a user selects a conversation from the list, the component calls the onSelectConversation callback with the selected conversation’s details. You can handle this event to update a chatbox or perform another action.

var conversationId by remember { mutableStateOf<String?>("sample_conversation") }

Column {
    ConversationList(
        modifier = Modifier.weight(1f),
        appId = "<APP_ID>",
        userId = "sample_user_alice",
        selectedConversationId = conversationId,
        onSelectConversation = { event ->
            conversationId = event.conversation?.id
        },
    )

    Chatbox(
        modifier = Modifier.weight(1f),
        appId = "<APP_ID>",
        userId = "sample_user_alice",
        conversationId = conversationId,
    )
}