Skip to main content
// Create a user
const user = new CometChat.User("user1");
user.setName("Kevin");
await CometChat.createUser(user, "AUTH_KEY");

// Update a user
user.setName("Kevin Fernandez");
await CometChat.updateUser(user, "AUTH_KEY");

// Update logged-in user (no auth key needed)
await CometChat.updateCurrentUserDetails(user);
Note: User creation/deletion should ideally happen on your backend via the REST API.
Users must exist in CometChat before they can log in. Typically:
  1. User registers in your app → Create user in CometChat
  2. User logs into your app → Log user into CometChat

Creating a User

User creation should ideally happen on your backend via the REST API.
Security: Never expose your Auth Key in client-side production code. User creation and updates using Auth Key should ideally happen on your backend server. Use client-side creation only for prototyping or development.
For client-side creation (development only), use createUser():
let authKey = "AUTH_KEY";
let uid = "user1";
let name = "Kevin";

let user = new CometChat.User(uid);

user.setName(name);

CometChat.createUser(user, authKey).then(
  user => {
    console.log("user created", user);
  }, error => {
    console.log("error", error);
  }
)
The method returns a User object. Access the response data using getter methods:
FieldGetterReturn TypeDescription
uidgetUid()stringUnique user ID
namegetName()stringDisplay name of the user
avatargetAvatar()stringURL of the user’s avatar image
rolegetRole()stringRole assigned to the user
statusgetStatus()stringOnline status of the user
UID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed.

Updating a User

Like creation, user updates should ideally happen on your backend via the REST API. For client-side updates (development only), use updateUser():
let authKey = "AUTH_KEY";
let uid = "user1";
let name = "Kevin Fernandez";

let user = new CometChat.User(uid);

user.setName(name);

CometChat.updateUser(user, authKey).then(
  user => {
    console.log("user updated", user);
  }, error => {
    console.log("error", error);
  }
)
Ensure the User object has the correct UID set. The method returns a User object. Access the response data using getter methods:
FieldGetterReturn TypeDescription
uidgetUid()stringUnique user ID
namegetName()stringDisplay name of the user
avatargetAvatar()stringURL of the user’s avatar image
rolegetRole()stringRole assigned to the user
metadatagetMetadata()ObjectCustom metadata attached to the user

Updating Logged-in User

Use updateCurrentUserDetails() to update the current user without an Auth Key. Note: You cannot update the user’s role with this method.
let uid = "user1";
let name = "Kevin Fernandez";

let user = new CometChat.User(uid);

user.setName(name);

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

Deleting a User

User deletion is only available via the REST API.

User Class

FieldEditableInformation
uidspecified on user creation. Not editable after thatUnique identifier of the user
nameYesDisplay name of the user
avatarYesURL to profile picture of the user
linkYesURL to profile page
roleYesUser role of the user for role based access control
metadataYesAdditional information about the user as JSON
statusNoStatus of the user. Could be either online/offline
statusMessageYesAny custom status message that needs to be set for a user
lastActiveAtNoThe unix timestamp of the time the user was last active.
hasBlockedMeNoA boolean that determines if the user has blocked the logged in user
blockedByMeNoA boolean that determines if the logged in user has blocked the user
tagsYesA list of tags to identify specific users

Next Steps

Retrieve Users

Fetch and filter user lists with pagination.

User Presence

Monitor real-time online/offline status.

Block Users

Block and unblock users.

Authentication

Log users into CometChat.