Skip to main content
Quick Reference - Create and update users:
Available via: SDK | REST API
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 to 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.
The Auth Key is intended for development and testing only. In production, create users from your backend using the REST API with your API Key instead.
On SuccesscreateUser() returns the created User object:When creating a user with all optional fields set:
UID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed. CometChat automatically converts any uppercase characters in the UID to lowercase.

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.
On SuccessupdateUser() returns the updated User object:When updating a user with all optional fields:
Please make sure the User object provided to the updateUser() method has the UID of the user to be updated set.

Updating logged-in user

Updating a logged-in user is similar to updating a user. The only difference being this method does not require an AuthKey. This method takes a User object as input and returns the updated User object on the successful execution of the request.
On SuccessupdateCurrentUserDetails() returns the updated User object with additional session data:
By using the updateCurrentUserDetails() method one can only update the logged-in user irrespective of the UID passed. Also, it is not possible to update the role of a logged-in user.

Deleting a user

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

User Class

Best Practices

Use the REST API with your API Key to create users server-side. The createUser() SDK method requires the Auth Key, which should not be exposed in production client apps.
Use the same user identifier from your backend as the CometChat UID. This simplifies mapping between your user system and CometChat.
When the logged-in user updates their own profile, use updateCurrentUserDetails() which doesn’t require an Auth Key. Reserve updateUser() for admin-level operations from your backend.

Troubleshooting

Each UID must be unique across your CometChat app. If the user already exists, use updateUser() instead, or skip creation and proceed to login.
Role updates are not allowed via updateCurrentUserDetails(). Use the REST API or updateUser() with Auth Key to change a user’s role.
UIDs can only contain alphanumeric characters, underscores, and hyphens. Spaces, punctuation, and other special characters are not allowed.

Next Steps

Retrieve Users

Fetch user lists, search users, and get user details

Block Users

Block and unblock users, retrieve blocked user lists

Authentication

Log users into CometChat after creation

User Presence

Track online/offline status of users in real time