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

# Events & Callbacks

> Handle taps, selection and lifecycle callbacks on CometChat iOS UI Kit v6 components, and listen to and emit the Kit's events in SwiftUI and UIKit.

The UI Kit offers two levels of event handling: **callbacks** on one component, and the Kit's
**event bus**, which carries what this user did anywhere in the Kit.

***

## Component-Level Callbacks

### Click Callbacks

> [tab] SwiftUI
>
> ```swift theme={null}
> CometChatConversations(
>     onItemClick: { conversation in openChat(conversation) },
>     onItemLongClick: { conversation in print("long press", conversation.conversationId ?? "") },
>     onSearchClick: { showSearch() },
>     onBack: { print("back") },
>     onLoad: { list in print("loaded", list.count) },
>     onEmpty: { print("no conversations") },
>     onError: { error in print(error) }
> )
> ```
>
> Callbacks hand you SDK objects — here a `CometChatSDK.Conversation`.

> [tab] UIKit
>
> Fluent setters, which take precedence over the delegate:
>
> ```swift theme={null}
> conversations
>     .set(onItemClick: { conversation, indexPath in openChat(conversation) })
>     .set(onItemLongClick: { conversation, _ in print("long press", conversation.conversationId ?? "") })
>     .set(onSearchClick: { showSearch() })
>     .set(onBack: { print("back") })
>     .set(onError: { error in print(error.errorDescription) })
>     .set(onLoad: { list in print("loaded", list.count) })
>     .set(onEmpty: { print("no conversations") })
> ```
>
> Each list also has a delegate protocol — `CometChatConversationsDelegate` and siblings —
> for a coordinator.

> [!WARNING]
>
> A long press belongs to the context menu on iOS: `onItemLongClick` fires **only when no
> context menu is configured**. React to a menu choice with `onOptionSelected` instead.

***

## Selection Mode

> [tab] SwiftUI
>
> `selection` is a `Binding`, so your view owns it. `onSelection` fires **once**, when the
> user submits from the selection toolbar:
>
> ```swift theme={null}
> struct ForwardPicker: View {
>     @State private var selected: Set<String> = []
>
>     var body: some View {
>         CometChatConversations(
>             selection: $selected,
>             selectionMode: .multiple,
>             onSelection: { picked in forward(picked) }
>         )
>     }
> }
> ```

> [tab] UIKit
>
> ```swift theme={null}
> conversations.selectionMode = .multiple
> conversations.set(onSelection: { picked in forward(picked) })
>
> let current = conversations.getSelectedConversations()
> conversations.clearSelection()
> print(current.count)
> ```

While selection is on, a tap only toggles the row: `onItemClick` and `onItemLongClick` do not
fire.

***

## Global Events

The SDK tells you what **other people** did. It does not echo this user's own actions back,
so the Kit has an event bus: the component that performs an action announces it — a message
sent, a user blocked, a member kicked — and every other component showing the same data
updates.

| Family | Fires when |
| - | - |
| Message | sent, edited, deleted, read, pinned, saved, reacted, typing |
| User | blocked, unblocked |
| Group | created, deleted, left, members joined, added, kicked, banned, scope or owner changed |
| Conversation | deleted, updated |
| Call | outgoing, accepted, rejected, ended |
| UI | active chat changed, compose, open chat, panels, card actions |
| Thread | thread subscription changed |

### Subscribing to Events

> [tab] SwiftUI
>
> Every event has a view modifier that listens while the view is on screen:
>
> ```swift theme={null}
> .onCometChatMessageSent { message, status in
>     if status == .success { print("sent", message.id) }
> }
> ```
>
> Or one modifier per family:
>
> ```swift theme={null}
> .onCometChatGroupEvents { event in
>     if case .memberKicked(_, let user, _, let group) = event {
>         print(user.name ?? "", "left", group.name ?? "")
>     }
> }
> ```
>
> Outside a view, iterate the family's stream:
>
> ```swift theme={null}
> for await event in CometChatEvents.messageEvents {
>     if case .messageDeleted(let message) = event { print("deleted", message.id) }
> }
> ```

