Quick Reference for AI Agents & Developers// Fetch groups list
const groupsRequest = new CometChat.GroupsRequestBuilder()
.setLimit(30)
.build();
const groups = await groupsRequest.fetchNext();
// Get joined groups only
const groupsRequest = new CometChat.GroupsRequestBuilder()
.joinedOnly(true)
.build();
// Search groups
const groupsRequest = new CometChat.GroupsRequestBuilder()
.setSearchKeyword("team")
.build();
// Filter by tags
const groupsRequest = new CometChat.GroupsRequestBuilder()
.setTags(["engineering", "product"])
.withTags(true)
.build();
// Get specific group
const group = await CometChat.getGroup("group_guid");
// Get online member count
const counts = await CometChat.getOnlineGroupMemberCount(["guid1", "guid2"]);
Retrieve group information including group lists, individual group details, and online member counts.
Availability: SDK, API, UI KitsPublic and password groups are visible to all users. Private groups are only visible to members.
Fetch Group List
Use GroupsRequestBuilder to fetch groups with various filters:
JavaScript
TypeScript
Async/Await
const limit = 30;
const groupsRequest = new CometChat.GroupsRequestBuilder()
.setLimit(limit)
.build();
groupsRequest.fetchNext().then(
(groupList) => console.log("Groups:", groupList),
(error) => console.log("Failed:", error)
);
const limit: number = 30;
const groupsRequest: CometChat.GroupsRequest = new CometChat.GroupsRequestBuilder()
.setLimit(limit)
.build();
groupsRequest.fetchNext().then(
(groupList: CometChat.Group[]) => console.log("Groups:", groupList),
(error: CometChat.CometChatException) => console.log("Failed:", error)
);
async function fetchGroups(limit = 30) {
const groupsRequest = new CometChat.GroupsRequestBuilder()
.setLimit(limit)
.build();
try {
const groupList = await groupsRequest.fetchNext();
console.log(`Fetched ${groupList.length} groups`);
return groupList;
} catch (error) {
console.error("Failed to fetch groups:", error);
throw error;
}
}
// Usage
const groups = await fetchGroups();
Builder Options
| Method | Description |
|---|
setLimit(limit) | Number of groups per request (max 100) |
setSearchKeyword(keyword) | Search groups by name |
joinedOnly(true) | Only return groups user has joined |
setTags(tags) | Filter by group tags |
withTags(true) | Include tags in response |
Get Joined Groups Only
Fetch only groups the user is a member of:
const groupsRequest = new CometChat.GroupsRequestBuilder()
.setLimit(30)
.joinedOnly(true)
.build();
groupsRequest.fetchNext().then(
(groupList) => console.log("My groups:", groupList),
(error) => console.log("Failed:", error)
);
Search Groups
const groupsRequest = new CometChat.GroupsRequestBuilder()
.setLimit(30)
.setSearchKeyword("team")
.build();
groupsRequest.fetchNext().then(
(groupList) => console.log("Search results:", groupList),
(error) => console.log("Failed:", error)
);
const groupsRequest = new CometChat.GroupsRequestBuilder()
.setLimit(30)
.setTags(["engineering", "product"])
.withTags(true) // Include tags in response
.build();
groupsRequest.fetchNext().then(
(groupList) => {
groupList.forEach((group) => {
console.log(group.getName(), "Tags:", group.getTags());
});
},
(error) => console.log("Failed:", error)
);
Get Single Group
Fetch details for a specific group:
const GUID = "group-123";
CometChat.getGroup(GUID).then(
(group) => {
console.log("Name:", group.getName());
console.log("Type:", group.getType());
console.log("Members:", group.getMembersCount());
console.log("Has Joined:", group.getHasJoined());
},
(error) => console.log("Failed:", error)
);
const GUID: string = "group-123";
CometChat.getGroup(GUID).then(
(group: CometChat.Group) => {
console.log("Name:", group.getName());
console.log("Type:", group.getType());
console.log("Members:", group.getMembersCount());
},
(error: CometChat.CometChatException) => console.log("Failed:", error)
);
Get Online Member Count
Get the count of online members in specific groups:
const guids = ["group-123", "group-456"];
CometChat.getOnlineGroupMemberCount(guids).then(
(counts) => {
console.log("Online counts:", counts);
// { "group-123": 5, "group-456": 12 }
},
(error) => console.log("Failed:", error)
);
Use fetchNext() repeatedly to paginate through results:
class GroupListManager {
constructor() {
this.groupsRequest = new CometChat.GroupsRequestBuilder()
.setLimit(30)
.build();
this.groups = [];
}
async loadMore() {
try {
const groupList = await this.groupsRequest.fetchNext();
this.groups = [...this.groups, ...groupList];
return groupList;
} catch (error) {
console.log("Failed to load groups:", error);
return [];
}
}
}
// Usage
const manager = new GroupListManager();
const firstPage = await manager.loadMore();
const secondPage = await manager.loadMore();
Group Properties
| Property | Type | Description |
|---|
guid | String | Unique identifier |
name | String | Display name |
type | String | PUBLIC, PASSWORD, or PRIVATE |
icon | String | URL to group icon |
description | String | Group description |
owner | String | UID of group owner |
membersCount | Number | Number of members |
hasJoined | Boolean | If logged-in user is a member |
joinedAt | Number | When user joined (timestamp) |
scope | String | User’s scope (ADMIN, MODERATOR, PARTICIPANT) |
createdAt | Number | Creation timestamp |
tags | Array | Group tags |
metadata | Object | Custom data |
Next Steps