> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Threaded Messages

> Add threaded replies to a CometChat iOS UI Kit v6 chat: open a thread from the message list, show the parent message, list the replies and reply in the thread.

> [accordion] AI Integration Quick Reference
>
> ```json theme={null}
> {
>   "products": ["CometChatSwiftUI", "CometChatUIKit"],
>   "components": ["CometChatThreadHeader", "CometChatMessageList", "CometChatMessageComposer", "CometChatMessageHeader"],
>   "entry": "CometChatMessageList onThreadRepliesClick (SwiftUI) / set(onThreadRepliesClick:) (UIKit)",
>   "thread list": "CometChatMessageList(user:group:parentMessage:) / set(user:parentMessage:), set(group:parentMessage:)",
>   "thread composer": "CometChatMessageComposer(user:group:parentMessageId:) / set(parentMessageId:)",
>   "subscription": "CometChatThreadSubscription, hideThreadSubscriptionOption, hideThreadSubscription / hideThreadSubscriptionButton",
>   "note": "The Kit has no thread screen: you compose one from the three components."
> }
> ```

A thread lets people reply to one message in a focused sub-conversation. The Kit gives you
the pieces — a header that shows the parent message, a message list in thread mode and a
composer that replies in the thread — and you put them on a screen of your own.

## Overview

* The message list raises a callback when someone taps **N replies** under a message or
  chooses **Reply in thread** from its menu.
* Your thread screen shows the parent in `CometChatThreadHeader`, the replies in
  `CometChatMessageList` given the parent, and a `CometChatMessageComposer` given the
  parent's id.
* Replies, edits, reactions and receipts update live on both screens.

## Prerequisites

