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.

// Get logged-in user
val user = CometChat.getLoggedInUser()

// Fetch user list with filters
val usersRequest = UsersRequestBuilder()
    .setLimit(30)
    .setSearchKeyword("john")
    .hideBlockedUsers(true)
    .build()

usersRequest.fetchNext(object : CallbackListener<List<User?>>() {
    override fun onSuccess(list: List<User?>) { }
    override fun onError(e: CometChatException) { }
})

// Get specific user details
CometChat.getUser("UID", object : CometChat.CallbackListener<User>() {
    override fun onSuccess(user: User) { }
    override fun onError(e: CometChatException) { }
})

Retrieve Logged In User Details

Use getLoggedInUser() to get the details of the logged-in user. Returns null if no user is logged in.
User user = CometChat.getLoggedInUser();
This method will return a User object containing all the information related to the logged-in user.

Retrieve List of Users

Use UsersRequestBuilder to configure filters, then call fetchNext() to retrieve users.

Set Limit

Set the number of users to fetch per request.
UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
.setLimit(30)
.build();

Set Search Keyword

Filter users by a search string.
UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
.setLimit(limit)
.setSearchKeyword("abc")
.build();

Search In

Define which user property the searchKeyword should be searched in. Works only with setSearchKeyword(). By default, searches both UID and Name.
List<String> searchInList = new ArrayList<>();
searchInList.add("uid");

UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
.setLimit(20)
.setSearchKeyword("super")
.searchIn(searchInList)
.build();

Set Status

The status based on which the users are to be fetched. The status parameter can contain one of the following values:
  • CometChat.USER_STATUS.ONLINE - Returns the list of only online users.
  • CometChat.USER_STATUS.OFFLINE - Returns the list of only offline users.
If this parameter is not set, all available users will be returned.
UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
.setLimit(limit)
.setUserStatus(UsersRequest.USER_STATUS_ONLINE)
.build();  
If this parameter is not set, all users will be returned.

Hide Blocked Users

Exclude users blocked by the logged-in user from the results. Default is false.
UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
.setLimit(limit)
.hideBlockedUsers(true)
.build();   

Set Roles

Filter users by one or more roles.
List<String> roles = new ArrayList<>();
roles.add("role1");
roles.add("role2");
UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
.setLimit(limit)
.setRoles(roles)
.build();

Friends Only

Return only friends of the logged-in user when set to true.
UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
.setLimit(limit)
.friendsOnly(true)
.build();

Set Tags

Filter users by tags. Only users tagged with the specified tags are returned.
List<String> tags = new ArrayList<>();
tags.add("tag1");
tags.add("tag2");
UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
.setLimit(limit)
.setTags(tags)
.build();

With Tags

Include tag data in the response when set to true.
UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
.setLimit(limit)
.withTags(true)
.build();

Set UIDs

Fetch specific users by their UIDs. Maximum 25 UIDs per request.
List<String> uids = new ArrayList<>();
uids.add("cometchat-uid-1");
uids.add("cometchat-uid-2");
UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
.setLimit(limit)
.setUIDs(uids)
.build();

Sort By

Sort the user list by a specific property. Default sort order is status => name => UID.
UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
.setLimit(20)
.sortBy(CometChatConstants.SORT_BY_NAME)
.build();

Sort By Order

Sort the user list in ascending or descending order. Default is ascending.
UsersRequest usersRequest = new UsersRequest.UsersRequestBuilder()
.setLimit(20)
.sortByOrder(CometChatConstants.SORT_ORDER_DESCENDING)
.build();

Fetch Users

After configuring the builder, call build() then fetchNext() to retrieve users. Call fetchNext() repeatedly on the same object to paginate.
private UsersRequest usersRequest = null;
private int limit = 30;

usersRequest = new UsersRequest.UsersRequestBuilder().setLimit(limit).build();

usersRequest.fetchNext(new CometChat.CallbackListener<List<User>>() {
@Override
public void onSuccess(List <User> list) {
  Log.d(TAG, "User list received: " + list.size());
}
@Override
public void onError(CometChatException e) {
  Log.d(TAG, "User list fetching failed with exception: " + e.getMessage());
}
});

Retrieve Particular User Details

Use getUser() to fetch details for a specific user by UID.
private String UID = "UID";

CometChat.getUser(UID, new CometChat.CallbackListener<User>() {
@Override
public void onSuccess(User user) {
  Log.d(TAG, "User details fetched for user: " + user.toString());
}
@Override
public void onError(CometChatException e) {
  Log.d(TAG, "User details fetching failed with exception: " + e.getMessage());
}
});
The getUser() method takes the following parameters:
ParameterDescription
UIDThe UID of the user for whom the details are to be fetched
On success, the User object containing the details of the user is returned.

Get Online User Count

Use getOnlineUserCount() to get the total count of online users for your app.
CometChat.getOnlineUserCount(new CometChat.CallbackListener<Integer>() {
@Override
public void onSuccess(Integer count) {
  Log.d(TAG, "Online users : " + count);
}

@Override
public void onError(CometChatException e) {
  Log.d(TAG, "Error : " + e.getMessage());
}
});

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

User Presence

Track and subscribe to user online/offline status updates

Block Users

Block and unblock users to control interactions

User Management

Create, update, and manage user accounts

Send Messages

Start sending messages to retrieved users