Login Listener

Helps you to know the login status of CometChat

CometChat SDK provides you with a mechanism to get real-time status whenever a user logs into CometChat or logs out from CometChat.

Login Listener provides you with the below 4 methods:

Delegate Method

Information

loginSuccess(event)

This method is triggered when the user successfully logs into the CometChat SDK. It returns an Object of the User Class.

loginFailure(event)

This method is triggered when the user could not successfully log into the CometChat SDK. It returns an Object of the CometChatException Class.

logoutSuccess()

This method is called when the user successfully logs out from the CometChat SDK. It does not return anything.

logoutFailure(event)

This method is triggered when the user could not successfully log into the CometChat SDK. It returns an Object of the CometChatException Class.

To receive real-time login status, you need to register LoginListener wherever you wish to receive the real-time status. You can use the addLoginListener() method to do so.

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);
        }
    })
);

📘

We recommend you to add the Login Listener in your method on app startup, preferably in the index.js file. Once you have successfully initialized CometChat.

In order to stop receiving events related to login and logout you need to use the removeLoginListener() method provided by the SDK and pass the ID of the listener that needs to be removed.

var listenerID = "UNIQUE_LISTENER_ID";
CometChat.removeLoginListener(listenerID);

Did this page help you?