Skip to main content
Quick Reference for AI Agents & Developers
  • Send in thread: Set parentMessageId on TextMessage, MediaMessage, or CustomMessage before sending
  • Fetch thread messages: MessagesRequest.MessageRequestBuilder().setParentMessageId(parentMessageId:).build()fetchPrevious(onSuccess:onError:)
  • Hide replies: MessagesRequest.MessageRequestBuilder().hideReplies(hide: true) to exclude thread messages from main conversation
  • Parent message: Original message that starts the thread
  • Related: Send Message · Retrieve Messages · Messaging Overview
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.

Object Structures

For complete object structures, see Send Message - Object Structures.

Thread Message Properties


Send Message in a Thread

As mentioned in the Send a Message section. You can either send a message to a User or a Group based on the receiverType and the UID/GUID specified for the message. A message can belong to either of the below types:
  1. Text Message
  2. Media Message
  3. Custom Message.
Any of the above messages can be sent in a thread. As mentioned, a thread is identified based on the Parent message. So while sending a message the parentMessageId must be set for the message to indicate that the message to be sent needs to be a part of the thread with the specified parentMessageId. This can be achieved using the parentMessageId property provided by the object of the TextMessage, MediaMessage, and CustomMessage class. The id specified in the parentMessageId property maps the message sent to the particular thread.

Send Text Message in Thread

Method: CometChat.sendTextMessage(message:onSuccess:onError:)Object Type: TextMessage (with parentMessageId)

Send Media Message in Thread

Method: CometChat.sendMediaMessage(message:onSuccess:onError:)Object Type: MediaMessage (with parentMessageId)

Send Custom Message in Thread

Method: CometChat.sendCustomMessage(message:onSuccess:onError:)Object Type: CustomMessage (with parentMessageId)customData ([String: Any]):

Receiving Real-Time Messages

The procedure to receive real-time messages is exactly the same as mentioned in the Receive Messages. This can be achieved using the CometChatMessageDelegate class provided by the SDK. In order to receive incoming messages, you must add protocol conformance CometChatMessageDelegate. The only thing that needs to be checked is if the received message belongs to the active thread. This can be done using the parentMessageId field of the message object.

Fetch all the messages for any particular thread

You can fetch all the messages belonging to a particular thread by using the MessagesRequest class. Use the setParentMessageId() method of the MessagesRequestBuilder to inform the SDK that you only need the messages belonging to the thread with the specified parentMessageId.
Method: messagesRequest.fetchPrevious(onSuccess:onError:)Object Type: MessagesRequest.MessageRequestBuilder

Avoid Threaded Messages in User/Group Conversations

While fetching messages for normal user/group conversations using the MessagesRequest, the threaded messages by default will be a part of the list of messages received. In order to exclude the threaded messages from the list of user/group messages, you need to use the hideReplies() method of the MessagesRequestBuilder class. This method takes a boolean argument which when set to true excludes the messages belonging to threads from the list of messages.
Method: messagesRequest.fetchPrevious(onSuccess:onError:)Object Type: MessagesRequest.MessageRequestBuilder
Method: messagesRequest.fetchPrevious(onSuccess:onError:)Object Type: MessagesRequest.MessageRequestBuilder

Common Error Codes