Quick Reference for AI Agents & Developers
Retrieve Logged In User Details
You can get the details of the logged-in user using thegetLoggedInUser() method. This method can also be used to check if the user is logged in or not. If the method returns null, it indicates that the user is not logged in and you need to log the user into CometChat.
- Java
- Kotlin
User object containing all the information related to the logged-in user.
Retrieve List of Users
To fetch the list of users, you can use theUsersRequest class. To create an object of the UsersRequest class, you need to use the UsersRequestBuilder class. The UsersRequestBuilder class allows you to set the parameters based on which the users are to be fetched.
The UsersRequestBuilder class allows you to set the following parameters:
Set Limit
This method sets the limit i.e. the number of users that should be fetched in a single iteration.- Java
- Kotlin
Set Search Keyword
This method allows you to set the search string based on which the users are to be fetched.- Java
- Kotlin
Search In
This method allows you to define in which user property thesearchKeyword should be searched. This method only works in combination with setSearchKeyword(). By default, the keyword is searched in both UID and Name.
- Java
- Kotlin
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.
- Java
- Kotlin
Hide Blocked Users
This method determines if blocked users should be returned as part of the user list. If set totrue, the user list will not contain users blocked by the logged-in user.
- Java
- Kotlin
Set Roles
This method allows you to fetch the users based on multiple roles.- Java
- Kotlin
Friends Only
This property when set to true will return only the friends of the logged-in user.- Java
- Kotlin
Set Tags
This method accepts a list of tags based on which the list of users is to be fetched. The list fetched will only contain the users that have been tagged with the specified tags.- Java
- Kotlin
With Tags
This property when set to true will fetch tags data along with the list of users.- Java
- Kotlin
Set UIDs
This method accepts a list of UIDs based on which the list of users is fetched. A maximum of 25 users can be fetched.- Java
- Kotlin
Sort By
This method allows you to sort the User List by a specific property of User. By default, the User List is sorted bystatus => name => UID. If name is passed to the sortBy() method, the user list will be sorted by name => UID.
- Java
- Kotlin
Sort By Order
This method allows you to sort the User List in a specific order. By default the user list is sorted in ascending order.- Java
- Kotlin
build() method to get the object of the UsersRequest class.
Once you have the object of the UsersRequest class, you need to call the fetchNext() method. Calling this method will return a list of User objects containing X number of users depending on the limit set.
- Java
- Kotlin
Retrieve Particular User Details
To get the information of a user, you can use thegetUser() method.
- Java
- Kotlin
getUser() method takes the following parameters:
| Parameter | Description |
|---|---|
UID | The UID of the user for whom the details are to be fetched |
User object containing the details of the user is returned.
Get online user count
To get the total count of online users for your app, you can use thegetOnlineUserCount() method.
- Java
- Kotlin
Best Practices
Use Pagination for Large User Lists
Use Pagination for Large User Lists
Always set a reasonable limit (20-50 users) and use
fetchNext() to paginate through results. This prevents memory issues and improves performance when dealing with large user bases.Filter Users at Query Time
Filter Users at Query Time
Use
UsersRequestBuilder filters (search, status, roles, tags) to fetch only the users you need rather than fetching all users and filtering client-side. This reduces bandwidth and improves response times.Cache User Details Locally
Cache User Details Locally
Store frequently accessed user details locally to reduce API calls. Use
getLoggedInUser() to check login status without making network requests.Combine Multiple Filters
Combine Multiple Filters
You can chain multiple filters like
setSearchKeyword(), hideBlockedUsers(), and setUserStatus() to create precise queries that return exactly the users you need.