Quick Reference - Fetch groups:
Overview
In other words, as a logged-in user, how do I retrieve the list of groups I’ve joined and groups that are available? In order to fetch the list of groups, you can use theGroupsRequest class. To use this class i.e to create an object of the GroupsRequest class, you need to use the GroupsRequestBuilder class. The GroupsRequestBuilder class allows you to set the parameters based on which the groups are to be fetched.
GroupsRequestBuilder
TheGroupsRequestBuilder class allows you to set the below parameters:
Set Limit
This method sets the limit i.e. the number of groups 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 groups are to be fetched.- JavaScript
- TypeScript
Joined Only
This method when used, will ask the SDK to only return the groups that the user has joined or is a part of.- JavaScript
- TypeScript
Set Tags
This method accepts a list of tags based on which the list of groups is to be fetched. The list fetched will only contain the groups 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 groups.- JavaScript
- TypeScript
GroupsRequest class.
Once you have the object of the GroupsRequest class, you need to call the fetchNext() method. Calling this method will return a list of Group objects containing n number of groups where n is the limit set in the builder class.
The list of groups fetched will only have the public and password type groups. The private groups will only be available if the user is a member of the group.
- JavaScript
- TypeScript
Retrieve Particular Group Details
In other words, as a logged-in user, how do I retrieve information for a specific group? To get the information of a group, you can use thegetGroup() method.
- JavaScript
- TypeScript
| Parameter | Description |
|---|---|
GUID | The GUID of the group for whom the details are to be fetched |
Group object containing the details of the group.
Get online group member count
To get the total count of online users in particular groups, you can use thegetOnlineGroupMemberCount() method.
- JavaScript
- TypeScript
Best Practices
Use pagination for large group lists
Use pagination for large group lists
Always use
fetchNext() with a reasonable setLimit() value (e.g., 20-30) rather than fetching all groups at once. This improves performance and reduces memory usage.Reuse the same GroupsRequest instance for pagination
Reuse the same GroupsRequest instance for pagination
The
GroupsRequest 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.Use joinedOnly for user-specific views
Use joinedOnly for user-specific views
When displaying “My Groups” in your UI, use
joinedOnly(true) to fetch only groups the user belongs to, rather than filtering the full list client-side.Troubleshooting
Private groups not appearing in list
Private groups not appearing in list
Private groups are only returned if the logged-in user is a member. This is by design — use
joinedOnly(true) to see all groups the user has access to, including private ones.fetchNext returns empty list
fetchNext returns empty list
Verify the logged-in user session is active. Also check if filters like
setTags or setSearchKeyword are too restrictive. Try removing filters to confirm groups exist.getGroup returns error
getGroup returns error
Ensure the GUID exists in your CometChat app. GUIDs are case-sensitive — double-check the exact GUID string.