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

> Listen to and emit UI Kit events: what this user did elsewhere in the Kit, and the events your own screens must announce.

The UI Kit has an event bus. It carries what **this user did** through the Kit — sent a
message, blocked someone, kicked a member — so every other component showing the same data can
update.

The Chat SDK already tells you what *other people* did. It does not echo this user's own
actions back, which is why the bus exists: the component that performed the action announces
it, and the others react. SDK callbacks are not forwarded onto the Kit's buses, so listen to
the SDK directly for other people's messages, typing and receipts.

***

## Event Families

Events come in families: message, user, group, conversation, call, thread and UI.

> [tab] SwiftUI
>
> The bus is `CometChatEvents`. Each family has a modifier that listens to every event in it,
> and most events also have a modifier named after them.
>
> | Family | Family modifier | Per-event modifiers |
> | - | - | - |
> | Message | `onCometChatMessageEvents` | `onCometChatMessageSent`, `…MessageEdited`, `…MessageDeleted`, `…MessageRead`, `…MessagePinned`, `…MessageUnpinned`, `…MessageSaved`, `…MessageUnsaved`, `…MessageReceived` (text, media or custom), `…TextMessageReceived`, `…MediaMessageReceived`, `…CustomMessageReceived`, `…TypingStarted`, `…TypingEnded`, `…MessagesDelivered`, `…MessagesRead`, `…ReactionAdded`, `…ReactionRemoved`, `…TransientMessageReceived`, `…LiveReaction` |
> | User | `onCometChatUserEvents` | `onCometChatUserBlocked`, `…UserUnblocked` |
> | Group | `onCometChatGroupEvents` | `onCometChatGroupCreated`, `…GroupDeleted`, `…GroupLeft`, `…MemberJoined`, `…MembersAdded`, `…MemberKicked`, `…MemberBanned`, `…MemberUnbanned`, `…MemberScopeChanged`, `…OwnershipChanged` |
> | Call | `onCometChatCallEvents` | `onCometChatOutgoingCall`, `…CallAccepted`, `…CallRejected`, `…CallEnded` |
> | Conversation | `onCometChatConversationEvents` | `onCometChatConversationDeleted` |
> | UI | `onCometChatUIEvents` | `onCometChatActiveChatChanged`, `…ComposeMessage`, `…OpenChat` |
>
> Events with no per-event modifier — `replyToMessage`, `messageEditedByOther`,
> `messageDeletedByOther`, `messagesDeliveredToAll`, `messagesReadByAll`, `messageModerated`,
> `interactionGoalCompleted`, `conversationUpdated`, `showPanel`, `hidePanel`,
> `agentChatThreadResolved`, `cardActionClicked` and the thread family's
> `subscriptionChanged` — arrive through the family modifier.

> [tab] UIKit
>
> There are seven buses, each with a listener protocol:
>
> | Bus / listener | Methods |
> | - | - |
> | `CometChatMessageEvents` / `CometChatMessageEventListener` | `ccMessageSent(message:status:)`, `ccMessageEdited(message:status:)`, `ccMessageDeleted(message:)`, `ccMessageRead(message:)`, `ccReplyToMessage(message:status:)`, `ccLiveReaction(icon:)`, `onTextMessageReceived(textMessage:)`, `onMediaMessageReceived(mediaMessage:)`, `onCustomMessageReceived(customMessage:)`, `onTypingStarted(_:)`, `onTypingEnded(_:)`, `onMessagesDelivered(receipt:)`, `onMessagesRead(receipt:)`, `onMessagesDeliveredToAll(receipt:)`, `onMessagesReadByAll(receipt:)`, `onMessageReactionAdded(reactionEvent:)`, `onMessageReactionRemoved(reactionEvent:)`, `onTransientMessageReceived(_:)`, `onMessageEdited(message:)`, `onMessageDeleted(message:)`, `onMessageModerated(message:)`, `onInteractionGoalCompleted(receipt:)`, `onMessagePinned(message:)`, `onMessageUnpinned(message:)`, `onMessageSaved(message:)`, `onMessageUnsaved(message:)` |
> | `CometChatUserEvents` / `CometChatUserEventListener` | `ccUserBlocked(user:)`, `ccUserUnblocked(user:)` |
> | `CometChatGroupEvents` / `CometChatGroupEventListener` | `ccGroupCreated(group:)`, `ccGroupDeleted(group:)`, `ccGroupLeft(action:leftUser:leftGroup:)`, `ccGroupMemberJoined(joinedUser:joinedGroup:)`, `ccGroupMemberAdded(messages:usersAdded:groupAddedIn:addedBy:)`, `ccGroupMemberKicked(action:kickedUser:kickedBy:kickedFrom:)`, `ccGroupMemberBanned(action:bannedUser:bannedBy:bannedFrom:)`, `ccGroupMemberUnbanned(action:unbannedUser:unbannedBy:unbannedFrom:)`, `ccGroupMemberScopeChanged(action:updatedUser:scopeChangedTo:scopeChangedFrom:group:)`, `ccOwnershipChanged(group:newOwner:)` |
> | `CometChatConversationEvents` / `CometChatConversationEventListener` | `ccConversationDeleted(conversation:)`, `ccUpdateConversation(conversation:)` |
> | `CometChatCallEvents` / `CometChatCallEventListener` | `ccOutgoingCall(call:)`, `ccCallAccepted(call:)`, `ccCallRejected(call:)`, `ccCallEnded(call:)` |
> | `CometChatThreadEvents` / `CometChatThreadEventListener` | `ccThreadSubscriptionChanged(parentMessageId:isSubscribed:)` |
> | `CometChatUIEvents` / `CometChatUIEventListener` | `showPanel(id:alignment:view:)`, `hidePanel(id:alignment:)`, `ccActiveChatChanged(id:lastMessage:user:group:unreadCount:)`, `ccComposeMessage(id:text:)`, `openChat(user:group:)`, `ccAgentChatThreadResolved(receiverId:parentMessageId:)`, `ccCardActionClicked(message:action:)` |
>
> All seven listener protocols are `@objc`, so Objective-C classes can conform. A conforming
> type must be a class that inherits from `NSObject` — a view controller already does.
>
> `ccComposeMessage(id:text:)` carries an id and text, not a message, and `ccLiveReaction(icon:)`
> carries an image name. `ccMessageDeleted` is emitted by the pinned-messages and AI chat-history
> screens; the message list's own delete does not emit it.

