Fetch, filter, and search groups using the CometChat JavaScript SDK. Includes pagination, tag-based filtering, joined-only groups, and online member counts.
AI Integration Quick Reference
// Fetch groups listconst request = new CometChat.GroupsRequestBuilder() .setLimit(30).build();const groups = await request.fetchNext();// Get specific group detailsconst group = await CometChat.getGroup("GUID");// Fetch only joined groupsconst joinedRequest = new CometChat.GroupsRequestBuilder() .setLimit(30).joinedOnly(true).build();// Get online member countconst count = await CometChat.getOnlineGroupMemberCount(["GUID"]);
Fetch the list of Group objects the logged-in user can see, get details for a specific group, or check online member counts.
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 the GroupsRequest 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.Fetching using this builder will return Group objectsUse GroupsRequestBuilder to fetch groups with filtering, searching, and pagination.
When true, includes tag data in the returned group objects.
TypeScript
JavaScript
let limit: number = 30;let groupsRequest: CometChat.GroupsRequest = new CometChat.GroupsRequestBuilder() .setLimit(limit) .withTags(true) .build();
let limit = 30;let groupsRequest = new CometChat.GroupsRequestBuilder() .setLimit(limit) .withTags(true) .build();
Relevant fields to access on returned groups:
Field
Getter
Return Type
Description
tags
getTags()
string[]
Tags associated with the group
Finally, once all the parameters are set to the builder class, you need to call the build() method to get the object of the 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.
TypeScript
JavaScript
let limit = 30;let groupsRequest = new CometChat.GroupsRequestBuilder() .setLimit(limit) .withTags(true) .build();
let limit = 30;let groupsRequest = new CometChat.GroupsRequestBuilder() .setLimit(limit) .withTags(true) .build();
After configuring the builder, call build() to get the GroupsRequest object, then call fetchNext() to retrieve groups.
The list only includes public and password-protected groups. Private groups appear only if the user is a member.
TypeScript
JavaScript
let limit: number = 30;let groupsRequest: CometChat.GroupsRequest = new CometChat.GroupsRequestBuilder() .setLimit(limit) .build();groupsRequest.fetchNext().then( (groupList: CometChat.Group[]) => { console.log("Groups list fetched successfully", groupList); }, (error: CometChat.CometChatException) => { console.log("Groups list fetching failed with error", error); });
let limit = 30;let groupsRequest = new CometChat.GroupsRequestBuilder() .setLimit(limit) .build();groupsRequest.fetchNext().then(groupList => { console.log("Groups list fetched successfully", groupList);}, error => { console.log("Groups list fetching failed with error", error);});
The fetchNext() method returns an array of Group objects.
Use getOnlineGroupMemberCount() to get the number of online members in specified groups.
TypeScript
JavaScript
let guids: String[] = ["cometchat-guid-1"];CometChat.getOnlineGroupMemberCount(guids).then( (groupMemberCount: Object) => { console.log("Total online user for specified groups:", groupMemberCount); }, (error: CometChat.CometChatException) => { console.log("Online group member count fetching failed with error:", error); });
let guids = ["cometchat-guid-1"];CometChat.getOnlineGroupMemberCount(guids).then(groupMemberCount => { console.log("Total online user for specified groups:", groupMemberCount);}, error => { console.log("Online group member count fetching failed with error:", error);});
Returns an object with GUIDs as keys and online member counts as values.getOnlineGroupMemberCount() resolves with a { guid: count } object where each key is a group GUID and its value is the number of currently online members in that group.