> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-ios-ui-kit-sdk-fixes.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Threaded Messages

> Guide to sending and receiving threaded messages using the CometChat iOS SDK for organized conversations.

<Info>
  **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](/sdk/ios/send-message) · [Retrieve Messages](/sdk/ios/receive-message) · [Messaging Overview](/sdk/ios/messaging-overview)
</Info>

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](/sdk/ios/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](/sdk/ios/send-message#user-object) | 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](/sdk/ios/send-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

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let receiverID = "cometchat-uid-2"
    let text = "Hello"

    let textMessage = TextMessage(receiverUid: receiverID, text: text, receiverType: .user)
    textMessage.parentMessageId = 38208

    CometChat.sendTextMessage(message: textMessage, onSuccess: { (message) in
      print("TextMessage sent successfully. " + message.stringValue())
    }) { (error) in
      print("TextMessage sending failed with error: " + error!.errorDescription);
    }
    ```
  </Tab>
</Tabs>

<Accordion title="Sample Payloads">
  <Tabs>
    <Tab title="Request">
      **Method:** `CometChat.sendTextMessage(message:onSuccess:onError:)`

      **Object Type:** [TextMessage](/sdk/ios/send-message#textmessage-object) (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`                                    |
    </Tab>

    <Tab title="Success Response">
      **Object Type:** [TextMessage](/sdk/ios/send-message#textmessage-object)

      | Parameter          | Type                        | Value                                      |
      | ------------------ | --------------------------- | ------------------------------------------ |
      | id                 | `Int`                       | `38209`                                    |
      | muid               | `String`                    | `""`                                       |
      | conversationId     | `String`                    | `"cometchat-uid-2_user_cometchat-uid-2"`   |
      | parentMessageId    | `Int`                       | `38208`                                    |
      | senderUid          | `String`                    | `"cometchat-uid-2"`                        |
      | receiverUid        | `String`                    | `"cometchat-uid-2"`                        |
      | receiverType       | `CometChat.ReceiverType`    | `0` (`.user`)                              |
      | messageType        | `CometChat.MessageType`     | `0` (`.text`)                              |
      | messageCategory    | `CometChat.MessageCategory` | `0` (`.message`)                           |
      | sentAt             | `Int`                       | `1772025201`                               |
      | deliveredAt        | `Double`                    | `0.0`                                      |
      | readAt             | `Double`                    | `0.0`                                      |
      | editedAt           | `Double`                    | `0.0`                                      |
      | deletedAt          | `Double`                    | `0.0`                                      |
      | updatedAt          | `Double`                    | `1772025201.0`                             |
      | editedBy           | `String`                    | `""`                                       |
      | deletedBy          | `String`                    | `""`                                       |
      | replyCount         | `Int`                       | `0`                                        |
      | unreadRepliesCount | `Int`                       | `0`                                        |
      | metaData           | `[String: Any]`             | `[:]` (empty dictionary)                   |
      | tags               | `[String]`                  | `[]` (empty array)                         |
      | mentionedUsers     | `[User]`                    | `[]` (empty array)                         |
      | mentionedMe        | `Bool`                      | `false`                                    |
      | reactions          | `[ReactionCount]`           | `[]` (empty array)                         |
      | text               | `String`                    | `"Thread reply 2026-02-25 13:13:21 +0000"` |

      **sender** ([User](/sdk/ios/send-message#user-object)):

      | Parameter    | Type                   | Value                                                                   |
      | ------------ | ---------------------- | ----------------------------------------------------------------------- |
      | uid          | `String`               | `"cometchat-uid-2"`                                                     |
      | name         | `String`               | `"George Alan"`                                                         |
      | avatar       | `String`               | `"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"` |
      | status       | `CometChat.UserStatus` | `0` (`.online`)                                                         |
      | lastActiveAt | `Double`               | `1772024897.0`                                                          |

      **receiver** ([User](/sdk/ios/send-message#user-object)):

      | Parameter | Type                   | Value                                                                   |
      | --------- | ---------------------- | ----------------------------------------------------------------------- |
      | uid       | `String`               | `"cometchat-uid-2"`                                                     |
      | name      | `String`               | `"George Alan"`                                                         |
      | avatar    | `String`               | `"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"` |
      | status    | `CometChat.UserStatus` | `0` (`.online`)                                                         |
    </Tab>

    <Tab title="Error Response">
      **Object Type:** [CometChatException](/sdk/ios/send-message#cometchatexception-object)

      | Parameter        | Type     | Value                                          |
      | ---------------- | -------- | ---------------------------------------------- |
      | errorCode        | `String` | `"ERR_WRONG_MESSAGE_THREAD"`                   |
      | errorDescription | `String` | `"The message cannot be added to the thread."` |
    </Tab>
  </Tabs>
</Accordion>

### Send Media Message in Thread

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let receiverID = "cometchat-uid-2"
    let fileUrl = "https://data-us.cometchat.io/assets/images/avatars/ironman.png"

    let mediaMessage = MediaMessage(receiverUid: receiverID, fileurl: fileUrl, messageType: .image, receiverType: .user)
    mediaMessage.parentMessageId = 38208

    CometChat.sendMediaMessage(message: mediaMessage, onSuccess: { (message) in
      print("MediaMessage sent successfully. " + message.stringValue())
    }) { (error) in
      print("MediaMessage sending failed with error: " + error!.errorDescription);
    }
    ```
  </Tab>
</Tabs>

<Accordion title="Sample Payloads">
  <Tabs>
    <Tab title="Request">
      **Method:** `CometChat.sendMediaMessage(message:onSuccess:onError:)`

      **Object Type:** [MediaMessage](/sdk/ios/send-message#mediamessage-object) (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`                                                            |
    </Tab>

    <Tab title="Success Response">
      **Object Type:** [MediaMessage](/sdk/ios/send-message#mediamessage-object)

      | Parameter       | Type                        | Value                                    |
      | --------------- | --------------------------- | ---------------------------------------- |
      | id              | `Int`                       | `38210`                                  |
      | muid            | `String`                    | `""`                                     |
      | conversationId  | `String`                    | `"cometchat-uid-2_user_cometchat-uid-2"` |
      | parentMessageId | `Int`                       | `38208`                                  |
      | senderUid       | `String`                    | `"cometchat-uid-2"`                      |
      | receiverUid     | `String`                    | `"cometchat-uid-2"`                      |
      | receiverType    | `CometChat.ReceiverType`    | `0` (`.user`)                            |
      | messageType     | `CometChat.MessageType`     | `1` (`.image`)                           |
      | messageCategory | `CometChat.MessageCategory` | `0` (`.message`)                         |
      | sentAt          | `Int`                       | `1772025205`                             |
      | deliveredAt     | `Double`                    | `0.0`                                    |
      | readAt          | `Double`                    | `0.0`                                    |
      | replyCount      | `Int`                       | `0`                                      |
      | metaData        | `[String: Any]`             | `[:]` (empty dictionary)                 |
      | tags            | `[String]`                  | `[]` (empty array)                       |
      | caption         | `String?`                   | `nil`                                    |

      **attachment** ([Attachment](/sdk/ios/send-message#attachment-object)):

      | Parameter     | Type     | Value                                                                                                              |
      | ------------- | -------- | ------------------------------------------------------------------------------------------------------------------ |
      | fileName      | `String` | `"ironman.png"`                                                                                                    |
      | fileExtension | `String` | `"png"`                                                                                                            |
      | fileSize      | `Double` | `11290.0`                                                                                                          |
      | fileMimeType  | `String` | `"image/png"`                                                                                                      |
      | fileUrl       | `String` | `"https://data-in.cometchat.io/279557705a948ad6/media/1772025205_1004925822_9130eb5bdd50e13381cb30871f79a83f.png"` |

      **sender** ([User](/sdk/ios/send-message#user-object)):

      | Parameter | Type     | Value               |
      | --------- | -------- | ------------------- |
      | uid       | `String` | `"cometchat-uid-2"` |
      | name      | `String` | `"George Alan"`     |
    </Tab>

    <Tab title="Error Response">
      **Object Type:** [CometChatException](/sdk/ios/send-message#cometchatexception-object)

      | Parameter        | Type     | Value                                          |
      | ---------------- | -------- | ---------------------------------------------- |
      | errorCode        | `String` | `"ERR_WRONG_MESSAGE_THREAD"`                   |
      | errorDescription | `String` | `"The message cannot be added to the thread."` |
    </Tab>
  </Tabs>
</Accordion>

### Send Custom Message in Thread

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let receiverID = "cometchat-uid-2"
    let customData: [String: Any] = ["emoji": "👍", "timestamp": 1772025206.052701, "replyType": "reaction"]

    let customMessage = CustomMessage(receiverUid: receiverID, receiverType: .user, customData: customData, type: "thread_reaction")
    customMessage.parentMessageId = 38208

    CometChat.sendCustomMessage(message: customMessage, onSuccess: { (message) in
      print("CustomMessage sent successfully. " + message.stringValue())
    }) { (error) in
      print("CustomMessage sending failed with error: " + error!.errorDescription);
    }
    ```
  </Tab>
</Tabs>

<Accordion title="Sample Payloads">
  <Tabs>
    <Tab title="Request">
      **Method:** `CometChat.sendCustomMessage(message:onSuccess:onError:)`

      **Object Type:** [CustomMessage](/sdk/ios/send-message#custommessage-object) (with parentMessageId)

      | Parameter       | Type                     | Value               |
      | --------------- | ------------------------ | ------------------- |
      | receiverUid     | `String`                 | `"cometchat-uid-2"` |
      | receiverType    | `CometChat.ReceiverType` | `0` (`.user`)       |
      | type            | `String`                 | `"thread_reaction"` |
      | parentMessageId | `Int`                    | `38208`             |

      **customData** (`[String: Any]`):

      | Key       | Type     | Value               |
      | --------- | -------- | ------------------- |
      | emoji     | `String` | `"👍"`              |
      | timestamp | `Double` | `1772025206.052701` |
      | replyType | `String` | `"reaction"`        |
    </Tab>

    <Tab title="Success Response">
      **Object Type:** [CustomMessage](/sdk/ios/send-message#custommessage-object)

      | Parameter          | Type                        | Value                                    |
      | ------------------ | --------------------------- | ---------------------------------------- |
      | id                 | `Int`                       | `38211`                                  |
      | muid               | `String`                    | `""`                                     |
      | conversationId     | `String`                    | `"cometchat-uid-2_user_cometchat-uid-2"` |
      | parentMessageId    | `Int`                       | `38208`                                  |
      | senderUid          | `String`                    | `"cometchat-uid-2"`                      |
      | receiverUid        | `String`                    | `"cometchat-uid-2"`                      |
      | receiverType       | `CometChat.ReceiverType`    | `0` (`.user`)                            |
      | messageCategory    | `CometChat.MessageCategory` | `3` (`.custom`)                          |
      | sentAt             | `Int`                       | `1772025206`                             |
      | deliveredAt        | `Double`                    | `0.0`                                    |
      | readAt             | `Double`                    | `0.0`                                    |
      | replyCount         | `Int`                       | `0`                                      |
      | metaData           | `[String: Any]`             | `[:]` (empty dictionary)                 |
      | tags               | `[String]`                  | `[]` (empty array)                       |
      | customType         | `String`                    | `"thread_reaction"`                      |
      | subType            | `String?`                   | `nil`                                    |
      | updateConversation | `Bool`                      | `false`                                  |
      | conversationText   | `String?`                   | `nil`                                    |

      **customData** (`[String: Any]`):

      | Key       | Type     | Value               |
      | --------- | -------- | ------------------- |
      | timestamp | `Double` | `1772025206.052701` |
      | emoji     | `String` | `"👍"`              |
      | replyType | `String` | `"reaction"`        |

      **sender** ([User](/sdk/ios/send-message#user-object)):

      | Parameter | Type     | Value               |
      | --------- | -------- | ------------------- |
      | uid       | `String` | `"cometchat-uid-2"` |
      | name      | `String` | `"George Alan"`     |
    </Tab>

    <Tab title="Error Response">
      **Object Type:** [CometChatException](/sdk/ios/send-message#cometchatexception-object)

      | Parameter        | Type     | Value                                          |
      | ---------------- | -------- | ---------------------------------------------- |
      | errorCode        | `String` | `"ERR_WRONG_MESSAGE_THREAD"`                   |
      | errorDescription | `String` | `"The message cannot be added to the thread."` |
    </Tab>
  </Tabs>
</Accordion>

***

## Receiving Real-Time Messages

The procedure to receive real-time messages is exactly the same as mentioned in the [Receive Messages](/sdk/ios/receive-message). 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.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    var activeThreadId = 38208

    extension ViewController: CometChatMessageDelegate {

      func onTextMessageReceived(textMessage: TextMessage) {
        if textMessage.parentMessageId == activeThreadId {
          print("TextMessage received in thread: " + textMessage.stringValue())
        }
      }

      func onMediaMessageReceived(mediaMessage: MediaMessage) {
        if mediaMessage.parentMessageId == activeThreadId {
          print("MediaMessage received in thread: " + mediaMessage.stringValue())
        }
      }

      func onCustomMessageReceived(customMessage: CustomMessage) {
        if customMessage.parentMessageId == activeThreadId {
          print("CustomMessage received in thread: " + customMessage.stringValue())
        }
      }
    }
    ```
  </Tab>
</Tabs>

***

## 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`.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let messagesRequest = MessagesRequest.MessageRequestBuilder()
      .setParentMessageId(parentMessageId: 38208)
      .set(limit: 50)
      .build()

    messagesRequest.fetchPrevious(onSuccess: { (messages) in
      for message in messages! {
        if let textMessage = message as? TextMessage {
          print("Text Message: " + textMessage.stringValue())
        } else if let mediaMessage = message as? MediaMessage {
          print("Media Message: " + mediaMessage.stringValue())
        } else if let customMessage = message as? CustomMessage {
          print("Custom Message: " + customMessage.stringValue())
        }
      }
    }) { (error) in
      print("Messages fetching failed with error: " + error!.errorDescription)
    }
    ```
  </Tab>
</Tabs>

<Accordion title="Sample Payloads">
  <Tabs>
    <Tab title="Request">
      **Method:** `messagesRequest.fetchPrevious(onSuccess:onError:)`

      **Object Type:** `MessagesRequest.MessageRequestBuilder`

      | Parameter       | Type  | Value   |
      | --------------- | ----- | ------- |
      | parentMessageId | `Int` | `38208` |
      | limit           | `Int` | `50`    |
    </Tab>

    <Tab title="Success Response">
      **Object Type:** `[BaseMessage]` (Array of thread messages)

      **Response Summary:**

      | Parameter       | Type  | Value   |
      | --------------- | ----- | ------- |
      | parentMessageId | `Int` | `38208` |
      | count           | `Int` | `3`     |

      **Message\[0]** ([TextMessage](/sdk/ios/send-message#textmessage-object)):

      | Parameter          | Type                        | Value                                      |
      | ------------------ | --------------------------- | ------------------------------------------ |
      | id                 | `Int`                       | `38209`                                    |
      | parentMessageId    | `Int`                       | `38208`                                    |
      | muid               | `String`                    | `""`                                       |
      | conversationId     | `String`                    | `"cometchat-uid-2_user_cometchat-uid-2"`   |
      | senderUid          | `String`                    | `"cometchat-uid-2"`                        |
      | receiverUid        | `String`                    | `"cometchat-uid-2"`                        |
      | receiverType       | `CometChat.ReceiverType`    | `0` (`.user`)                              |
      | messageType        | `CometChat.MessageType`     | `0` (`.text`)                              |
      | messageCategory    | `CometChat.MessageCategory` | `0` (`.message`)                           |
      | sentAt             | `Int`                       | `1772025201`                               |
      | deliveredAt        | `Double`                    | `0.0`                                      |
      | readAt             | `Double`                    | `0.0`                                      |
      | editedAt           | `Double`                    | `0.0`                                      |
      | deletedAt          | `Double`                    | `0.0`                                      |
      | updatedAt          | `Double`                    | `1772025201.0`                             |
      | replyCount         | `Int`                       | `0`                                        |
      | unreadRepliesCount | `Int`                       | `0`                                        |
      | metaData           | `[String: Any]`             | `[:]` (empty dictionary)                   |
      | reactions          | `[ReactionCount]`           | `[]` (empty array)                         |
      | text               | `String`                    | `"Thread reply 2026-02-25 13:13:21 +0000"` |
      | tags               | `[String]`                  | `[]` (empty array)                         |

      **Message\[0].sender** ([User](/sdk/ios/send-message#user-object)):

      | Parameter | Type     | Value                                                                   |
      | --------- | -------- | ----------------------------------------------------------------------- |
      | uid       | `String` | `"cometchat-uid-2"`                                                     |
      | name      | `String` | `"George Alan"`                                                         |
      | avatar    | `String` | `"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"` |

      **Message\[1]** ([MediaMessage](/sdk/ios/send-message#mediamessage-object)):

      | Parameter          | Type                        | Value                                    |
      | ------------------ | --------------------------- | ---------------------------------------- |
      | id                 | `Int`                       | `38210`                                  |
      | parentMessageId    | `Int`                       | `38208`                                  |
      | muid               | `String`                    | `""`                                     |
      | conversationId     | `String`                    | `"cometchat-uid-2_user_cometchat-uid-2"` |
      | senderUid          | `String`                    | `"cometchat-uid-2"`                      |
      | receiverUid        | `String`                    | `"cometchat-uid-2"`                      |
      | receiverType       | `CometChat.ReceiverType`    | `0` (`.user`)                            |
      | messageType        | `CometChat.MessageType`     | `1` (`.image`)                           |
      | messageCategory    | `CometChat.MessageCategory` | `0` (`.message`)                         |
      | sentAt             | `Int`                       | `1772025205`                             |
      | deliveredAt        | `Double`                    | `0.0`                                    |
      | readAt             | `Double`                    | `0.0`                                    |
      | editedAt           | `Double`                    | `0.0`                                    |
      | deletedAt          | `Double`                    | `0.0`                                    |
      | updatedAt          | `Double`                    | `0.0`                                    |
      | replyCount         | `Int`                       | `0`                                      |
      | unreadRepliesCount | `Int`                       | `0`                                      |
      | metaData           | `[String: Any]`             | `[:]` (empty dictionary)                 |
      | reactions          | `[ReactionCount]`           | `[]` (empty array)                       |
      | caption            | `String?`                   | `nil`                                    |
      | tags               | `[String]`                  | `[]` (empty array)                       |

      **Message\[1].attachment** ([Attachment](/sdk/ios/send-message#attachment-object)):

      | Parameter     | Type     | Value                                                                                                              |
      | ------------- | -------- | ------------------------------------------------------------------------------------------------------------------ |
      | fileName      | `String` | `"ironman.png"`                                                                                                    |
      | fileExtension | `String` | `"png"`                                                                                                            |
      | fileSize      | `Double` | `11290.0`                                                                                                          |
      | fileMimeType  | `String` | `"image/png"`                                                                                                      |
      | fileUrl       | `String` | `"https://data-in.cometchat.io/279557705a948ad6/media/1772025205_1004925822_9130eb5bdd50e13381cb30871f79a83f.png"` |

      **Message\[1].sender** ([User](/sdk/ios/send-message#user-object)):

      | Parameter | Type     | Value                                                                   |
      | --------- | -------- | ----------------------------------------------------------------------- |
      | uid       | `String` | `"cometchat-uid-2"`                                                     |
      | name      | `String` | `"George Alan"`                                                         |
      | avatar    | `String` | `"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"` |

      **Message\[2]** ([CustomMessage](/sdk/ios/send-message#custommessage-object)):

      | Parameter          | Type                        | Value                                    |
      | ------------------ | --------------------------- | ---------------------------------------- |
      | id                 | `Int`                       | `38211`                                  |
      | parentMessageId    | `Int`                       | `38208`                                  |
      | muid               | `String`                    | `""`                                     |
      | conversationId     | `String`                    | `"cometchat-uid-2_user_cometchat-uid-2"` |
      | senderUid          | `String`                    | `"cometchat-uid-2"`                      |
      | receiverUid        | `String`                    | `"cometchat-uid-2"`                      |
      | receiverType       | `CometChat.ReceiverType`    | `0` (`.user`)                            |
      | messageCategory    | `CometChat.MessageCategory` | `3` (`.custom`)                          |
      | sentAt             | `Int`                       | `1772025206`                             |
      | deliveredAt        | `Double`                    | `0.0`                                    |
      | readAt             | `Double`                    | `0.0`                                    |
      | editedAt           | `Double`                    | `0.0`                                    |
      | deletedAt          | `Double`                    | `0.0`                                    |
      | updatedAt          | `Double`                    | `1772025206.0`                           |
      | replyCount         | `Int`                       | `0`                                      |
      | unreadRepliesCount | `Int`                       | `0`                                      |
      | metaData           | `[String: Any]`             | `[:]` (empty dictionary)                 |
      | reactions          | `[ReactionCount]`           | `[]` (empty array)                       |
      | customType         | `String`                    | `"thread_reaction"`                      |

      **Message\[2].customData** (`[String: Any]`):

      | Key       | Type     | Value               |
      | --------- | -------- | ------------------- |
      | replyType | `String` | `"reaction"`        |
      | emoji     | `String` | `"👍"`              |
      | timestamp | `Double` | `1772025206.052701` |

      **Message\[2].sender** ([User](/sdk/ios/send-message#user-object)):

      | Parameter | Type     | Value                                                                   |
      | --------- | -------- | ----------------------------------------------------------------------- |
      | uid       | `String` | `"cometchat-uid-2"`                                                     |
      | name      | `String` | `"George Alan"`                                                         |
      | avatar    | `String` | `"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"` |
    </Tab>

    <Tab title="Error Response">
      **Object Type:** [CometChatException](/sdk/ios/send-message#cometchatexception-object)

      | Parameter        | Type     | Value                              |
      | ---------------- | -------- | ---------------------------------- |
      | errorCode        | `String` | `"ERR_MESSAGE_NOT_FOUND"`          |
      | errorDescription | `String` | `"Parent message does not exist."` |
    </Tab>
  </Tabs>
</Accordion>

***

## 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.

<Tabs>
  <Tab title="Swift (User)">
    ```swift theme={null}
    let messagesRequest = MessagesRequest.MessageRequestBuilder()
      .set(uid: "cometchat-uid-2")
      .set(limit: 50)
      .hideReplies(hide: true)
      .build()

    messagesRequest.fetchPrevious(onSuccess: { (messages) in
      for message in messages! {
        print("Message ID: \(message.id), parentMessageId: \(message.parentMessageId)")
      }
    }) { (error) in
      print("Messages fetching failed with error: " + error!.errorDescription)
    }
    ```
  </Tab>

  <Tab title="Swift (Group)">
    ```swift theme={null}
    let messagesRequest = MessagesRequest.MessageRequestBuilder()
      .set(guid: "cometchat-guid-1")
      .set(limit: 50)
      .hideReplies(hide: true)
      .build()

    messagesRequest.fetchPrevious(onSuccess: { (messages) in
      for message in messages! {
        print("Message ID: \(message.id), parentMessageId: \(message.parentMessageId)")
      }
    }) { (error) in
      print("Messages fetching failed with error: " + error!.errorDescription)
    }
    ```
  </Tab>
</Tabs>

<Accordion title="Sample Payloads - User Conversation">
  <Tabs>
    <Tab title="Request">
      **Method:** `messagesRequest.fetchPrevious(onSuccess:onError:)`

      **Object Type:** `MessagesRequest.MessageRequestBuilder`

      | Parameter   | Type     | Value               |
      | ----------- | -------- | ------------------- |
      | uid         | `String` | `"cometchat-uid-2"` |
      | limit       | `Int`    | `50`                |
      | hideReplies | `Bool`   | `true`              |
    </Tab>

    <Tab title="Success Response">
      **Object Type:** `[BaseMessage]` (Array excluding thread replies)

      **Response Summary:**

      | Parameter   | Type     | Value               |
      | ----------- | -------- | ------------------- |
      | uid/guid    | `String` | `"cometchat-uid-2"` |
      | hideReplies | `Bool`   | `true`              |
      | count       | `Int`    | `3`                 |

      **Note:** Messages with `parentMessageId > 0` are excluded from results.

      **Message\[0]** ([TextMessage](/sdk/ios/send-message#textmessage-object)):

      | Parameter       | Type                        | Value               |
      | --------------- | --------------------------- | ------------------- |
      | id              | `Int`                       | `37789`             |
      | parentMessageId | `Int`                       | `0`                 |
      | senderUid       | `String`                    | `"cometchat-uid-2"` |
      | receiverUid     | `String`                    | `"cometchat-uid-2"` |
      | messageType     | `CometChat.MessageType`     | `0` (`.text`)       |
      | messageCategory | `CometChat.MessageCategory` | `0` (`.message`)    |
      | sentAt          | `Int`                       | `1771589782`        |
      | replyCount      | `Int`                       | `1`                 |
      | text            | `String`                    | `""`                |
      | tags            | `[String]`                  | `[]` (empty array)  |

      **Message\[0].sender** ([User](/sdk/ios/send-message#user-object)):

      | Parameter | Type     | Value               |
      | --------- | -------- | ------------------- |
      | uid       | `String` | `"cometchat-uid-2"` |
      | name      | `String` | `"George Alan"`     |

      **Message\[1]** (ActionMessage):

      | Parameter       | Type                        | Value                   |
      | --------------- | --------------------------- | ----------------------- |
      | id              | `Int`                       | `37791`                 |
      | parentMessageId | `Int`                       | `0`                     |
      | senderUid       | `String`                    | `"cometchat-uid-2"`     |
      | receiverUid     | `String`                    | `"cometchat-uid-2"`     |
      | messageCategory | `CometChat.MessageCategory` | `1` (`.action`)         |
      | sentAt          | `Int`                       | `1771589787`            |
      | replyCount      | `Int`                       | `0`                     |
      | message         | `String`                    | `"Message is deleted."` |
      | action          | `ActionType`                | `7` (`.messageDeleted`) |

      **Message\[1].actionOn** ([TextMessage](/sdk/ios/send-message#textmessage-object)):

      | Parameter | Type     | Value   |
      | --------- | -------- | ------- |
      | id        | `Int`    | `37789` |
      | text      | `String` | `""`    |

      **Message\[1].actionBy** ([User](/sdk/ios/send-message#user-object)):

      | Parameter | Type     | Value               |
      | --------- | -------- | ------------------- |
      | uid       | `String` | `"cometchat-uid-2"` |
      | name      | `String` | `"George Alan"`     |

      **Message\[2]** ([TextMessage](/sdk/ios/send-message#textmessage-object)):

      | Parameter       | Type                        | Value                                    |
      | --------------- | --------------------------- | ---------------------------------------- |
      | id              | `Int`                       | `38208`                                  |
      | parentMessageId | `Int`                       | `0`                                      |
      | senderUid       | `String`                    | `"cometchat-uid-2"`                      |
      | receiverUid     | `String`                    | `"cometchat-uid-2"`                      |
      | messageType     | `CometChat.MessageType`     | `0` (`.text`)                            |
      | messageCategory | `CometChat.MessageCategory` | `0` (`.message`)                         |
      | sentAt          | `Int`                       | `1772025178`                             |
      | replyCount      | `Int`                       | `3`                                      |
      | text            | `String`                    | `"Hello! This is a basic text message."` |
      | tags            | `[String]`                  | `[]` (empty array)                       |

      **Message\[2].sender** ([User](/sdk/ios/send-message#user-object)):

      | Parameter | Type     | Value               |
      | --------- | -------- | ------------------- |
      | uid       | `String` | `"cometchat-uid-2"` |
      | name      | `String` | `"George Alan"`     |
    </Tab>

    <Tab title="Error Response">
      **Object Type:** [CometChatException](/sdk/ios/send-message#cometchatexception-object)

      | Parameter        | Type     | Value                                                      |
      | ---------------- | -------- | ---------------------------------------------------------- |
      | errorCode        | `String` | `"ERR_NOT_LOGGED_IN"`                                      |
      | errorDescription | `String` | `"Please log in to CometChat before calling this method."` |
    </Tab>
  </Tabs>
</Accordion>

<Accordion title="Sample Payloads - Group Conversation">
  <Tabs>
    <Tab title="Request">
      **Method:** `messagesRequest.fetchPrevious(onSuccess:onError:)`

      **Object Type:** `MessagesRequest.MessageRequestBuilder`

      | Parameter   | Type     | Value                |
      | ----------- | -------- | -------------------- |
      | guid        | `String` | `"cometchat-guid-1"` |
      | limit       | `Int`    | `50`                 |
      | hideReplies | `Bool`   | `true`               |
    </Tab>

    <Tab title="Success Response">
      **Object Type:** `[BaseMessage]` (Array excluding thread replies)

      **Response Summary:**

      | Parameter   | Type     | Value                |
      | ----------- | -------- | -------------------- |
      | uid/guid    | `String` | `"cometchat-guid-1"` |
      | hideReplies | `Bool`   | `true`               |
      | count       | `Int`    | `50`                 |

      **Note:** Messages with `parentMessageId > 0` are excluded from results.

      **Message\[0]** ([TextMessage](/sdk/ios/send-message#textmessage-object)):

      | Parameter       | Type                        | Value                |
      | --------------- | --------------------------- | -------------------- |
      | id              | `Int`                       | `36590`              |
      | parentMessageId | `Int`                       | `0`                  |
      | senderUid       | `String`                    | `"cometchat-uid-2"`  |
      | receiverUid     | `String`                    | `"cometchat-guid-1"` |
      | messageType     | `CometChat.MessageType`     | `0` (`.text`)        |
      | messageCategory | `CometChat.MessageCategory` | `0` (`.message`)     |
      | sentAt          | `Int`                       | `1770622963`         |
      | replyCount      | `Int`                       | `0`                  |
      | text            | `String`                    | `"Asdasd"`           |
      | tags            | `[String]`                  | `[]` (empty array)   |

      **Message\[0].sender** ([User](/sdk/ios/send-message#user-object)):

      | Parameter | Type     | Value               |
      | --------- | -------- | ------------------- |
      | uid       | `String` | `"cometchat-uid-2"` |
      | name      | `String` | `"George Alan"`     |

      **Message\[44]** ([CustomMessage](/sdk/ios/send-message#custommessage-object)):

      | Parameter       | Type                        | Value                 |
      | --------------- | --------------------------- | --------------------- |
      | id              | `Int`                       | `37141`               |
      | parentMessageId | `Int`                       | `0`                   |
      | senderUid       | `String`                    | `"cometchat-uid-2"`   |
      | receiverUid     | `String`                    | `"cometchat-guid-1"`  |
      | messageCategory | `CometChat.MessageCategory` | `3` (`.custom`)       |
      | sentAt          | `Int`                       | `1770726132`          |
      | replyCount      | `Int`                       | `0`                   |
      | customType      | `String`                    | `"extension_sticker"` |

      **Message\[44].customData** (`[String: Any]`):

      | Key           | Type     | Value                                                         |
      | ------------- | -------- | ------------------------------------------------------------- |
      | sticker\_name | `String` | `"momo_13.png"`                                               |
      | sticker\_url  | `String` | `"https://data-in.cc-cluster-2.io/stickers/momo/momo_13.png"` |

      **Message\[44].sender** ([User](/sdk/ios/send-message#user-object)):

      | Parameter | Type     | Value               |
      | --------- | -------- | ------------------- |
      | uid       | `String` | `"cometchat-uid-2"` |
      | name      | `String` | `"George Alan"`     |

      **Message\[48]** ([MediaMessage](/sdk/ios/send-message#mediamessage-object)):

      | Parameter       | Type                        | Value                |
      | --------------- | --------------------------- | -------------------- |
      | id              | `Int`                       | `37395`              |
      | parentMessageId | `Int`                       | `0`                  |
      | senderUid       | `String`                    | `"cometchat-uid-4"`  |
      | receiverUid     | `String`                    | `"cometchat-guid-1"` |
      | messageType     | `CometChat.MessageType`     | `1` (`.image`)       |
      | messageCategory | `CometChat.MessageCategory` | `0` (`.message`)     |
      | sentAt          | `Int`                       | `1770895286`         |
      | replyCount      | `Int`                       | `0`                  |
      | caption         | `String?`                   | `nil`                |

      **Message\[48].attachment** ([Attachment](/sdk/ios/send-message#attachment-object)):

      | Parameter     | Type     | Value                                                                                                             |
      | ------------- | -------- | ----------------------------------------------------------------------------------------------------------------- |
      | fileName      | `String` | `"image_1770895286.519397.jpg"`                                                                                   |
      | fileExtension | `String` | `"jpg"`                                                                                                           |
      | fileSize      | `Double` | `1544221.0`                                                                                                       |
      | fileMimeType  | `String` | `"image/jpeg"`                                                                                                    |
      | fileUrl       | `String` | `"https://data-in.cometchat.io/279557705a948ad6/media/1770895286_854529381_a94b8cf283d5bcb6e639627043c62d5b.jpg"` |

      **Message\[48].sender** ([User](/sdk/ios/send-message#user-object)):

      | Parameter | Type     | Value               |
      | --------- | -------- | ------------------- |
      | uid       | `String` | `"cometchat-uid-4"` |
      | name      | `String` | `"Susan Marie"`     |
    </Tab>

    <Tab title="Error Response">
      **Object Type:** [CometChatException](/sdk/ios/send-message#cometchatexception-object)

      | Parameter        | Type     | Value                                                      |
      | ---------------- | -------- | ---------------------------------------------------------- |
      | errorCode        | `String` | `"ERR_NOT_LOGGED_IN"`                                      |
      | errorDescription | `String` | `"Please log in to CometChat before calling this method."` |
    </Tab>
  </Tabs>
</Accordion>

***

## 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             |
