Home
/
Chat
/
Flutter

Message threading

Message threading is a feature that allows users to ask questions, give feedback or add context to a specific message without disrupting the conversation flow. A message thread refers to a collection of messages grouped together, consisting of a parent message and its replies. It can have the following elements:

  • A message can have a thread of replies.
  • A message that has a thread of replies is a parent message.
  • A parent message and its threaded replies are collectively called a message thread.
  • Every message within a thread, whether it's parent or reply, is a threaded message.
  • A message that doesn't have any replies is an unthreaded message.

Showing how message threading works in chat view.


Benefits

Message threading can be widely used to help conversations flow smoothly while keeping users engaged. Message replies in a thread are common among popular messaging platforms, such as Slack and iMessage.

  • Conversation flow: Without message threading, it's hard to specify which message a channel member is responding to. Users would have to explain the context of their response, potentially confusing other channel members and distracting them from reading new messages. Message threads can help everyone stay engaged and carry on their conversation without any interferences.

  • In-depth discussions: By allowing users to reply to each other's messages in a thread, they can have more in-depth conversations on one topic. They're able to ask follow-up questions or provide more detailed explanations to a specific message without interrupting others in the channel. Message threads can encourage users to start topic-specific discussions separate from the main channel conversation.


Limitations

Refer to the following limitations before using the message threading feature:

  • Message threading doesn't integrate with SyncManager.
  • SDK only supports 1-depth threads, meaning you can only add reply messages to non-reply messages. You can't add a reply to a reply message.
  • You can only reply to text and file messages, not admin messages. However, you can add admin type messages as a reply to another message.

List messages along with their replies

You can retrieve messages and their replies in a specific thread.

Load channel messages

The loadNext() method of a PreviousMessageListQuery instance returns a list of BaseMessage objects. With this method, you can retrieve previous messages in a specific channel. To include the replies of the target messages in the results, you need to change the value of the includeReplies property in the PreviousMessageListQuery instance.

List of properties

The following table lists the properties of the PreviousMessageListQuery class.

Property nameDescription

includeReplies

Type: bool
Determines whether to include replies in the results.

includeThreadInfo

Type: bool
Determines whether to include the thread information of the messages in the results when the results contain parent messages.

includeParentMessageText

Type: bool
Determines whether to include the parent message text in the results when the retrieved messages are replies in a thread. If the type of the parent message is UserMessage, the value is a message property. If it is FileMessage, the value is the name of the uploaded file.

Light Color Skin
Copy
final listQuery = PreviousMessageListQuery()
    ..channelType = channel.channelType
    ..channelUrl = channel.channelUrl
    ..includeReplies = INCLUDE_REPLIES
    ..includeThreadInfo = INCLUDE_THREAD_INFO
    ..includeParentMessageText = INCLUDE_PARENT_MESSAGE_TEXT;

Load previous messages with limit

You can also limit the number of returned messages or the order of the results.

Light Color Skin
Copy
try {
    final listQuery = PreviousMessageListQuery()
        ..channelType = channel.channelType
        ..channelUrl = channel.channelUrl
        ..limit = LIMIT
        ..reverse = REVERSE
        ..includeReplies = INCLUDE_REPLIES
        ..includeThreadInfo = INCLUDE_THREAD_INFO
        ..includeParentMessageText = INCLUDE_PARENT_MESSAGE_TEXT;

    final result = await listQuery.loadNext();
} catch (e) {
    // Handle error.
}

List of properties

Property nameDescription

limit

Type: int
Specifies the number of results to return per call. Acceptable values are 1 to 100, inclusive. The recommended value for this parameter is 30.

reverse

Type: bool
Determines whether to sort the retrieved messages in reverse order. If false, the results are in ascending order.

Load messages by timestamp or message ID

The getMessagesByTimestamp() and getMessagesById() methods of a BaseChannel instance returns a list of BaseMessage objects. By using these methods, you can retrieve messages in a specific channel according to a MessageListParams object.

List of properties

The following table lists the properties of the MessageListParams.

Property nameDescription

previousResultSize

Type: int
Specifies the number of messages to retrieve that were sent before the specified timestamp or message ID.

nextResultSize

Type: int
Specifies the number of messages to retrieve that were sent after the specified timestamp or message ID.

isInclusive

Type: bool
Determines whether to include the messages with the matching timestamp or message ID in the results.

reverse

Type: bool
Determines whether to sort the retrieved messages in reverse order. If false, the results are in ascending order.