***

## Listening to Events

> [tab] SwiftUI
>
> Every event has a modifier named after it. It listens while the view is on screen and stops
> when the view goes. The closure receives the event's fields.
>
> ```swift theme={null}
> .onCometChatMessageSent { message, status in
>     if status == .success { print("sent", message.id) }
> }
> ```
>
> Each family also has one modifier for every event in it:
>
> ```swift theme={null}
> .onCometChatGroupEvents { event in
>     if case .memberKicked(_, let user, _, let group) = event {
>         print(user.name ?? "", "left", group.name ?? "")
>     }
> }
> ```
>
> Outside a view — in a store or a service — iterate the family's stream. Leaving the loop, or
> cancelling its task, stops listening.
>
> ```swift theme={null}
> for await event in CometChatEvents.userEvents {
>     if case .userBlocked(let user) = event { print("blocked", user.uid ?? "") }
> }
> ```

> [tab] UIKit
>
> Conform to the family's listener protocol — every method is optional — and add yourself under
> an id. Listeners are held weakly, so a released listener simply stops hearing;
> `removeListener(_:)` stops one earlier. Adding again under the same id replaces the earlier
> listener.
>
> ```swift theme={null}
> final class ChatsViewController: UIViewController, CometChatUserEventListener, CometChatGroupEventListener {
>
>     override func viewDidLoad() {
>         super.viewDidLoad()
>         CometChatUserEvents.addListener("chats", self)
>         CometChatGroupEvents.addListener("chats", self)
>     }
>
>     func ccUserBlocked(user: User) {
>         print("blocked", user.uid ?? "")
>     }
>
>     func ccGroupMemberKicked(action: ActionMessage, kickedUser: User, kickedBy: User, kickedFrom: Group) {
>         print(kickedUser.name ?? "", "left", kickedFrom.name ?? "")
>     }
> }
> ```
>
> A listener hears events from anywhere — this toolkit, the SwiftUI toolkit, and your own
> code — because every bus is a view of one Kit bus.

Handlers run on the main thread, in the order the events were emitted. In SwiftUI, a stream
listener that falls behind keeps the newest events of each family — 64 message, 32 group, 16
user, conversation, UI and thread, 8 call — and loses older ones.

***

## Emitting Events

Some actions happen on screens **you** build — a profile screen with a block button, a
group-details screen that adds members. The Kit cannot see them, so emit the event yourself, or
the Kit's lists go stale.

> [tab] SwiftUI
>
> ```swift theme={null}
> let user = User(uid: "bob", name: "Bob")
> CometChatEvents.emitUserEvent(.userBlocked(user: user))
> ```
>
> | After your screen… | Emit | Otherwise |
> | - | - | - |
> | creates a group | `.groupCreated(group:)` | the group is missing from Conversations and Groups until they refetch |
> | joins a group | `.memberJoined(user:group:)` | Groups, Group Members, the message header's member count and the Conversations row go stale |
> | adds members | `.membersAdded(actions:users:group:addedBy:)` | as above, and the message list shows no "added" message |
> | leaves a group | `.groupLeft(action:user:group:)` | the group stays in Conversations and Groups |
> | deletes a group | `.groupDeleted(group:)` | the group stays in Conversations and Groups |
> | transfers ownership | `.ownershipChanged(group:newOwner:)` | Groups, Group Members and the header show the old owner |
> | deletes a conversation | `.conversationDeleted(conversation:)` | the conversation stays in the list |
> | blocks a user | `.userBlocked(user:)` | the chat stays in Conversations; Users and the header show the user unblocked |
> | unblocks a user | `.userUnblocked(user:)` | Users and the header show the user blocked |

