> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-feature-react-native-sdk-quotedmessage-a.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Threaded Messages

Enhance your iOS chat app with threaded messaging by integrating CometChat’s **UIKit for iOS**, allowing users to reply to specific messages within a focused thread view.

## Overview

Threaded messages allow users to reply to specific messages within a conversation, creating a sub-conversation for improved clarity and context. With CometChat’s UIKit for iOS, you can:

* Display a dedicated thread view.
* View and send replies to a selected message.
* Maintain context between the main conversation and the thread.

## Prerequisites

* A working UIKit-based iOS project.
* CometChat UIKit for iOS v4+ installed via Swift Package Manager or CocoaPods.
* Valid CometChat **App ID**, **Region**, and **Auth Key**.
* A navigation controller configured in your project.

## Components

| Component                        | Role                                                           |
| :------------------------------- | :------------------------------------------------------------- |
| `CometChatMessageList`           | Displays messages and provides `onThreadRepliesClick` handler. |
| `CometChatThreadedMessageHeader` | Shows the parent message context at the top of the thread.     |
| `CometChatMessageComposer`       | Composes messages with an optional `parentMessageId`.          |
| `ThreadedMessagesVC`             | View controller that hosts the threaded conversation.          |

## Integration Steps

### Show the "Reply in Thread" Option

Navigate to the thread when a message’s thread icon is tapped.

```swift theme={null}
messageListView.set(onThreadRepliesClick: { [weak self] message, _ in
    guard let self = self else { return }
    let threadVC = ThreadedMessagesVC()
    threadVC.parentMessage = message
    self.navigationController?.pushViewController(threadVC, animated: true)
})
```

**File reference:**\
[`ThreadedMessagesVC.swift`](https://github.com/cometchat/cometchat-uikit-ios/blob/v5/SampleApp/View%20Controllers/CometChat%20Components/ThreadedMessagesVC.swift)

Captures user intent and opens a focused thread screen.

### Navigate to the Thread Screen

Show a dedicated UI for thread replies.

In `ThreadedMessagesVC.swift`:

```swift theme={null}
view.addSubview(parentMessageView)
view.addSubview(messageListView)
view.addSubview(composerView)
```

Header:

```swift theme={null}
let parentMessageContainerView = CometChatThreadedMessageHeader()
```

Message List:

```swift theme={null}
messageListView.set(user: user, parentMessage: parentMessage)
```

Provides a focused UI for thread interactions.

### Send a Threaded Message

Ensure new replies are attached to the correct parent message.

```swift theme={null}
composerView.set(parentMessageId: parentMessage?.id ?? 0)
```

Set the conversation context:

```swift theme={null}
composerView.set(user: user)
composerView.set(group: group)
```

### Fetch & Display Thread Replies

Only messages that are part of the thread are displayed.

Handled internally by:

```swift theme={null}
messageListView.set(user: user, parentMessage: parentMessage)
```

Ensures `CometChatMessageList` fetches replies using the `parentMessageId`.

## Customization Options

* **Header Styling:** Customize `CometChatThreadedMessageHeader` (fonts, colors, etc.).
* **Composer:** Modify placeholder text, input styles, and icons.
* **Navigation:** Add a custom `navigationItem.leftBarButtonItem` for back navigation.

## Filtering / Edge Cases

* **Parent Message Deleted:** Display a fallback UI or disable the composer.
* **No Replies:** Show an empty state (e.g., “No replies yet”).
* **Offline Mode:** Disable the composer and queue thread operations.

## Error Handling & Edge Cases

* **Fetch Failures:** Show an error UI or retry mechanism when loading thread messages.
* **Send Failures:** Handle send errors via delegate callbacks or show an alert with retry.
* **Loading States:** Display a `UIActivityIndicatorView` during fetch/send operations.
* **Blocked Users:** Remove the composer and display a blocked status label.

## Summary / Feature Matrix

| Feature                   | Implementation                                   |
| :------------------------ | :----------------------------------------------- |
| Show thread option        | `CometChatMessageList.onThreadRepliesClick`      |
| Thread view screen        | `ThreadedMessagesVC.swift`                       |
| Display threaded messages | `CometChatMessageList.set(parentMessage:)`       |
| Send threaded message     | `CometChatMessageComposer.set(parentMessageId:)` |
| Thread header             | `CometChatThreadedMessageHeader`                 |
| Handle blocked user       | Remove composer & show a blocked user label      |

<CardGroup>
  <Card title="Full Sample App">
    Explore a complete sample application demonstrating threaded messaging.

    [View on GitHub](https://github.com/cometchat/cometchat-uikit-ios/tree/v5/SampleApp)
  </Card>

  <Card title="UIKit Source Code">
    Browse the CometChat UIKit for iOS source code.

    [View on GitHub](https://github.com/cometchat/cometchat-uikit-ios/tree/v5)
  </Card>
</CardGroup>