includeReplies

Type: bool
Determines whether to include replies in the results.

includeThreadInfo

Type: bool
Determines whether to include the thread information of the messages in the results when the results contain parent messages.

includeParentMessageText

Type: bool
Determines whether to include the parent message text in the results when the messages are replies in a thread. If the type of the parent message is UserMessage, the value is a message property. If it is FileMessage, the value is the name of the uploaded file.

Light Color Skin
Copy
final params = MessageListParams()
    ..includeReplies = true
    ..includeParentMessageText = true
    ..includeThreadInfo = true
    ..previousResultSize = 10;

By timestamp

By using the getMessagesByTimestamp() method, you can retrieve a set number of previous and next messages on both sides of a specific timestamp in a channel.

Light Color Skin
Copy
try {
    final result = await channel.getMessagesByTimestamp(
        TIMESTAMP,
        params,
    );
    // The result includes replies.
} catch (e) {
    // Handle error.
}

List of arguments

ArgumentDescription

TIMESTAMP

Type: int
Specifies the timestamp to be the reference point for messages to retrieve, in Unix milliseconds format. Messages sent before or after the timestamp can be retrieved.

By message ID

By using the getMessagesById() method, you can retrieve a set number of previous and next messages on both sides of a specific message ID in a channel.

Light Color Skin
Copy
try {
    final result = await channel.getMessagesById(
        MESSAGE_ID,
        params,
    );
    // The result includes replies.
} catch (e) {
    // Handle error.
}

List of arguments

ArgumentDescription

MESSAGE_ID

Type: int
Specifies the message ID to be the reference point for messages to retrieve. Messages sent before or after the message with the matching message ID can be retrieved.


List replies of a parent message

You can retrieve replies of a parent message if you can determine the parent message like the following. First, create a ThreadMessageListParams object and set properties related to the thread where the target replies belong to.

Light Color Skin
Copy
final params = ThreadedMessageListParams()
    ..includeReplies = true
    ..includeParentMessageText = true
    ..includeThreadInfo = true
    ..previousResultSize = 10;

List of properties

The following table lists the properties of the ThreadMessageListParams.

Property nameDescription

previousResultSize

Type: int
Specifies the number of messages to retrieve that were sent before the specified timestamp.

nextResultSize

Type: int
Specifies the number of messages to retrieve that were sent after the specified timestamp.

isInclusive

Type: bool
Determines whether to include the messages with the matching timestamp in the results.

reverse

Type: bool
Determines whether to sort the retrieved messages in reverse order. If false, the results are in ascending order.

includeParentMessageText

Type: bool
Determines whether to include the parent message text in the results when the messages are replies in a thread. If the type of the parent message is UserMessage, the value is a message property. If it is FileMessage, the value is the name of the uploaded file.

With the timestamp of the parent message, you can retrieve its replies by passing a TreadMessageListParams object as an argument to the parameter in the getThreadedMessagesByTimestamp method.

Light Color Skin
Copy
try {
    final result = await message.getThreadedMessagesByTimestamp(
        TIMESTAMP,
        params
    );
    // The result includes an object that contains a parent message and replies.
} catch (e) {
    // Handle error.
}

List of arguments

ArgumentDescription

TIMESTAMP

Type: int
Specifies the timestamp to be the reference point of the retrieval, in Unix milliseconds format.


View a single message

You can retrieve a specific message by creating and passing the MessageRetrievalParams object as a parameter to the getMessage() method.

Light Color Skin
Copy
try {
    final params = MessageRetrievalParams()
        ..messageId = MESSAGE_ID
        ..channelType = CHANNEL_TYPE
        ..channelUrl = CHANNEL_URL;

    final message = await BaseMessage.getMessage(params);
} catch (e) {
    // Handle error.
}

List of properties

Property nameDescription

messageId

Type: int
Specifies the unique ID of the message to retrieve.

channelType

Type: ChannelType
Specifies the type of the channel.

channelUrl

Type: String
Specifies the URL of the channel to retrieve the message.


Reply to a message

You can reply to a specific message in a channel through the sendUserMessage() or sendFileMessage() method. To do so, you should create a UserMessageParams or a FileMessageParams object and then specify the parentMessageId property of the object. Sending reply messages works the same way as sending regular messages to a channel except replies have an additional parentMessageId property.

Reply with a text message

When replying to a message through the sendUserMessage() method, specify and pass a UserMessageParams object to the method as a parameter.

