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!

User Management

Learn how to sync your users with CometChat.

When a user logs into your app, you need to programmatically login the user into CometChat. But before you log in the user to CometChat, you need to create the user.

Summing up-

When a user registers in your app

  1. You add the user details in your database
  2. You create a user in CometChat

When a user logs into your app

  1. You log in the user to your app
  2. You log in the user to CometChat (programmatically)

Creating a user

Ideally, user creation should take place at your backend. You can refer our Rest API to learn more about creating a user and use the appropriate code sample based on your backend language.

However, if you wish to create users on the fly, you can use the createUser() method. This method takes a User object and the Auth Key as input parameters and returns the created User object if the request is successful.

let authKey = "AUTH_KEY";
var uid = "user1";
var name = "Kevin";

var user = new CometChat.User(uid);

user.setName(name);

CometChat.createUser(user, authKey).then(
    user => {
        console.log("user created", user);
    },error => {
        console.log("error", error);
    }
)

🚧

UID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed.

Updating a user

Updating a user similar to creating a user should ideally be achieved at your backend using the Restful APIs. For more information, you can check the update a user section. However, this can be achieved on the fly as well as using the updateUser() method. This method takes a User object and the Auth Key as inputs and returns the updated User object on the successful execution of the request.

let authKey = "AUTH_KEY";
let uid = "user1";
let name = "Kevin Fernandez";

var user = new CometChat.User(uid);

user.setName(name);

CometChat.updateUser(user, authKey).then(
    user => {
        console.log("user updated", user);
    }, error => {
        console.log("error", error);
    }
)

Please make sure the User object provided to the updateUser() method has the UID of the user to be updated set.

Deleting a user

Deleting a user can only be achieved via the Restful APIs. For more information please check the delete a user section.

Updated 2 months ago


User Management


Learn how to sync your users with CometChat.

Suggested Edits are limited on API Reference Pages

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