Skip to main content

Documentation Index

Fetch the complete documentation index at: https://cometchat-22654f5b-docs-android-v6-beta2.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

// Create user (use API Key - dev/testing only)
val apiKey = "API_KEY"
val user = User()
user.uid = "user1"
user.name = "Kevin"

CometChat.createUser(user, apiKey, object : CometChat.CallbackListener<User>() {
    override fun onSuccess(user: User) { }
    override fun onError(e: CometChatException) { }
})

// Update logged-in user (no API Key needed)
val updatedUser = User()
updatedUser.name = "Andrew Joseph"

CometChat.updateCurrentUserDetails(updatedUser, object : CallbackListener<User>() {
    override fun onSuccess(user: User) { }
    override fun onError(e: CometChatException) { }
})
Note: User creation/updates should ideally happen on your backend using REST API.
When a user registers in your app, create them in CometChat. When they log in, log them into CometChat as well.

Creating a user

Ideally, user creation should happen on your backend using the REST API. For on-the-fly creation during development, use the createUser() method with a User object and your API Key.
String apiKey = "AUTH_KEY"; // Replace with your API Key.
User user = new User();
user.setUid("user1"); // Replace with your uid for the user to be created.
user.setName("Kevin"); // Replace with the name of the user

CometChat.createUser(user, apiKey, new CometChat.CallbackListener<User>() {
@Override
public void onSuccess(User user) {
  Log.d("createUser", user.toString());
}

@Override
public void onError(CometChatException e) {
  Log.e("createUser", e.getMessage());
}
});
API Key Security: The API Key should only be used for development and testing. In production, create and update users on your backend server using the REST API to keep your API Key secure. Never expose API Keys in production client code.
UID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed.

Updating a user

Use updateUser() with a User object and API Key. Ideally done on your backend via the REST API.
String apiKey = "AUTH_KEY"; // Replace with your API Key.
User user = new User();
user.setUid("user1"); // Replace with your uid for the user to be updated.
user.setName("Kevin Fernandez"); // Replace with the name of the user

CometChat.updateUser(user, apiKey, new CometChat.CallbackListener<User>() {
@Override
public void onSuccess(User user) {
  Log.d("updateUser", user.toString());
}

@Override
public void onError(CometChatException e) {
  Log.e("updateUser", e.getMessage());
}
});
Please make sure the User object provided to the updateUser() method has the UID of the user to be updated.

Updating logged-in user

Use updateCurrentUserDetails() to update the currently logged-in user’s profile. No API Key required.
User user = new User();
user.setName("Andrew Joseph");

CometChat.updateCurrentUserDetails(user, new CometChat.CallbackListener<User>() {
@Override
public void onSuccess(User user) {
  Log.d(TAG, user.toString());
}

@Override
public void onError(CometChatException e) {
  Log.d(TAG, e.getMessage());
}
});
By using the updateCurrentUserDetails() method, you can only update the logged-in user regardless 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, see the delete a user section.

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

User Payload Structure

The User object returned by SDK methods contains the following fields:
ParameterTypeDescription
uidStringUnique identifier of the user
nameStringDisplay name of the user
avatarStringURL to user’s profile picture
linkStringURL to user’s profile page
roleStringUser role for role-based access control
metadataJSONObjectCustom data set by developer. Can contain any key-value pairs
statusStringUser online status. Values: "online", "offline"
statusMessageStringCustom status message set by user
lastActiveAtlongUnix timestamp of last activity (milliseconds)
hasBlockedMebooleanWhether this user has blocked the logged-in user
blockedByMebooleanWhether the logged-in user has blocked this user
tagsArray<String>List of tags for user identification and filtering
deactivatedAtlongUnix timestamp when user was deactivated (0 if active)
Sample User Object:
{
  "uid": "user_123",
  "name": "John Doe",
  "avatar": "https://example.com/avatar.png",
  "link": "https://example.com/profile/user_123",
  "role": "default",
  "metadata": {
    "customKey": "customValue",
    "preferences": {
      "theme": "dark",
      "notifications": true
    }
  },
  "status": "online",
  "statusMessage": "Available",
  "lastActiveAt": 1699900000000,
  "hasBlockedMe": false,
  "blockedByMe": false,
  "tags": ["premium", "verified"],
  "deactivatedAt": 0
}

Next Steps

Authentication

Log users into CometChat after creating their accounts

Retrieve Users

Fetch user lists and search for specific users

User Presence

Track user online/offline status in real-time

Block Users

Implement user blocking and unblocking features