Skip to main content
Quick Reference - Register a login listener:
CometChat.addLoginListener(
  "UNIQUE_LISTENER_ID",
  new CometChat.LoginListener({
    loginSuccess: (e) => console.log("Logged in", e),
    loginFailure: (e) => console.log("Login failed", e),
    logoutSuccess: () => console.log("Logged out"),
    logoutFailure: (e) => console.log("Logout failed", e),
  })
);
The CometChat SDK provides you with real-time updates for the login and logout events. This can be achieved using the LoginListener class. LoginListener provides the following 4 methods:
Delegate MethodInformation
loginSuccess(event)Informs you that the login was successful and provides you with a user object containing the data for the user that logged in.
loginFailure(event)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(event)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.

Add Login Listener

To add the LoginListener, use the addLoginListener() method provided by the SDK. It takes a unique identifier for the listener and an instance of the LoginListener class.
var listenerID = "UNIQUE_LISTENER_ID";

CometChat.addLoginListener(
  listenerID,
  new CometChat.LoginListener({
    loginSuccess: (e) => {
      console.log("LoginListener :: loginSuccess", e);
    },
    loginFailure: (e) => {
      console.log("LoginListener :: loginFailure", e);
    },
    logoutSuccess: () => {
      console.log("LoginListener :: logoutSuccess");
    },
    logoutFailure: (e) => {
      console.log("LoginListener :: logoutFailure", e);
    },
  })
);

Remove Login Listener

To stop receiving events related to login and logout, use the removeLoginListener() method and pass the ID of the listener that needs to be removed.
var listenerID = "UNIQUE_LISTENER_ID";
CometChat.removeLoginListener(listenerID);
Listener Cleanup: Always remove your login listeners when they are no longer needed (e.g., when a component unmounts) to prevent memory leaks and unexpected behavior. Use removeLoginListener() with the same listener ID you used when adding it.
  • Use unique, descriptive listener IDs to avoid conflicts (e.g., "login-screen-listener")
  • Add listeners in component mount lifecycle and remove them on unmount
  • Handle all four events to provide a complete user experience
  • Use loginFailure to display user-friendly error messages
  • Listener not firing: Ensure the listener was added before the login/logout event occurs
  • Duplicate events: Check that you’re not adding the same listener multiple times without removing it first
  • Memory leaks: Verify that removeLoginListener() is called when the component unmounts

Next Steps