Learn how to register and manage real-time event listeners for messages, users, groups, calls, connections, login, and AI in the CometChat Flutter SDK.
CometChat provides real-time event listeners to keep your app updated with live changes. These listeners notify your app when messages are received, users come online/offline, group membership changes occur, calls are initiated, and connection state changes.
Listener Cleanup Required: Always remove listeners when they’re no longer needed (e.g., on widget dispose or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling.
where UNIQUE_LISTENER_ID is the unique identifier for the listener. Please make sure that no two listeners are added with the same listener id as this could lead to unexpected behavior resulting in loss of events.Once the activity/fragment where the MessageListener is declared is not in use, you need to remove the listener using the removeMessageListener() method which takes the id of the listener to be removed as the parameter. We suggest you call this method in the onPause() method of the activity/fragment.
Triggered when a user comes online and is available to chat. The details of the user can be obtained from the User object received as the method parameter.
Triggered when a user goes offline. The details of the user can be obtained from the User object received as the parameter.
To add the UserListener, you need to use the addUserListener() method provided by the CometChat class.
Parameter
Type
Description
listenerId
String
A unique identifier for the listener. No two listeners should share the same ID.
listener
UserListener
An instance of a class that implements the UserListener mixin
Dart
class Class_Name with UserListener { //CometChat.addUserListener("user_Listener_id", this); @override void onUserOnline(User user) { } @override void onUserOffline(User user) { }}
where UNIQUE_LISTENER_ID is the unique identifier for the listener. Please make sure that no two listeners are added with the same listener id as this could lead to unexpected behavior resulting in loss of events.Once the activity/fragment where the UserListener is declared is not in use, you need to remove the listener using the removeUserListener() method which takes the id of the listener to be removed as the parameter. We suggest you call this method in the onPause() method of the activity/fragment.
The GroupListener class provides you with all the real-time events related to groups. Below are the callback methods provided by the GroupListener class.
Triggered when a user is added to any group. All the members including the user himself receive this event.
To add the GroupListener, you need to use the addGroupListener() method provided by the CometChat class.
Parameter
Type
Description
listenerId
String
A unique identifier for the listener. No two listeners should share the same ID.
listener
GroupListener
An instance of a class that implements the GroupListener mixin
Dart
class Class_Name with GroupListener { //CometChat.addGroupListener("UNIQUE_LISTENER_ID", this); @override void onGroupMemberJoined(Action action, User joinedUser, Group joinedGroup) { } @override void onGroupMemberLeft(Action action, User leftUser, Group leftGroup) { } @override void onGroupMemberKicked(Action action, User kickedUser, User kickedBy, Group kickedFrom) { } @override void onGroupMemberBanned(Action action, User bannedUser, User bannedBy, Group bannedFrom) { } @override void onGroupMemberUnbanned(Action action, User unbannedUser, User unbannedBy, Group unbannedFrom) { } @override void onGroupMemberScopeChanged(Action action, User updatedBy, User updatedUser, String scopeChangedTo, String scopeChangedFrom, Group group) { } @override void onMemberAddedToGroup(Action action, User addedby, User userAdded, Group addedTo) { }}
where UNIQUE_LISTENER_ID is the unique identifier for the listener. Please make sure that no two listeners are added with the same listener id as this could lead to unexpected behavior resulting in loss of events.Once the activity/fragment where the GroupListener is declared is not in use, you need to remove the listener using the removeGroupListener() method which takes the id of the listener to be removed as the parameter. We suggest you call this method in the onPause() method of the activity/fragment.
where UNIQUE_LISTENER_ID is the unique identifier for the listener. Please make sure that no two listeners are added with the same listener id as this could lead to unexpected behavior resulting in loss of events.Once the CallListener is no longer needed, remove it using the removeCallListener() method.
The ConnectionListener class provides you with real-time events related to the WebSocket connection status. Below are the callback methods provided by the ConnectionListener class.
where UNIQUE_LISTENER_ID is the unique identifier for the listener. Please make sure that no two listeners are added with the same listener id as this could lead to unexpected behavior resulting in loss of events.Once the ConnectionListener is no longer needed, remove it using the removeConnectionListener() method.
The LoginListener class provides you with real-time events related to user authentication. Below are the callback methods provided by the LoginListener class.
where UNIQUE_LISTENER_ID is the unique identifier for the listener. Please make sure that no two listeners are added with the same listener id as this could lead to unexpected behavior resulting in loss of events.Once the LoginListener is no longer needed, remove it using the removeLoginListener() method.
The AIAssistantListener class provides you with real-time events related to AI assistant interactions. Below are the callback methods provided by the AIAssistantListener class.
To add the AIAssistantListener, you need to use the addAIAssistantListener() method provided by the CometChat class.
Parameter
Type
Description
listenerId
String
A unique identifier for the listener. No two listeners should share the same ID.
listener
AIAssistantListener
An instance of a class that implements the AIAssistantListener mixin
Dart
class Class_Name with AIAssistantListener { //CometChat.addAIAssistantListener("UNIQUE_LISTENER_ID", this); @override void onAIAssistantEventReceived(AIAssistantBaseEvent aiAssistantBaseEvent) { }}
where UNIQUE_LISTENER_ID is the unique identifier for the listener. Please make sure that no two listeners are added with the same listener id as this could lead to unexpected behavior resulting in loss of events.Once the AIAssistantListener is no longer needed, remove it using the removeAIAssistantListener() method.