> [tab] UIKit
>
> Each bus has a static function per event, with the listener method's name:
>
> ```swift theme={null}
> let user = User(uid: "bob", name: "Bob")
> CometChatUserEvents.ccUserBlocked(user: user)
> ```
>
> | After your screen… | Emit | Otherwise |
> | - | - | - |
> | creates a group | `CometChatGroupEvents.ccGroupCreated(group:)` | the group is missing from Conversations and Groups until they refetch |
> | joins a group | `ccGroupMemberJoined(joinedUser:joinedGroup:)` | Groups, Group Members, the message header's member count and the Conversations row go stale |
> | adds members | `ccGroupMemberAdded(messages:usersAdded:groupAddedIn:addedBy:)` | as above, and the message list shows no "added" message |
> | leaves a group | `ccGroupLeft(action:leftUser:leftGroup:)` | the group stays in Conversations and Groups |
> | deletes a group | `ccGroupDeleted(group:)` | the group stays in Conversations and Groups |
> | transfers ownership | `ccOwnershipChanged(group:newOwner:)` | Groups, Group Members and the header show the old owner |
> | deletes a conversation | `CometChatConversationEvents.ccConversationDeleted(conversation:)` | the conversation stays in the list |
> | blocks a user | `CometChatUserEvents.ccUserBlocked(user:)` | the chat stays in Conversations; Users and the header show the user unblocked |
> | unblocks a user | `CometChatUserEvents.ccUserUnblocked(user:)` | Users and the header show the user blocked |

***

## Sending Through the Kit

Send through the Kit rather than the SDK, and the message is announced as it is sent, as it
succeeds and if it fails — so the message list and Conversations update without any wiring.

> [tab] SwiftUI
>
> Send with `CometChatSession`:
>
> ```swift theme={null}
> let message = TextMessage(receiverUid: "bob", text: "Hello", receiverType: .user)
> let sent = try await CometChatSession.sendTextMessage(message)
> print(sent.id)
> ```
>
> | Function | Emits |
> | - | - |
> | `sendTextMessage(_:)`, `sendMediaMessage(_:)`, `sendCustomMessage(_:)` | `.messageSent(message:status:)` with `.inProgress` first, then `.success` with the server's message, or `.error` with yours |
>
> The sender, the local id (`muid`) and the sent time are filled in when missing. A custom
> message is sent with its push notification on. On failure the message's metadata holds the
> error under `error`, `errorCode` and `errorDetails`, and the function throws.

> [tab] UIKit
>
> `CometChatUIKit.sendTextMessage(message:)` and its media and custom siblings send and announce
> the message — `ccMessageSent` with `.inProgress`, then `.success` or `.error`. An optional
> `completion` reports the result:
>
> ```swift theme={null}
> let message = TextMessage(receiverUid: "bob", text: "Hello", receiverType: .user)
> CometChatUIKit.sendTextMessage(message: message) { result in
>     if case .failure(let error) = result { print(error.message) }
> }
> ```

***

## Thread Subscription

Following a thread is on by default. `CometChatThreadSubscription` holds the rules the Kit
uses; call it from your own control to stay in step with the Kit's.

> [tab] SwiftUI
>
> ```swift theme={null}
> let parent = TextMessage(receiverUid: "bob", text: "Root", receiverType: .user)
> if CometChatThreadSubscription.isAvailableForThread(parent),
>    let result = await CometChatThreadSubscription.toggle(parent, currentlySubscribed: parent.threadSubscribed) {
>     print(result)
> }
> ```
>
> `toggle` updates every surface at once, then asks the server, and puts the old state back if
> the server refuses. It returns `nil` — and does nothing — while a toggle for the same thread is
> in flight or ran in the last 400 ms. Every change is announced as the thread family's
> `.subscriptionChanged(parentMessageId:subscribed:)`.

> [tab] UIKit
>
> `CometChatThreadSubscription.withToggleSlot(parentMessageId:_:)` runs your toggle only if the
> Kit's own is not in flight for that thread, and releases the slot when you call the
> completion:
>
> ```swift theme={null}
> let granted = CometChatThreadSubscription.withToggleSlot(parentMessageId: 42) { done in
>     // subscribe or unsubscribe here, then:
>     done()
> }
> print(granted)
> ```

***

## Next Steps

> [card] [Customization Events](https://www.cometchat.com/docs/ui-kit/ios/v6/customization-events.md)
>
> React to events in your own screens

> [card] [Message List](https://www.cometchat.com/docs/ui-kit/ios/v6/message-list.md)
>
> The component most events update

> [card] [Conversations](https://www.cometchat.com/docs/ui-kit/ios/v6/conversations.md)
>
> Stays in step with the events your screens emit

> [card] [Groups](https://www.cometchat.com/docs/ui-kit/ios/v6/groups.md)
>
> Follows group events from anywhere in the Kit


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