Quick Reference - Fetch group members:
Retrieve the List of Group Members
In order to fetch the list of groups members for a group, you can use theGroupMembersRequest class. To use this class i.e to create an object of the GroupMembersRequest class, you need to use the GroupMembersRequestBuilder class. The GroupMembersRequestBuilder class allows you to set the parameters based on which the groups are to be fetched.
The GroupMembersRequestBuilder class allows you to set the below parameters:
The GUID of the group for which the members are to be fetched must be specified in the constructor of the GroupMembersRequestBuilder class.
Set Limit
This method sets the limit i.e. the number of members 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 group members are to be fetched.- JavaScript
- TypeScript
Set Scopes
This method allows you to fetch group members based on multiple scopes.- JavaScript
- TypeScript
Set Status
The status based on which the group members 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 group members.
- CometChat.USER_STATUS.OFFLINE - will return the list of only offline group members.
- Set Status
- TypeScript
GroupMembersRequest class.
Once you have the object of the GroupMembersRequest class, you need to call the fetchNext() method. Calling this method will return a list of GroupMember objects containing n number of members where n is the limit set in the builder class.
- JavaScript
- TypeScript
Response
Response
On Success —
fetchNext() returns an array of GroupMember objects:GroupMember Object (per item in array):| Parameter | Type | Description | Sample Value |
|---|---|---|---|
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 |
uid | string | Unique identifier of the user | "cometchat-uid-7" |
joinedAt | number | Unix timestamp when the user joined the group | 1772428121 |
name | string | Display name of the user | "Henry Marino" |
avatar | string | URL to user’s avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-7.webp" |
status | string | User’s online status | "online" |
role | string | User’s role | "default" |
statusMessage | string | User’s status message | "Testing CometChat SDK" |
lastActiveAt | number | Unix timestamp of last activity | 1772427635 |
scope | string | Member’s scope in the group (admin, moderator, participant) | "participant" |
conversationId | string | Conversation ID between this user and logged-in user | "cometchat-uid-6_user_cometchat-uid-7" |
guid | string | GUID of the group | "group_1772427551785" |
Best Practices
Use pagination for large groups
Use pagination for large groups
Always use
fetchNext() with a reasonable setLimit() value (e.g., 20-30). For groups with many members, paginate through results rather than fetching all at once.Reuse the same request instance for pagination
Reuse the same request instance for pagination
The
GroupMembersRequest object maintains an internal cursor. Creating a new instance resets the cursor. Reuse the same instance across fetchNext() calls.Filter by scope for admin views
Filter by scope for admin views
Use
setScopes() to fetch only admins or moderators when building admin management UIs, rather than fetching all members and filtering client-side.Troubleshooting
fetchNext returns empty list
fetchNext returns empty list
Verify the GUID is correct and the logged-in user is a member of the group. Non-members cannot fetch the member list of private groups.
Same members returned on each fetchNext call
Same members returned on each fetchNext call
Ensure you’re reusing the same
GroupMembersRequest instance. Creating a new builder resets the pagination cursor.