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
Response
Response
On Success —
getLoggedinUser() returns a User object containing the logged-in user’s details:User Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-7" |
name | string | Display name of the user | "Henry Marino" |
avatar | string | URL to user’s avatar image | "https://artriva.com/media/k2/galleries/20/d.jpg" |
authToken | string | Authentication token for the user | "cometchat-uid-7_177199269018c2c2995f0b69b3844abc9fdb9843" |
status | string | User’s online status | "online" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771853565 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
tags | array | Tags associated with the user | ["vip"] |
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
Response
Response
On Success —
fetchNext() with basic limit returns an array of User objects:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-6" |
name | string | Display name of the user | "Ronald Jerry" |
avatar | string | URL to user’s avatar image | "https://media.istockphoto.com/id/1682296067/photo/happy-studio-portrait-or-professional-man-real-estate-agent-or-asian-businessman-smile-for.jpg?s=612x612&w=0&k=20&c=9zbG2-9fl741fbTWw5fNgcEEe4ll-JegrGlQQ6m54rg=" |
status | string | User’s online status | "online" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1772163872 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-6_user_cometchat-uid-7" |
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
Response
Response
On Success —
fetchNext() with search filter returns an array of User objects matching the search criteria:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "123456" |
name | string | Display name of the user | "Farhan Ahmed" |
avatar | string | URL to user’s avatar image | "https://st2.depositphotos.com/38197074/46684/v/450/depositphotos_466848082-stock-illustration-initial-letter-vector-logo.jpg" |
status | string | User’s online status | "offline" |
role | string | User’s role | "extrarole" |
lastActiveAt | number | Unix timestamp of last activity | 1768988601 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | true |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
metadata | object | Custom metadata attached to the user | {"metadata": "something"} |
blockedByMeAt | number | Timestamp when blocked by current user | 1772164515 |
blockedAt | number | Timestamp of block action | 1772164515 |
conversationId | string | Conversation ID between this user and logged-in user | "123456_user_cometchat-uid-7" |
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
Response
Response
On Success —
fetchNext() with status filter returns an array of User objects with matching status:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-6" |
name | string | Display name of the user | "Ronald Jerry" |
avatar | string | URL to user’s avatar image | "https://media.istockphoto.com/id/1682296067/photo/happy-studio-portrait-or-professional-man-real-estate-agent-or-asian-businessman-smile-for.jpg?s=612x612&w=0&k=20&c=9zbG2-9fl741fbTWw5fNgcEEe4ll-JegrGlQQ6m54rg=" |
status | string | User’s online status | "online" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1772163872 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-6_user_cometchat-uid-7" |
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
Response
Response
On Success —
fetchNext() with hideBlockedUsers(true) returns an array of User objects excluding blocked users:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-6" |
name | string | Display name of the user | "Ronald Jerry" |
avatar | string | URL to user’s avatar image | "https://media.istockphoto.com/id/1682296067/photo/happy-studio-portrait-or-professional-man-real-estate-agent-or-asian-businessman-smile-for.jpg?s=612x612&w=0&k=20&c=9zbG2-9fl741fbTWw5fNgcEEe4ll-JegrGlQQ6m54rg=" |
status | string | User’s online status | "online" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1772163872 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-6_user_cometchat-uid-7" |
Set Roles
This method allows you to fetch the users based on multiple roles.- JavaScript
- TypeScript
Response
Response
On Success —
fetchNext() with roles filter returns an array of User objects with matching roles:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-6" |
name | string | Display name of the user | "Ronald Jerry" |
avatar | string | URL to user’s avatar image | "https://media.istockphoto.com/id/1682296067/photo/happy-studio-portrait-or-professional-man-real-estate-agent-or-asian-businessman-smile-for.jpg?s=612x612&w=0&k=20&c=9zbG2-9fl741fbTWw5fNgcEEe4ll-JegrGlQQ6m54rg=" |
status | string | User’s online status | "online" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1772163872 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-6_user_cometchat-uid-7" |
Friends Only
This property when set to true will return only the friends of the logged-in user.- JavaScript
- TypeScript
Response
Response
On Success —
fetchNext() with friendsOnly(true) returns an array of User objects who are friends:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-6" |
name | string | Display name of the user | "Ronald Jerry" |
avatar | string | URL to user’s avatar image | "https://media.istockphoto.com/id/1682296067/photo/happy-studio-portrait-or-professional-man-real-estate-agent-or-asian-businessman-smile-for.jpg?s=612x612&w=0&k=20&c=9zbG2-9fl741fbTWw5fNgcEEe4ll-JegrGlQQ6m54rg=" |
status | string | User’s online status | "online" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1772163872 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-6_user_cometchat-uid-7" |
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
Response
Response
On Success —
fetchNext() with tags filter returns an array of User objects with matching tags:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "user509" |
name | string | Display name of the user | "again stokes" |
status | string | User’s online status | "offline" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1759921301 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-7_user_user509" |
With Tags
This property when set to true will fetch tags data along with the list of users.- JavaScript
- TypeScript
Response
Response
On Success —
fetchNext() with withTags(true) returns an array of User objects including the tags field:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-6" |
name | string | Display name of the user | "Ronald Jerry" |
avatar | string | URL to user’s avatar image | "https://media.istockphoto.com/id/1682296067/photo/happy-studio-portrait-or-professional-man-real-estate-agent-or-asian-businessman-smile-for.jpg?s=612x612&w=0&k=20&c=9zbG2-9fl741fbTWw5fNgcEEe4ll-JegrGlQQ6m54rg=" |
status | string | User’s online status | "online" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1772163872 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
tags | array | Tags associated with the user | ["vip"] |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-6_user_cometchat-uid-7" |
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
Response
Response
On Success —
fetchNext() with UIDs filter returns an array of User objects for the specified UIDs:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-5" |
name | string | Display name of the user | "John Paul" |
avatar | string | URL to user’s avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-5.webp" |
status | string | User’s online status | "offline" |
role | string | User’s role | "admin" |
lastActiveAt | number | Unix timestamp of last activity | 1772087140 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-5_user_cometchat-uid-7" |
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
Response
Response
On Success —
fetchNext() with sortBy("name") returns an array of User objects sorted by name:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "1" |
name | string | Display name of the user | "1" |
status | string | User’s online status | "offline" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1770017972 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
conversationId | string | Conversation ID between this user and logged-in user | "1_user_cometchat-uid-7" |
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
Response
Response
On Success —
fetchNext() with sortByOrder("desc") returns an array of User objects in descending order:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "voip3" |
name | string | Display name of the user | "voip3" |
status | string | User’s online status | "offline" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1770193733 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-7_user_voip3" |
- JavaScript
- TypeScript
Response
Response
On Success —
fetchNext() returns an array of User objects:User Object (per item in array):| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-6" |
name | string | Display name of the user | "Ronald Jerry" |
avatar | string | URL to user’s avatar image | "https://media.istockphoto.com/id/1682296067/photo/happy-studio-portrait-or-professional-man-real-estate-agent-or-asian-businessman-smile-for.jpg?s=612x612&w=0&k=20&c=9zbG2-9fl741fbTWw5fNgcEEe4ll-JegrGlQQ6m54rg=" |
status | string | User’s online status | "online" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1772163872 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-6_user_cometchat-uid-7" |
tags | array | Tags associated with the user (when withTags(true)) | ["vip"] |
metadata | object | Custom metadata attached to the user (if set) | {"metadata": "something"} |
blockedByMeAt | number | Timestamp when blocked by current user (if blocked) | 1772164515 |
blockedAt | number | Timestamp of block action (if blocked) | 1772164515 |
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 |
Response
Response
On Success —
getUser() returns a User object containing the requested user’s details:User Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-5" |
name | string | Display name of the user | "John Paul" |
avatar | string | URL to user’s avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-5.webp" |
status | string | User’s online status | "offline" |
role | string | User’s role | "admin" |
lastActiveAt | number | Unix timestamp of last activity | 1772087140 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when user was deactivated (0 if active) | 0 |
tags | array | Tags associated with the user | ["tag1"] |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-5_user_cometchat-uid-7" |
Get online user count
To get the total count of online users for your app, you can use thegetOnlineUserCount() method.
- JavaScript
- TypeScript
Response
Response
On Success —
getOnlineUserCount() returns an object with the count:Response Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
count | number | Total number of online users | 2 |
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.