Quick Reference - Fetch users:
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 Promise with reject callback, it indicates that the user is not logged in and you need to log the user into CometChat SDK.
- JavaScript
- TypeScript
User object containing all the information related to the logged-in user.
Retrieve List of Users
In order to fetch the list of users, you can use theUsersRequest class. To use this class i.e 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 below parameters:
Set Limit
This method sets the limit i.e. the number of users that should be fetched in a single iteration.- JavaScript
- TypeScript
Set Search Keyword
This method allows you to set the search string based on which the users are to be fetched.- JavaScript
- TypeScript
Search In
This method allows you to define in which user property should the searchKeyword be searched. This method only works in combination withsetSearchKeyword(). By default the keyword is searched in both UID & Name.
- JavaScript
- TypeScript
Set Status
The status based on which the users are to be fetched. The status parameter can contain one of the below two values:- CometChat.USER_STATUS.ONLINE - will return the list of only online users.
- CometChat.USER_STATUS.OFFLINE - will return the list of only offline users.
- JavaScript
- TypeScript
Hide Blocked Users
This method is used to determine if the blocked users should be returned as a part of the user list. If set to true, the user list will not contain the users blocked by the logged in user.- JavaScript
- TypeScript
Set Roles
This method allows you to fetch the users based on multiple roles.- JavaScript
- TypeScript
Friends Only
This property when set to true will return only the friends of the logged-in user.- JavaScript
- TypeScript
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.- JavaScript
- TypeScript
With Tags
This property when set to true will fetch tags data along with the list of users.- JavaScript
- TypeScript
Set UIDs
This method accepts a list of UIDs based on which the list of users is fetched. A maximum of25 users can be fetched.
- JavaScript
- TypeScript
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 pass to the sortBy() method the user list will be sorted by name => UID.
- JavaScript
- TypeScript
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.- JavaScript
- TypeScript
- JavaScript
- TypeScript
Retrieve Particular User Details
To get the information of a user, you can use thegetUser() method.
- JavaScript
- TypeScript
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.
Get online user count
To get the total count of online users for your app, you can use thegetOnlineUserCount() method.
- JavaScript
- TypeScript
Best Practices
Use pagination for large user lists
Use pagination for large user lists
Always use
fetchNext() with a reasonable setLimit() value (e.g., 20-30) rather than fetching all users at once. This improves performance and reduces memory usage.Reuse the same UsersRequest instance for pagination
Reuse the same UsersRequest instance for pagination
The
UsersRequest object maintains an internal cursor. Creating a new instance resets the cursor, causing the same page to be fetched repeatedly. Reuse the same instance across fetchNext() calls.Filter server-side, not client-side
Filter server-side, not client-side
Use the builder’s filter methods (
setSearchKeyword, setStatus, setRoles, setTags) to narrow results at the API level rather than fetching all users and filtering in your app.Troubleshooting
fetchNext returns empty list
fetchNext returns empty list
Verify the logged-in user session is active. Also check if filters like
setRoles, setTags, or setStatus are too restrictive. Try removing filters to confirm users exist.getUser returns error
getUser returns error
Ensure the UID exists in your CometChat app. UIDs are case-sensitive — double-check the exact UID string.
Blocked users still appearing in list
Blocked users still appearing in list
By default, blocked users are included in the user list. Use
hideBlockedUsers(true) in the UsersRequestBuilder to exclude them.