CometChat Pro Documentation

You'll find comprehensive guides and documentation to help you start working with CometChat as quickly as possible. Let's jump right in!

Authentication

Create User

Before you login the user, you must add the user to CometChat.

  1. For proof of concept/MVPs: Create the user using the CometChat Dashboard.
  2. For production apps: Use the CometChat Create User API to create the user when your user signs up in your app.

Sample Users

We have setup 5 users for testing having UIDs: SUPERHERO1, SUPERHERO2, SUPERHERO3, SUPERHERO4 and SUPERHERO5.

Once initialization is successful, you will need to log the user into CometChat using the login() method.

We recommend you call the CometChat login method once your user logs into your app.

Login using API Key

This simple authentication procedure is useful when you are in development or if you do not require additional security.

var UID = "SUPERHERO1";
var apiKey = "API_KEY";

CometChat.login(UID, apiKey).then(
  User => {
    console.log("Login Successful:", { User });
    // User loged in successfully.
  },
  error => {
    console.log("Login failed with exception:", { error });
    // User login failed, check error and take appropriate action.
  }
);
Parameters
Description

UID

The UID of the user that you would like to login

apiKey

CometChat API Key

After the user logs in, their information is returned in the User object on Promise resolved.

Login using Auth Token

This advanced authentication procedure does not use the API Key directly in your client code and thus ensuring that your API Key is not leaked even if the client code is reverse engineered.

  1. Create a user account via the CometChat API when the user signs up in your app.
  2. Create an Auth Token via the CometChat API for the new user and save the token in your database.
  3. Load the Auth Token in your client and pass it to the login() method.
var authToken = "AUTH_TOKEN";

CometChat.login(authToken).then(
  User => {
    console.log("Login successfully:", { User });
    // User loged in successfully.
  },
  error => {
    console.log("Login failed with exception:", { error });
    // User login failed, check error and take appropriate action.
  }
);
Parameter
Description

authToken

Auth Token of the user you would like to login

After the user logs in, their information is returned in the User object on the Promise resolved.

Logout

You can use the logout() method to log out the user from CometChat. We suggest you call this method once your user has been successfully logged out from your app.

CometChat.logout().then({
	//Logout completed successfully
  console.log("Logout completed successfully");
},error=>{
  //Logout failed with exception
  console.log("Logout failed with exception:",{error});
})

What's Next

Messaging

Authentication


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.