Use this file to discover all available pages before exploring further.
Messages that are started from a particular message are called Threaded messages or simply threads. Each Thread is attached to a message which is the Parent message for that thread.
Any message type (Text, Media, or Custom) can be sent in a thread. Set the parentMessageId using setParentMessageId() to indicate which thread the message belongs to.
TypeScript
JavaScript
let receiverId = "UID", receiverType: string = CometChat.RECEIVER_TYPE.USER, textMessage: CometChat.TextMessage = new CometChat.TextMessage(receiverId, "Hello", receiverType), messageId: number = 100;textMessage.setParentMessageId(messageId);CometChat.sendMessage(textMessage).then((message: CometChat.TextMessage) => {console.log('Message sent successfully', message);}, (error: CometChat.CometChatException) => {console.log('Message sending failed', error);});
let UID = "UID";let textMessage = new CometChat.TextMessage(UID, "Hello", CometChat.RECEIVER_TYPE.USER);textMessage.setParentMessageId(100);CometChat.sendMessage(textMessage).then( message => { console.log('Message sent successfully', message); }, err => { console.log('err', err); })
The above snippet sends “Hello” in the thread with parentMessageId 100. Media and Custom messages can also be sent in threads using setParentMessageId().
Use MessagesRequestBuilder with setParentMessageId() to fetch messages belonging to a specific thread. Call fetchPrevious() to get messages (max 100 per request).
TypeScript
JavaScript
let limit: number = 30, parentMessageId: number = 1, messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder() .setLimit(limit) .setParentMessageId(parentMessageId) .build();messagesRequest.fetchPrevious().then((messages: CometChat.BaseMessage[]) => {console.log("Messages for thread fetched successfully", messages);}, (error: CometChat.CometChatException) => {console.log("Message fetching failed with error:", error);});
let limit = 30;let parentMessageId = 1;let messagesRequest = new CometChat.MessagesRequestBuilder() .setLimit(limit) .setParentMessageId(parentMessageId) .build();messagesRequest.fetchPrevious().then( messages => { console.log("Messages for thread fetched successfully", messages); }, error => { console.log("Message fetching failed with error:", error); });
The fetchPrevious() method returns an array of BaseMessage objects representing thread replies.
The response is an array of BaseMessage objects, excluding any messages that are replies within a thread. Only top-level messages in the conversation are returned.
Always remove message listeners when they’re no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling.