Skip to main content
Quick Reference for AI Agents & Developers
// Fetch groups with filters
val groupsRequest = GroupsRequestBuilder()
    .setLimit(30)
    .joinedOnly(true)
    .setSearchKeyWord("search")
    .build()

groupsRequest.fetchNext(object : CallbackListener<List<Group>>() {
    override fun onSuccess(groups: List<Group>) { }
    override fun onError(e: CometChatException) { }
})

// Get specific group details
CometChat.getGroup("GUID", callback)
Available via: SDK | REST API | UI Kits

Retrieve List of Groups

How do I retrieve the list of groups I’ve joined and groups that are available? To fetch the list of groups, use the GroupsRequest class. 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 fetched.

Set Limit

This method sets the limit i.e. the number of groups that should be fetched in a single iteration.
GroupsRequest groupsRequest = new GroupsRequest.GroupsRequestBuilder()
.setLimit(limit)
.build();

Set Search Keyword

This method allows you to set the search string based on which the groups are to be fetched.
GroupsRequest groupsRequest = new GroupsRequest.GroupsRequestBuilder()
.setSearchKeyWord("abc")
.build();

Joined Only

This method tells CometChat to only return the groups that the user has joined or is a part of.
GroupsRequest groupsRequest = new GroupsRequest.GroupsRequestBuilder()
.joinedOnly(true)
.build();

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.
List<String> tags = new ArrayList<>();
tags.add("tag1");
tags.add("tag2");
GroupsRequest groupsRequest = new GroupsRequest.GroupsRequestBuilder()
.setLimit(limit)
.setTags(tags)
.build();  

With Tags

This property when set to true will fetch tags data along with the list of groups.
GroupsRequest groupsRequest = new GroupsRequest.GroupsRequestBuilder()
.setLimit(limit)
.withTags(true)
.build();
Finally, once all the parameters are set on the builder class, call the build() method to get the object of the GroupsRequest class. Once you have the object of the GroupsRequest class, call the fetchNext() method. This method returns a list of Group objects containing N number of groups depending on the limit set. The list of groups fetched will only include public and password type groups. Private groups are only included if the user is a member of that private group.
private GroupsRequest groupsRequest = null;
private int limit = 30;

groupsRequest = new GroupsRequest.GroupsRequestBuilder().setLimit(limit).build();

groupsRequest.fetchNext(new CometChat.CallbackListener<List<Group>>() {
@Override
public void onSuccess(List <Group> list) {
  Log.d(TAG, "Groups list fetched successfully: " + list.size());
}
@Override
public void onError(CometChatException e) {
  Log.d(TAG, "Groups list fetching failed with exception: " + e.getMessage());
}
});

Retrieve Particular Group Details

How do I retrieve information for a specific group? To get the information of a group, use the getGroup() method.
private String GUID = "GUID";

CometChat.getGroup(GUID, new CometChat.CallbackListener<Group>() {
@Override
public void onSuccess(Group group) {
  Log.d(TAG, "Group details fetched successfully: " + group.toString());        
}

@Override
public void onError(CometChatException e) { 
  Log.d(TAG, "Group details fetching failed with exception: " + e.getMessage());   

}
});
ParameterDescription
GUIDThe GUID of the group for whom the details are to be fetched
On success, the Group object containing the details of the group is returned.

Get online group member count

To get the total count of online users in particular groups, you can use the getOnlineGroupMemberCount() method.
List<String> guids = new ArrayList<>();
guids.add("cometchat-guid-1");
guids.add("cometchat-guid-11");

CometChat.getOnlineGroupMemberCount(guids, new CometChat.CallbackListener<HashMap<String, Integer>>() {
@Override
public void onSuccess(HashMap<String, Integer> stringIntegerHashMap) {
  Log.d(TAG, "Online count fetched successfully " + stringIntegerHashMap.toString());
}

@Override
public void onError(CometChatException e) {
  Log.d(TAG, e.getMessage());
}
});
This method returns a Hashmap with the GUID of the group as the key and the online member count for that group as the value.

Best Practices

Set an appropriate limit (e.g., 30) and call fetchNext() multiple times to load groups in batches, improving performance and user experience.
Use joinedOnly(true) to fetch only groups the user has joined, reducing unnecessary data transfer and improving load times.
Use setSearchKeyWord() to allow users to quickly find specific groups by name, especially in apps with many groups.
Organize groups with tags and use setTags() to fetch groups by category (e.g., “sports”, “work”, “friends”).

Troubleshooting

Symptom: fetchNext() returns an empty list even though groups exist.Cause: Filters may be too restrictive, or the user hasn’t joined any groups (when using joinedOnly(true)).Solution: Remove filters temporarily to verify groups exist. Check if joinedOnly() is appropriate for your use case.
Symptom: Private groups are missing from the fetched list.Cause: Private groups only appear if the user is a member.Solution: This is expected behavior. Users must be added to private groups by admins before they can see them.
Symptom: getGroup() fails with “Group not found” error.Cause: The GUID is incorrect, or the group has been deleted.Solution: Verify the GUID is correct. Check if the group still exists by fetching the groups list first.

Next Steps