CometChat Flutter UIKit — Events & Listeners
Two event systems: SDK listeners (from CometChat server) and UI events (from UIKit components).
SDK Listeners (Server → App)
Register on CometChat SDK directly. These fire when the server pushes real-time data.
Available Listener Types
| Listener Mixin |
Register / Remove |
Key Callbacks |
MessageListener |
CometChat.addMessageListener(id, this) / removeMessageListener(id) |
onTextMessageReceived, onMediaMessageReceived, onCustomMessageReceived, onTypingStarted, onTypingEnded, onMessagesDelivered, onMessagesRead, onMessageEdited, onMessageDeleted |
UserListener |
CometChat.addUserListener(id, this) / removeUserListener(id) |
onUserOnline, onUserOffline |
GroupListener |
CometChat.addGroupListener(id, this) / removeGroupListener(id) |
onGroupMemberJoined, onGroupMemberLeft, onGroupMemberKicked, onGroupMemberBanned, onGroupMemberScopeChanged, onMemberAddedToGroup |
CallListener |
CometChat.addCallListener(id, this) / removeCallListener(id) |
onIncomingCallReceived, onOutgoingCallAccepted, onOutgoingCallRejected, onIncomingCallCancelled |
ConnectionListener |
CometChat.addConnectionListener(id, this) / removeConnectionListener(id) |
onConnected, onDisconnected, onConnecting, onFeatureThrottled |
Registration Pattern
class _MyScreenState extends State<MyScreen>
with MessageListener, UserListener {
late final String _listenerId;
@override
void initState() {
super.initState();
_listenerId = 'my_screen_${DateTime.now().millisecondsSinceEpoch}';
CometChat.addMessageListener(_listenerId, this);
CometChat.addUserListener(_listenerId, this);
}
@override
void dispose() {
CometChat.removeMessageListener(_listenerId);
CometChat.removeUserListener(_listenerId);
super.dispose();
}
@override
void onTextMessageReceived(TextMessage message) {
// Handle incoming text message
}
@override
void onUserOnline(User user) {
// Handle user coming online
}
@override
void onUserOffline(User user) {
// Handle user going offline
}
}
UI Events (UIKit Component → App)
These fire when UIKit components perform actions. Use these to react to user interactions across components.
CometChatMessageEvents
// Listen
CometChatMessageEvents.addMessagesListener('my_listener', MyMessageEventListener());
// Remove
CometChatMessageEvents.removeMessagesListener('my_listener');
// Key callbacks in CometChatMessageEventListener:
// ccMessageSent(BaseMessage message, MessageStatus status) — inProgress/sent/error
// ccMessageEdited(BaseMessage message, MessageEditStatus status)
// ccMessageDeleted(BaseMessage message, EventStatus status)
// ccMessageRead(BaseMessage message)
// ccLiveReaction(String reaction)
CometChatUserEvents
CometChatUserEvents.addUsersListener('my_listener', MyUserEventListener());
CometChatUserEvents.removeUsersListener('my_listener');
// Key callbacks:
// ccUserBlocked(User user)
// ccUserUnblocked(User user)
CometChatGroupEvents
CometChatGroupEvents.addGroupsListener('my_listener', MyGroupEventListener());
CometChatGroupEvents.removeGroupsListener('my_listener');
// Key callbacks:
// ccGroupMemberKicked(Action, User kickedUser, User kickedBy, Group)
// ccGroupMemberBanned(Action, User bannedUser, User bannedBy, Group)
// ccGroupMemberScopeChanged(Action, User updatedUser, String newScope, String oldScope, Group)
// ccGroupCreated(Group)
// ccGroupDeleted(Group)
// ccGroupLeft(Action, User, Group)
// ccOwnershipChanged(Group, GroupMember)
CometChatConversationEvents
CometChatConversationEvents.addConversationListListener('my_listener', MyConvListener());
CometChatConversationEvents.removeConversationListListener('my_listener');
// Key callbacks:
// ccConversationDeleted(Conversation)
SDK vs UI Events — When to Use Which
| Scenario |
Use |
| Incoming message from another user |
SDK MessageListener.onTextMessageReceived |
| Current user sent a message via composer |
UI CometChatMessageEvents.ccMessageSent |
| Another user comes online |
SDK UserListener.onUserOnline |
| Current user blocked someone via UIKit |
UI CometChatUserEvents.ccUserBlocked |
| Group member kicked by another admin |
SDK GroupListener.onGroupMemberKicked |
| Current user kicked someone via UIKit |
UI CometChatGroupEvents.ccGroupMemberKicked |
Key distinction: SDK listeners fire for remote events. UI events fire for local user actions performed through UIKit components.
BLoC Components Handle Listeners Automatically
The UIKit BLoCs (ConversationsBloc, MessageListBloc, etc.) register all necessary SDK listeners internally. You do NOT need to register listeners for components you're using — they handle their own real-time updates.
Register your own listeners only when you need to:
- Update custom UI outside UIKit components
- Track events for analytics
- Sync state between screens
ChatSDKEventInitializer
Called automatically by CometChatUIKit._initiateAfterLogin(). Bridges SDK events to UIKit's internal event bus. You never need to call this manually.
Gotchas
- Listener IDs MUST be unique per registration. Using the same ID overwrites the previous listener silently — no error thrown, old listener just stops receiving events.
onTypingStarted/onTypingEnded only fire if subscriptionType was set in UIKitSettings. Omitting it silently disables all presence/typing events.
- SDK listeners fire on the main isolate. Heavy processing in callbacks blocks the UI thread. Offload to
compute() if needed.
ccMessageSent fires three times per message: once with MessageStatus.inProgress, once with MessageStatus.sent (success), or once with MessageStatus.error (failure). Filter by status.
- Connection listener's
onConnected fires on reconnect — use it to trigger a silent refresh of your data.
- Group listener callbacks include an
Action object (which is a BaseMessage subclass). Don't confuse it with the Action widget from Flutter.
Anti-Patterns
// ❌ WRONG — hardcoded listener ID causes silent overwrite
CometChat.addMessageListener('messages', this);
// Later, another screen does the same:
CometChat.addMessageListener('messages', this); // First listener silently lost!
// ✅ CORRECT — unique ID per instance
final id = 'screen_${DateTime.now().millisecondsSinceEpoch}';
CometChat.addMessageListener(id, this);
// ❌ WRONG — registering SDK listeners for a UIKit component you're already using
// ConversationsBloc already handles MessageListener, UserListener, GroupListener internally
CometChat.addMessageListener('redundant', this); // Duplicate handling
// ✅ CORRECT — only register for custom logic outside UIKit components
// Let ConversationsBloc handle its own listeners
// ❌ WRONG — not filtering ccMessageSent status
void onCcMessageSent(BaseMessage msg, MessageStatus status) {
addToList(msg); // Adds 3 times! (inProgress + sent + error)
}
// ✅ CORRECT — filter by status
void onCcMessageSent(BaseMessage msg, MessageStatus status) {
if (status == MessageStatus.sent) {
addToList(msg);
}
}
Checklist
1---2name: cometchat-flutter-events3description: Use when working with real-time events, SDK listeners, or UI event streams in CometChat Flutter UIKit v6. Triggers on mentions of CometChatMessageEvents, CometChatUserEvents, CometChatGroupEvents, CometChatCallEvents, CometChatConversationEvents, MessageListener, UserListener, GroupListener, CallListener, ConnectionListener, ccMessageSent, ccMessageEdited, ccMessageDeleted, ccUserBlocked, ccUserUnblocked, ccGroupMemberKicked, ccGroupMemberBanned, onMessageReceived, onTypingStarted, onTypingEnded, onUserOnline, onUserOffline, onMessageDelivered, onMessageRead, addMessageListener, removeMessageListener, ChatSDKEventInitializer, or any real-time update handling in CometChat.4license: MIT5---67# CometChat Flutter UIKit — Events & Listeners89Two event systems: SDK listeners (from CometChat server) and UI events (from UIKit components).1011## SDK Listeners (Server → App)1213Register on CometChat SDK directly. These fire when the server pushes real-time data.1415### Available Listener Types1617| Listener Mixin | Register / Remove | Key Callbacks |18|----------------|-------------------|---------------|19| `MessageListener` | `CometChat.addMessageListener(id, this)` / `removeMessageListener(id)` | `onTextMessageReceived`, `onMediaMessageReceived`, `onCustomMessageReceived`, `onTypingStarted`, `onTypingEnded`, `onMessagesDelivered`, `onMessagesRead`, `onMessageEdited`, `onMessageDeleted` |20| `UserListener` | `CometChat.addUserListener(id, this)` / `removeUserListener(id)` | `onUserOnline`, `onUserOffline` |21| `GroupListener` | `CometChat.addGroupListener(id, this)` / `removeGroupListener(id)` | `onGroupMemberJoined`, `onGroupMemberLeft`, `onGroupMemberKicked`, `onGroupMemberBanned`, `onGroupMemberScopeChanged`, `onMemberAddedToGroup` |22| `CallListener` | `CometChat.addCallListener(id, this)` / `removeCallListener(id)` | `onIncomingCallReceived`, `onOutgoingCallAccepted`, `onOutgoingCallRejected`, `onIncomingCallCancelled` |23| `ConnectionListener` | `CometChat.addConnectionListener(id, this)` / `removeConnectionListener(id)` | `onConnected`, `onDisconnected`, `onConnecting`, `onFeatureThrottled` |2425### Registration Pattern2627```dart28class _MyScreenState extends State<MyScreen>29 with MessageListener, UserListener {30 late final String _listenerId;3132 @override33 void initState() {34 super.initState();35 _listenerId = 'my_screen_${DateTime.now().millisecondsSinceEpoch}';36 CometChat.addMessageListener(_listenerId, this);37 CometChat.addUserListener(_listenerId, this);38 }3940 @override41 void dispose() {42 CometChat.removeMessageListener(_listenerId);43 CometChat.removeUserListener(_listenerId);44 super.dispose();45 }4647 @override48 void onTextMessageReceived(TextMessage message) {49 // Handle incoming text message50 }5152 @override53 void onUserOnline(User user) {54 // Handle user coming online55 }5657 @override58 void onUserOffline(User user) {59 // Handle user going offline60 }61}62```6364## UI Events (UIKit Component → App)6566These fire when UIKit components perform actions. Use these to react to user interactions across components.6768### CometChatMessageEvents6970```dart71// Listen72CometChatMessageEvents.addMessagesListener('my_listener', MyMessageEventListener());7374// Remove75CometChatMessageEvents.removeMessagesListener('my_listener');7677// Key callbacks in CometChatMessageEventListener:78// ccMessageSent(BaseMessage message, MessageStatus status) — inProgress/sent/error79// ccMessageEdited(BaseMessage message, MessageEditStatus status)80// ccMessageDeleted(BaseMessage message, EventStatus status)81// ccMessageRead(BaseMessage message)82// ccLiveReaction(String reaction)83```8485### CometChatUserEvents8687```dart88CometChatUserEvents.addUsersListener('my_listener', MyUserEventListener());89CometChatUserEvents.removeUsersListener('my_listener');9091// Key callbacks:92// ccUserBlocked(User user)93// ccUserUnblocked(User user)94```9596### CometChatGroupEvents9798```dart99CometChatGroupEvents.addGroupsListener('my_listener', MyGroupEventListener());100CometChatGroupEvents.removeGroupsListener('my_listener');101102// Key callbacks:103// ccGroupMemberKicked(Action, User kickedUser, User kickedBy, Group)104// ccGroupMemberBanned(Action, User bannedUser, User bannedBy, Group)105// ccGroupMemberScopeChanged(Action, User updatedUser, String newScope, String oldScope, Group)106// ccGroupCreated(Group)107// ccGroupDeleted(Group)108// ccGroupLeft(Action, User, Group)109// ccOwnershipChanged(Group, GroupMember)110```111112### CometChatConversationEvents113114```dart115CometChatConversationEvents.addConversationListListener('my_listener', MyConvListener());116CometChatConversationEvents.removeConversationListListener('my_listener');117118// Key callbacks:119// ccConversationDeleted(Conversation)120```121122## SDK vs UI Events — When to Use Which123124| Scenario | Use |125|----------|-----|126| Incoming message from another user | SDK `MessageListener.onTextMessageReceived` |127| Current user sent a message via composer | UI `CometChatMessageEvents.ccMessageSent` |128| Another user comes online | SDK `UserListener.onUserOnline` |129| Current user blocked someone via UIKit | UI `CometChatUserEvents.ccUserBlocked` |130| Group member kicked by another admin | SDK `GroupListener.onGroupMemberKicked` |131| Current user kicked someone via UIKit | UI `CometChatGroupEvents.ccGroupMemberKicked` |132133Key distinction: SDK listeners fire for remote events. UI events fire for local user actions performed through UIKit components.134135## BLoC Components Handle Listeners Automatically136137The UIKit BLoCs (`ConversationsBloc`, `MessageListBloc`, etc.) register all necessary SDK listeners internally. You do NOT need to register listeners for components you're using — they handle their own real-time updates.138139Register your own listeners only when you need to:140- Update custom UI outside UIKit components141- Track events for analytics142- Sync state between screens143144## ChatSDKEventInitializer145146Called automatically by `CometChatUIKit._initiateAfterLogin()`. Bridges SDK events to UIKit's internal event bus. You never need to call this manually.147148## Gotchas149150- Listener IDs MUST be unique per registration. Using the same ID overwrites the previous listener silently — no error thrown, old listener just stops receiving events.151- `onTypingStarted`/`onTypingEnded` only fire if `subscriptionType` was set in `UIKitSettings`. Omitting it silently disables all presence/typing events.152- SDK listeners fire on the main isolate. Heavy processing in callbacks blocks the UI thread. Offload to `compute()` if needed.153- `ccMessageSent` fires three times per message: once with `MessageStatus.inProgress`, once with `MessageStatus.sent` (success), or once with `MessageStatus.error` (failure). Filter by status.154- Connection listener's `onConnected` fires on reconnect — use it to trigger a silent refresh of your data.155- Group listener callbacks include an `Action` object (which is a `BaseMessage` subclass). Don't confuse it with the `Action` widget from Flutter.156157## Anti-Patterns158159```dart160// ❌ WRONG — hardcoded listener ID causes silent overwrite161CometChat.addMessageListener('messages', this);162// Later, another screen does the same:163CometChat.addMessageListener('messages', this); // First listener silently lost!164165// ✅ CORRECT — unique ID per instance166final id = 'screen_${DateTime.now().millisecondsSinceEpoch}';167CometChat.addMessageListener(id, this);168```169170```dart171// ❌ WRONG — registering SDK listeners for a UIKit component you're already using172// ConversationsBloc already handles MessageListener, UserListener, GroupListener internally173CometChat.addMessageListener('redundant', this); // Duplicate handling174175// ✅ CORRECT — only register for custom logic outside UIKit components176// Let ConversationsBloc handle its own listeners177```178179```dart180// ❌ WRONG — not filtering ccMessageSent status181void onCcMessageSent(BaseMessage msg, MessageStatus status) {182 addToList(msg); // Adds 3 times! (inProgress + sent + error)183}184185// ✅ CORRECT — filter by status186void onCcMessageSent(BaseMessage msg, MessageStatus status) {187 if (status == MessageStatus.sent) {188 addToList(msg);189 }190}191```192193## Checklist194195- [ ] Listener IDs are unique (use timestamp or UUID suffix)196- [ ] All listeners registered in `initState()` are removed in `dispose()`197- [ ] `subscriptionType` set in UIKitSettings for presence/typing events198- [ ] `ccMessageSent` handlers filter by `MessageStatus`199- [ ] No redundant listeners for UIKit components that handle their own200- [ ] `Action` from CometChat SDK not confused with Flutter's `Action` widget (use `import 'package:cometchat_chat_uikit/cometchat_chat_uikit.dart' as cc;` and `cc.Action`)