/ SDKs / JavaScript
SDKs
Chat SDKs JavaScript v4
Chat SDKs JavaScript
Chat SDKs
JavaScript
Version 4

Create a message thread

Copy link

When a user replies to a message in a channel, it creates a message thread, which refers to a collection of messages consisting of a parent message and its replies. Message threading lets users ask questions, give feedback, or add context to a specific message without disrupting the conversation. 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.

Message threading has the following limitations.

  • Only 1-depth threads are supported, meaning you can only add reply messages to non-reply messages. You can't add a reply to a reply message.
  • Message threading is limited to text and file messages. You can't send admin messages as replies or add replies to admin messages.

You can reply to a specific message in a channel through the sendUserMessage() or sendFileMessage() method. To do so, you should create a UserMessageCreateParams or a FileMessageCreateParams 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 that replies have an additional parentMessageId property.

Note: To retrieve information about parent messages and threaded replies, see how to list changelogs of messages in a channel.


Reply with a text message

Copy link

When replying to a message through the sendUserMessage() method, specify and pass a UserMessageCreateParams object as an argument to a parameter in the method.

JavaScriptTypeScript
// Create a UserMessageCreateParams object.
const params = {
    parentMessageId: PARENT_MESSAGE_ID, // This specifies the unique ID of a parent message which has a thread of replies. If the message sent through the sendUserMessage() method is a parent message, the value of this property is 0.
};

List of parameters

Copy link
Parameter nameTypeDescription

parentMessageId

long

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

Copy link

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

JavaScriptTypeScript
// Create a FileMessageCreateParams object.
const params = {
    file: FILE,
    parentMessageId: PARENT_MESSAGE_ID, // This specifies the unique ID of a parent message which has a thread of replies. If the message sent through the sendUserMessage() method is a parent message, the value of this property is 0.
};

List of parameters

Copy link
Parameter nameTypeDescription

file

string

Specifies the URL of the file to be attached to the message.

parentMessageId

long

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.

When replying with more than two images or videos through the sendMultipleFilesMessage() method, you should specify and pass a MultipleFilesMessageCreateParams object to the method as a parameter. Just like FileMessageCreateParams, the MultipleFilesMessageCreateParams class is derived from the BaseMessageCreateParams class and can access all the methods and properties of BaseMessageCreateParams. To learn more about how to send a multiple file message, see the Send a message page.

jsTypeScript
// Create a MultipleFilesMessageCreateParams object.
const params: FileMessageCreateParams = {
    fileInfoList: UPLOADABLE_FILE_INFO_LIST,
    parentMessageId: PARENT_MESSAGE_ID, // This specifies the unique ID of a parent message which has a thread of replies. If the message sent through the sendUserMessage() method is a parent message, the value of this property is 0.
};

Event handler for message threading

Copy link

When a reply message is created, onMessageReceived() is called with that reply message. A user will have to find the parent message with the parentMessageID property in that message object.

Boh when a reply message is created and deleted from a thread, the onThreadInfoUpdated() event handler is also called since the thread info has been changed, which takes a threadInfoUpdateEvent object as an argument that has the latest information about the thread. To update thread information, the user should find the matching parent message with targetMessageID and apply threadInfoUpdateEvent to the message through the parentMessage.applyThreadInfoUpdateEvent() method.

If the parent message is not in the message view stack and the user wants to get the parent message object, they can do so by calling sb.message.getMessage(params, handler) to retrieve the parent message object.

handler.onThreadInfoUpdated = (channel: BaseChannel, threadInfoUpdateEvent: ThreadInfoUpdateEvent) => {
    // Look for a message that has threadInfoUpdateEvent.targetMessageId.
    // Apply the event to the message.
    parentMessage.applyThreadInfoUpdateEvent(threadInfoUpdateEvent);
};

List of parameters

Copy link
Parameter nameTypeDescription

channel

BaseChannel

Specifies the channel that has the message thread.

threadInfoUpdateEvent

ThreadInfoUpdateEvent

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