Skip to main content
Quick Reference for AI Agents & Developers
// Add login listener
CometChat.addLoginListener("LISTENER_ID", object : LoginListener() {
    override fun loginSuccess(user: User) { /* Login successful */ }
    override fun loginFailure(e: CometChatException) { /* Login failed */ }
    override fun logoutSuccess() { /* Logout successful */ }
    override fun logoutFailure(e: CometChatException) { /* Logout failed */ }
})

// Remove listener
CometChat.removeLoginListener("LISTENER_ID")
The CometChat SDK provides you with real-time updates for the login and logout events. This can be achieved using the LoginListener class provided. LoginListener consists of 4 events that can be triggered. These are as follows:
Callback MethodInformation
loginSuccess(User user)Informs you that the login was successful and provides you with a user object containing the data for the user that logged in
loginFailure(CometChatException e)Informs you about the failure while logging in the user and provides you with the reason for the failure wrapped in an object of the CometChatException class
logoutSuccess()Informs you about the user being logged out successfully.
logoutFailure(CometChatException e)Informs you about the failure while logging out the user. The reason for the failure can be obtained from the object of the CometChatException class provided.
To add the LoginListener, you need to use the addLoginListener() method provided by the SDK which takes a unique identifier for the listener and of the the LoginListener class itself.
CometChat.addLoginListener("UNIQUE_ID", new CometChat.LoginListener() {
  @Override
  public void loginSuccess(User user) {
      Log.d("LoginListener", "loginSuccess " + user.toString());
  }

  @Override
  public void loginFailure(CometChatException e) {
      Log.d("LoginListener", "loginFailure " + e.getMessage());
  }

  @Override
  public void logoutSuccess() {
      Log.d("LoginListener", "logoutSuccess ");
  }

  @Override
  public void logoutFailure(CometChatException e) {
      Log.d("LoginListener", "logoutFailure ");
  }
});
In order to stop receiving events related to login and logout you need to use the removeLoginListener() method provided by the SDK and pas the ID of the listener that needs to be removed.
CometChat.removeLoginListener("UNIQUE_ID");  
Always remove login listeners when they’re no longer needed (e.g., in onDestroy() or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling.

Best Practices

Use loginSuccess() to initialize user-specific data (fetch conversations, register message listeners) and logoutSuccess() to clean up resources and navigate to the login screen.
In loginFailure(), parse the CometChatException to show user-friendly error messages (e.g., “Invalid credentials”, “Network error”). Provide retry options for transient failures.
Register login listeners before calling CometChat.login() to ensure you don’t miss any events. Register in onCreate() or before authentication attempts.
Remove all listeners (message, user, group, call) in logoutSuccess() to prevent receiving events after logout. This ensures a clean session termination.

Next Steps