Quick Reference for AI Agents & Developers
- Send in thread: Set
parentMessageIdonTextMessage,MediaMessage, orCustomMessagebefore 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
Object Structures
For complete object structures, see Send Message - Object Structures.Thread Message Properties
| Property | Type | Description |
|---|---|---|
| id | Int | Unique message identifier |
| parentMessageId | Int | ID of the parent message (thread root). 0 for non-threaded messages |
| sender | User | User who sent the message |
| receiverUid | String | UID of the receiver |
| receiverType | CometChat.ReceiverType | .user (0) or .group (1) |
| sentAt | Double | Unix timestamp when sent |
| text | String | Message text (for TextMessage) |
| replyCount | Int | Number of replies in thread |
| unreadRepliesCount | Int | Number of unread replies |
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 thereceiverType and the UID/GUID specified for the message. A message can belong to either of the below types:
- Text Message
- Media Message
- Custom Message.
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
- Swift
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
CometChat.sendTextMessage(message:onSuccess:onError:)Object Type: TextMessage (with parentMessageId)| Parameter | Type | Value |
|---|---|---|
| receiverUid | String | "cometchat-uid-2" |
| text | String | "Thread reply 2026-02-25 13:13:21 +0000" |
| receiverType | CometChat.ReceiverType | 0 (.user) |
| parentMessageId | Int | 38208 |
Send Media Message in Thread
- Swift
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
CometChat.sendMediaMessage(message:onSuccess:onError:)Object Type: MediaMessage (with parentMessageId)| Parameter | Type | Value |
|---|---|---|
| receiverUid | String | "cometchat-uid-2" |
| fileurl | String | "https://data-us.cometchat.io/assets/images/avatars/ironman.png" |
| messageType | CometChat.MessageType | 1 (.image) |
| receiverType | CometChat.ReceiverType | 0 (.user) |
| parentMessageId | Int | 38208 |
Send Custom Message in Thread
- Swift
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
customData (
CometChat.sendCustomMessage(message:onSuccess:onError:)Object Type: CustomMessage (with parentMessageId)| Parameter | Type | Value |
|---|---|---|
| receiverUid | String | "cometchat-uid-2" |
| receiverType | CometChat.ReceiverType | 0 (.user) |
| type | String | "thread_reaction" |
| parentMessageId | Int | 38208 |
[String: Any]):| Key | Type | Value |
|---|---|---|
| emoji | String | "👍" |
| timestamp | Double | 1772025206.052701 |
| replyType | String | "reaction" |
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 theCometChatMessageDelegate 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.
- Swift
Fetch all the messages for any particular thread
You can fetch all the messages belonging to a particular thread by using theMessagesRequest 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.
- Swift
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
messagesRequest.fetchPrevious(onSuccess:onError:)Object Type: MessagesRequest.MessageRequestBuilder| Parameter | Type | Value |
|---|---|---|
| parentMessageId | Int | 38208 |
| limit | Int | 50 |
Avoid Threaded Messages in User/Group Conversations
While fetching messages for normal user/group conversations using theMessagesRequest, 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.
- Swift (User)
- Swift (Group)
Sample Payloads - User Conversation
Sample Payloads - User Conversation
- Request
- Success Response
- Error Response
Method:
messagesRequest.fetchPrevious(onSuccess:onError:)Object Type: MessagesRequest.MessageRequestBuilder| Parameter | Type | Value |
|---|---|---|
| uid | String | "cometchat-uid-2" |
| limit | Int | 50 |
| hideReplies | Bool | true |
Sample Payloads - Group Conversation
Sample Payloads - Group Conversation
- Request
- Success Response
- Error Response
Method:
messagesRequest.fetchPrevious(onSuccess:onError:)Object Type: MessagesRequest.MessageRequestBuilder| Parameter | Type | Value |
|---|---|---|
| guid | String | "cometchat-guid-1" |
| limit | Int | 50 |
| hideReplies | Bool | true |
Common Error Codes
| Error Code | Description | Resolution |
|---|---|---|
ERR_MESSAGE_NOT_FOUND | Parent message does not exist | Verify the parent message ID exists |
ERR_INVALID_PARENT_MESSAGE_ID | Invalid parent message ID provided | Provide a valid message ID |
ERR_WRONG_MESSAGE_THREAD | Message cannot be added to the thread | Verify the thread is valid and accessible |
ERR_EMPTY_MESSAGE | Message text is empty | Provide message content |
ERR_NOT_LOGGED_IN | User is not logged in | Call CometChat.login() first |
ERR_SDK_NOT_INITIALIZED | SDK not initialized | Call CometChat.init() first |