Light Color Skin
Copy
try {
    final params = UserMessageParams()
        ..parentMessageId = PARENT_MESSAGE_ID;

    final result = await channel.sendUserMessage(params);
    // A reply to a specific message in the form of a text message is successfully sent.
} catch (e) {
    // Handle error.
}

List of properties

The following table lists the properties of the UserMessageParams.

Property nameDescription

parentMessageId

Type: int
Specifies the unique ID of a parent message. A parent message is a message that has a thread of replies. If the message sent through the sendUserMessage() method is a parent message, the value of this property is 0. If the message is a reply to a parent message, the value is the message ID of the parent message.

Reply with a file message

When replying with a file message through the sendFileMessage() method, specify and pass a FileMessageParams object as an argument to a parameter in the method.

Light Color Skin
Copy
try {
    final params = FileMessageParams().withFile(file, name: ‘FILE_NAME’)
        ..parentMessageId = PARENT_MESSAGE_ID;

    final result = await channel.sendFileMessage(params);
    // A reply to a specific message in the form of a file message is successfully sent
} catch (e) {
    // Handle error.
}

List of properties

The following table lists the properties of the FileMessageParams.

Property nameDescription

parentMessageId

Type: int
Specifies the unique ID of a parent message. A parent message is a message that has a thread of replies. If the message sent through the sendFileMessage() method is a parent message, the value of this property is 0. If the message is a reply to a parent message, the value is the message ID of the parent message.


List changelogs

You can retrieve message changelogs.

Load message changelogs

Each message changelog has distinct properties such as a timestamp of when a message was updated and a unique ID of a deleted message. Based on these two properties, you can retrieve message changelogs in two different ways: either by timestamp or by token. The getMessageChangeLogs() method requires a MessageChangeLogParams object to determine what messages to return. The results can also differ depending on a parameter specified in the method: either TIMESTAMP or TOKEN.

Light Color Skin
Copy
final params = MessageChangeLogParams()
    ..includeReplies = true
    ..includeThreadInfo = true
    ..includeParentMessageText = true;

List of properties

The following table lists the properties of the MessageChangeLogParams.

Property nameDescription

includeReplies

Type: bool
Determines whether replies are included in the results.

includeThreadInfo

Type: bool
Determines whether to include the thread information of the messages in the results when the results contain parent messages.

includeParentMessageText

Type: bool
Determines whether to include the parent message text in the results when the messages are replies in a thread. If the type of the parent message is UserMessage, the value is a message property. If it is FileMessage, the value is the name of the uploaded file.

By timestamp

You can retrieve message changelogs by specifying a timestamp. The results will include changelogs that were created after the specified timestamp.

Light Color Skin
Copy
try {
    final result = await channel.getMessageChangeLogs(
        timestamp: TIMESTAMP,
        params: params
    );
    // The result object may contain updated and deleted messages, the hasNext property, and the token for next results.
} catch (e) {
    // Handle error.
}

By token

You can also retrieve message changelogs by specifying a token. The token is an opaque string that marks the starting point of the next page in the result set and it is included in the callback of the previous call. Based on the token, the next page will start with changelogs that were created after the specified token.

Light Color Skin
Copy
try {
    final result = await channel.getMessageChangeLogs(
        token: TOKEN,
        params: params
    );
    // The result object contains updated and deleted messages, information on the hasNext property, and the next results.
} catch (e) {
    // Handle error.
}

Event handler

Once a reply is created or deleted from a thread, the onThreadInfoUpdated() method of channel event handlers is invoked. The method returns a threadInfoUpdateEvent object that has the latest information about the thread. This object needs to be applied to the parent message object.

Note: Like other messages, when a reply is created in a channel, the onMessageReceived() method of the channel event handler in client apps will be called.

Light Color Skin
Copy
Class MyClass with ChannelEventHandler {
    // Add this class via sendbird.addChannelEventHandler(UNIQUE_HANDLER_ID, this).
    // Remove with sendbird.removeChannelEventHandler(UNIQUE_HANDLER_ID) when it is no longer needed.

    
    void onThreadInfoUpdated(BaseChannel channel, ThreadInfoUpdateEvent event) {
        // Look for a message that has event.targetMessageId and apply it to the message.
        message.applyThreadInfoUpdateEvent(event);
    }
}

List of parameters

Parameter nameDescription

channel

Type: BaseChannel
Specifies the channel that has the message thread.

event

Type: ThreadInfoUpdateEvent
Specifies a ThreadInfoUpdateEvent object that has the latest information about the thread.