* The Kit is installed, initialised and a user is logged in — see
  [Getting Started](https://www.cometchat.com/docs/ui-kit/ios/v6/overview.md).
* A chat screen that already shows `CometChatMessageList` for a user or a group.

## Components

| Component | Role |
| - | - |
| `CometChatMessageList` | In the chat: raises the thread callback. In the thread: lists the replies when given the parent message. |
| `CometChatThreadHeader` | The parent message as a bubble, a live "N Replies" bar and a follow bell. Fetches nothing. |
| `CometChatMessageComposer` | Sends replies into the thread when given the parent's id **and** the chat's user or group. |
| `CometChatMessageHeader` | Optional: your thread screen's bar. |

## Integration Steps

### Step 1: Open the thread from the message list

Handle the thread callback on the chat screen and navigate to your thread screen with the
parent message.

### Step 2: Build the thread screen

Show the list in thread mode, with the thread header and the composer replying in the thread as
bars over it, hosted as
[Conversation List + Message View](https://www.cometchat.com/docs/ui-kit/ios/v6/conversation-message-view.md#step-2-set-up-the-message-screen)
hosts the chat's, so the rows pass under the bars. Pass the **same** user or group the chat uses:
the list and the composer need the chat as well as the parent.

> [tab] SwiftUI
>
> ```swift ChatView.swift theme={null}
> import SwiftUI
> import CometChatSwiftUI
> import CometChatSDK
>
> /// `BaseMessage` is neither `Hashable` nor `Identifiable`, so the route is keyed on its id.
> struct ThreadRoute: Identifiable, Hashable {
>     let parent: BaseMessage
>     var id: Int { parent.id }
>     static func == (lhs: ThreadRoute, rhs: ThreadRoute) -> Bool { lhs.id == rhs.id }
>     func hash(into hasher: inout Hasher) { hasher.combine(id) }
> }
>
> struct ChatView: View {
>     let user: CometChatSDK.User?
>     let group: CometChatSDK.Group?
>     @State private var thread: ThreadRoute?
>
>     var body: some View {
>         CometChatMessageList(
>             user: user,
>             group: group,
>             // "N replies", or Reply in thread from the menu
>             onThreadRepliesClick: { parent in thread = ThreadRoute(parent: parent) }
>         )
>         .modifier(ChatBars(
>             header: CometChatMessageHeader(user: user, group: group),
>             composer: CometChatMessageComposer(user: user, group: group)))
>         .navigationDestination(item: $thread) { route in
>             ThreadView(parent: route.parent, user: user, group: group)
>         }
>     }
> }
>
> struct ThreadView: View {
>     let parent: BaseMessage
>     let user: CometChatSDK.User?
>     let group: CometChatSDK.Group?
>
>     var body: some View {
>         CometChatMessageList(user: user, group: group, parentMessage: parent)
>             .modifier(ChatBars(
>                 header: CometChatThreadHeader(parentMessage: parent),
>                 composer: CometChatMessageComposer(user: user, group: group, parentMessageId: parent.id)))
>             .navigationTitle("Thread")
>     }
> }
>
> /// The header and the composer as bars over the list: the rows scroll under them and rest clear of them.
> struct ChatBars<Header: View, Composer: View>: ViewModifier {
>     let header: Header
>     let composer: Composer
>
>     func body(content: Content) -> some View {
>         let list = content.frame(maxWidth: .infinity, maxHeight: .infinity)
>         if #available(iOS 26.0, *) {
>             // The list's scroll edge effect reaches under each bar: the rows fade softly behind it.
>             list
>                 .safeAreaBar(edge: .top, spacing: 0) { header }
>                 .safeAreaBar(edge: .bottom, spacing: 0) { composer }
>         } else {
>             list
>                 .safeAreaInset(edge: .top, spacing: 0) { header }
>                 .safeAreaInset(edge: .bottom, spacing: 0) { composer }
>         }
>     }
> }
> ```

> [tab] UIKit
>
> ```swift ThreadViewController.swift theme={null}
> import UIKit
> import CometChatUIKit
> import CometChatSDK
>
> final class ChatViewController: UIViewController {
>     let user: CometChatSDK.User
>     let messageList = CometChatMessageList()
>
>     init(user: CometChatSDK.User) {
>         self.user = user
>         super.init(nibName: nil, bundle: nil)
>     }
>     required init?(coder: NSCoder) { fatalError() }
>
>     override func viewDidLoad() {
>         super.viewDidLoad()
>         messageList.set(user: user)
>         // "N replies", or Reply in thread from the menu
>         messageList.set(onThreadRepliesClick: { [weak self] parent, _ in
>             guard let self else { return }
>             let thread = ThreadViewController(parentMessage: parent, user: self.user)
>             self.navigationController?.pushViewController(thread, animated: true)
>         })
>         // … host the header and the composer over the list, as ThreadViewController does …
>     }
> }
>
> final class ThreadViewController: UIViewController {
>     let parentMessage: BaseMessage
>     let user: CometChatSDK.User
>     let threadHeader = CometChatThreadHeader()
>     let composer = CometChatMessageComposer()
>     /// Holds the list, so the bars' heights can reach it as safe area.
>     let listController = MessageListController()
>     var list: CometChatMessageList { listController.messageList }
>
>     init(parentMessage: BaseMessage, user: CometChatSDK.User) {
>         self.parentMessage = parentMessage
>         self.user = user
>         super.init(nibName: nil, bundle: nil)
>     }
>     required init?(coder: NSCoder) { fatalError() }
>
>     override func viewDidLoad() {
>         super.viewDidLoad()
>         title = "Thread"
>         view.backgroundColor = .systemBackground
>         // `set(controller:)` is where the header shows its follow / unfollow toast.
>         threadHeader.set(parentMessage: parentMessage).set(controller: self)
>         list.set(user: user, parentMessage: parentMessage)
>         composer.set(user: user).set(parentMessageId: parentMessage.id)
>
>         // The list first, so the bars sit over it.
>         addChild(listController)
>         for part in [listController.view, threadHeader, composer] as [UIView] {
>             part.translatesAutoresizingMaskIntoConstraints = false
>             view.addSubview(part)
>         }
>         listController.didMove(toParent: self)
>         NSLayoutConstraint.activate([
>             listController.view.topAnchor.constraint(equalTo: view.topAnchor),
>             listController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
>             listController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
>             // To the composer's bottom: the rows pass under the composer, never under the keyboard.
>             listController.view.bottomAnchor.constraint(equalTo: composer.bottomAnchor),
>
>             threadHeader.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
>             threadHeader.leadingAnchor.constraint(equalTo: view.leadingAnchor),
>             threadHeader.trailingAnchor.constraint(equalTo: view.trailingAnchor),
>
>             composer.leadingAnchor.constraint(equalTo: view.leadingAnchor),
>             composer.trailingAnchor.constraint(equalTo: view.trailingAnchor),
>             composer.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor),
>         ])
>
>         if #available(iOS 26.0, *) {
>             // The table's scroll edge effect reaches under each bar: the rows fade softly behind it.
>             for (bar, edge) in [(threadHeader as UIView, UIRectEdge.top), (composer, .bottom)] {
>                 let element = UIScrollEdgeElementContainerInteraction()
>                 element.scrollView = list.tableView
>                 element.edge = edge
>                 bar.addInteraction(element)
>             }
>         }
>     }
>
>     // The bars' heights, as the list's safe area. The composer's grows with the keyboard and a reply
>     // preview, and the list keeps its newest row clear of it.
>     override func viewDidLayoutSubviews() {
>         super.viewDidLayoutSubviews()
>         let container: UIView = listController.view
>         // The list's safe area is the screen's (the status bar, the home indicator) plus `added`.
>         let added = listController.additionalSafeAreaInsets
>         let inherited = container.safeAreaInsets
>         let headerBottom = view.convert(threadHeader.frame, to: container).maxY
>         let composerTop = view.convert(composer.frame, to: container).minY
>         let insets = UIEdgeInsets(
>             top: max(0, headerBottom - (inherited.top - added.top)),
>             left: 0,
>             bottom: max(0, container.bounds.maxY - composerTop - (inherited.bottom - added.bottom)),
>             right: 0)
>         if insets != added { listController.additionalSafeAreaInsets = insets }
>     }
> }
>
> /// The list, full-bleed, in a view controller of its own: its `additionalSafeAreaInsets` are how the
> /// bars' heights reach the list.
> final class MessageListController: UIViewController {
>     let messageList = CometChatMessageList()
>
>     override func viewDidLoad() {
>         super.viewDidLoad()
>         messageList.translatesAutoresizingMaskIntoConstraints = false
>         view.addSubview(messageList)
>         NSLayoutConstraint.activate([
>             messageList.topAnchor.constraint(equalTo: view.topAnchor),
>             messageList.leadingAnchor.constraint(equalTo: view.leadingAnchor),
>             messageList.trailingAnchor.constraint(equalTo: view.trailingAnchor),
>             messageList.bottomAnchor.constraint(equalTo: view.bottomAnchor),
>         ])
>     }
> }
> ```
>
> For a group, call `list.set(group:parentMessage:)` and `composer.set(group:)` instead.

> [!NOTE]
>
> The thread header fetches nothing — hand it the parent you already hold. It is at most
> 250 pt tall (`maxHeight`); a taller parent scrolls inside it.

### Step 3: Handle a blocked user

In a one-to-one thread, the composer stays on screen when you have blocked the other person.
Put your own banner in its place:

> [tab] SwiftUI
>
> ```swift theme={null}
> struct ThreadComposer: View {
>     let parent: BaseMessage
>     let user: CometChatSDK.User
>
>     var body: some View {
>         if user.blockedByMe {
>             Text("You can't send messages to \(user.name ?? "") because you blocked them.")
>                 .frame(maxWidth: .infinity)
>                 .padding()
>         } else {
>             CometChatMessageComposer(user: user, parentMessageId: parent.id)
>         }
>     }
> }
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let composer = CometChatMessageComposer()
> composer.set(user: guideUser).set(parentMessageId: 42)
> composer.isHidden = guideUser.blockedByMe
> ```

See [Block / Unblock User](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-block-unblock-user.md) for the whole flow.

## Thread Subscription

Following a thread is **on by default**. A user who follows a thread is notified of its
replies; the server also subscribes people who start, reply in, or are mentioned in a
thread. The Kit ships two controls for the same state, kept in sync:

1. **Subscribe to thread / Unsubscribe from thread** in the message list's long-press menu.
2. A **bell** on the thread header's reply bar.

Both go through `CometChatThreadSubscription`, so a change made in one place shows in the
other, and in any control of yours that uses it.

### Hide the menu option

> [tab] SwiftUI
>
> ```swift theme={null}
> CometChatMessageList(user: guideUser, hideThreadSubscriptionOption: true)
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let list = CometChatMessageList()
> list.hideThreadSubscriptionOption = true
> ```

### Hear the bell, or hide it

The callback hears the new state once the server accepts it. A failed toggle shows a toast
and calls nothing.

> [tab] SwiftUI
>
> ```swift theme={null}
> CometChatThreadHeader(parentMessage: parentMessage, onSubscriptionToggle: { following in
>     print(following ? "following" : "not following")
> })
>
> // Your bar has its own bell:
> CometChatThreadHeader(parentMessage: parentMessage, hideThreadSubscription: true)
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let threadHeader = CometChatThreadHeader()
> threadHeader.set(parentMessage: threadParent)
> threadHeader.set(onThreadSubscriptionToggle: { following in
>     print(following ? "following" : "not following")
> })
>
> // Your bar has its own bell:
> threadHeader.set(hideThreadSubscriptionButton: true)
> ```

### Your own control

Use `CometChatThreadSubscription` so your control follows the Kit's rules — one toggle per
thread at a time, and every surface updated at once:

```swift theme={null}
let parent = TextMessage(receiverUid: "bob", text: "Root", receiverType: .user)
if CometChatThreadSubscription.isAvailableForThread(parent),
   let following = await CometChatThreadSubscription.toggle(parent, currentlySubscribed: parent.threadSubscribed) {
    print(following)
}
```

`toggle` returns `nil` — and does nothing — while a toggle for the same thread is in flight
or ran in the last 400 ms.

> [!NOTE]
>
> `CometChatSession.initialize(appID:region:subscribePresenceForAllUsers:enableThreadSubscription:)`
> turns the feature off app-wide. It is deprecated — passing `true` does nothing — and kept only
> as that opt-out; prefer hiding the two controls per screen.

## Implementation Flow

1. The user taps **N replies** or chooses **Reply in thread**.
2. The list calls your handler with the parent message.
3. Your thread screen shows the parent, the replies and a composer bound to the thread.
4. A sent reply appears in the thread, the reply count goes up, and the sender now follows
   the thread.

## Edge Cases

| Case | What to do |
| - | - |
| Blocked user in a 1:1 thread | Hide the composer and show a banner (Step 3). |
| Not a member of the group | Join first — see [Group Chat](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-group-chat.md). |
| A deleted reply | Stays counted in "N Replies", as the server's count does. |
| An AI agent's replies | Not counted in "N Replies". |

## Feature Matrix

| Feature | SwiftUI | UIKit |
| - | - | - |
| Open a thread | `onThreadRepliesClick:` | `set(onThreadRepliesClick:)` |
| List replies | `CometChatMessageList(…, parentMessage:)` | `set(user:parentMessage:)` / `set(group:parentMessage:)` |
| Show the parent | `CometChatThreadHeader(parentMessage:)` | `set(parentMessage:)` |
| Reply in the thread | `CometChatMessageComposer(…, parentMessageId:)` | `set(parentMessageId:)` |
| Hide Reply in thread | `hideReplyInThreadOption: true` | `hideReplyInThreadOption = true` |
| Follow / unfollow | menu option + header bell | menu option + header bell |

## Next Steps

> [card] [Thread Header](https://www.cometchat.com/docs/ui-kit/ios/v6/thread-header.md)
>
> Every option of the parent-message strip

> [card] [Message List](https://www.cometchat.com/docs/ui-kit/ios/v6/message-list.md)
>
> Thread mode, options and callbacks

> [card] [Events](https://www.cometchat.com/docs/ui-kit/ios/v6/events.md)
>
> The thread family of events

> [card] [All Guides](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-overview.md)
>
> Browse the other guides


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.