> [tab] UIKit
>
> Conform to the family's listener protocol — every method is optional — and add yourself
> under an id. Listeners are held weakly.
>
> ```swift theme={null}
> final class ChatsListener: NSObject, CometChatMessageEventListener, CometChatCallEventListener {
>     func start() {
>         CometChatMessageEvents.addListener("chats-listener", self)
>         CometChatCallEvents.addListener("chats-listener", self)
>     }
>
>     func ccMessageSent(message: BaseMessage, status: MessageStatus) {
>         if status == .success { print("sent", message.id) }
>     }
>
>     func ccCallEnded(call: Call) {
>         print("call ended", call.sessionID ?? "")
>     }
> }
> ```

Handlers run on the main thread, in the order the events were emitted. A listener in one
toolkit hears events from the other and from your own code: both toolkits read one bus.

### Emitting Events

Emit when a screen **you** built does something the Kit cannot see — blocking from your own
profile screen, creating a group from your own form — or the Kit's lists go stale.

> [tab] SwiftUI
>
> ```swift theme={null}
> let blocked = User(uid: "bob", name: "Bob")
> CometChatEvents.emitUserEvent(.userBlocked(user: blocked))
>
> let created = Group(guid: "design", name: "Design", groupType: .public, password: nil)
> CometChatEvents.emitGroupEvent(.groupCreated(group: created))
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let blocked = User(uid: "bob", name: "Bob")
> CometChatUserEvents.ccUserBlocked(user: blocked)
>
> let created = Group(guid: "design", name: "Design", groupType: .public, password: nil)
> CometChatGroupEvents.ccGroupCreated(group: created)
> ```

Send messages through the Kit — `CometChatSession.sendTextMessage(_:)` in SwiftUI,
`CometChatUIKit.sendTextMessage(message:)` in UIKit — and the Kit announces them for you.

### Key Event Types

**Message events**

| Event | Data |
| - | - |
| `messageSent` / `ccMessageSent` | `message`, `status` |
| `messageEdited` / `ccMessageEdited` | `message`, `status` |
| `messageDeleted` / `ccMessageDeleted` | `message` |
| `messageRead` / `ccMessageRead` | `message` |
| `reactionAdded`, `reactionRemoved` | the `ReactionEvent` |
| `typingStarted`, `typingEnded` | the `TypingIndicator` |
| `messagesDelivered`, `messagesRead` | the `MessageReceipt` |

**Call events**

| Event | Data |
| - | - |
| `outgoingCall` / `ccOutgoingCall` | `call` |
| `callAccepted` / `ccCallAccepted` | `call` |
| `callRejected` / `ccCallRejected` | `call` |
| `callEnded` / `ccCallEnded` | `call` |

**Conversation events**

| Event | Data |
| - | - |
| `conversationDeleted` / `ccConversationDeleted` | `conversation` |
| `conversationUpdated` / `ccUpdateConversation` | `conversation` |

The SwiftUI name is the case of the family's event enum (`CometChatMessageEvent` …); the UIKit name is the listener method
and the static emitter.

***

## Component vs Global Events

| Aspect | Component callbacks | Global events |
| - | - | - |
| Scope | one component instance | app-wide |
| Registration | an argument (SwiftUI) or `set(on…:)` (UIKit) | `.onCometChat…` / `CometChatEvents` (SwiftUI), `CometChat…Events.addListener` (UIKit) |
| Use case | handle interaction on one screen | keep several screens in step |
| Example | tap a conversation → open the chat | a message sent → the conversation list updates |

***

## Related

* [Events](https://www.cometchat.com/docs/ui-kit/ios/v6/events.md) — every event, in both toolkits.
* [Customization Overview](https://www.cometchat.com/docs/ui-kit/ios/v6/customization-overview.md) — SDK listeners and Kit events.


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