Quick Reference// Create a group
const group = new CometChat.Group("GUID", "Group Name", CometChat.GROUP_TYPE.PUBLIC);
await CometChat.createGroup(group);
// Join a group
await CometChat.joinGroup("GUID", CometChat.GROUP_TYPE.PUBLIC, "");
// Send message to group
const msg = new CometChat.TextMessage("GUID", "Hello!", CometChat.RECEIVER_TYPE.GROUP);
await CometChat.sendMessage(msg);
// Fetch groups
const request = new CometChat.GroupsRequestBuilder().setLimit(30).build();
const groups = await request.fetchNext();
// Leave group
await CometChat.leaveGroup("GUID");
Group Types: PUBLIC (open), PASSWORD (requires password), PRIVATE (invite only)
Member Scopes: ADMIN > MODERATOR > PARTICIPANT
Groups enable multiple users to communicate together. CometChat supports public, private, and password-protected groups with role-based member management.
Group Types
| Type | Visibility | How to Join |
|---|
| Public | Visible to all users | Anyone can join freely |
| Password | Visible to all users | Requires correct password |
| Private | Only visible to members | Invitation only (auto-joined) |
// Group type constants
CometChat.GROUP_TYPE.PUBLIC
CometChat.GROUP_TYPE.PASSWORD
CometChat.GROUP_TYPE.PRIVATE
Member Scopes
| Scope | Capabilities |
|---|
| Admin | Full control: manage all members, update/delete group, change any scope |
| Moderator | Moderate: kick/ban participants, update group info |
| Participant | Basic: send/receive messages and calls |
// Scope constants
CometChat.GROUP_MEMBER_SCOPE.ADMIN
CometChat.GROUP_MEMBER_SCOPE.MODERATOR
CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT
The group creator is automatically assigned as Admin.
Quick Start
Create a Group
const GUID = "group_" + Date.now();
const groupName = "My Group";
const groupType = CometChat.GROUP_TYPE.PUBLIC;
const group = new CometChat.Group(GUID, groupName, groupType);
CometChat.createGroup(group).then(
(group) => console.log("Group created:", group),
(error) => console.log("Error:", error)
);
async function createGroup() {
const GUID = "group_" + Date.now();
const groupName = "My Group";
const groupType = CometChat.GROUP_TYPE.PUBLIC;
const group = new CometChat.Group(GUID, groupName, groupType);
try {
const createdGroup = await CometChat.createGroup(group);
console.log("Group created:", createdGroup);
return createdGroup;
} catch (error) {
console.log("Error:", error);
throw error;
}
}
Fetch Groups
const groupsRequest = new CometChat.GroupsRequestBuilder()
.setLimit(30)
.build();
groupsRequest.fetchNext().then(
(groups) => console.log("Groups:", groups),
(error) => console.log("Error:", error)
);
async function fetchGroups() {
const groupsRequest = new CometChat.GroupsRequestBuilder()
.setLimit(30)
.build();
try {
const groups = await groupsRequest.fetchNext();
console.log("Groups:", groups);
return groups;
} catch (error) {
console.log("Error:", error);
throw error;
}
}
Group Management
Membership
Member Management
Group Object Properties
| Property | Method | Description | Editable |
|---|
| GUID | getGuid() | Unique identifier | No (set at creation) |
| Name | getName() | Group name | Yes |
| Type | getType() | public, password, private | No |
| Icon | getIcon() | Group image URL | Yes |
| Description | getDescription() | Group description | Yes |
| Owner | getOwner() | UID of group owner | Yes |
| Metadata | getMetadata() | Custom JSON data | Yes |
| Members Count | getMembersCount() | Number of members | No |
| Has Joined | getHasJoined() | If logged-in user is member | No |
| Joined At | getJoinedAt() | When user joined | No |
| Scope | getScope() | User’s scope in group | Yes |
| Tags | getTags() | Array of tags | Yes |
| Created At | getCreatedAt() | Creation timestamp | No |
Common Use Cases
Create Group with Members
const GUID = "team_chat";
const groupName = "Team Chat";
const groupType = CometChat.GROUP_TYPE.PRIVATE;
const group = new CometChat.Group(GUID, groupName, groupType);
const members = [
new CometChat.GroupMember("user1", CometChat.GROUP_MEMBER_SCOPE.ADMIN),
new CometChat.GroupMember("user2", CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT),
new CometChat.GroupMember("user3", CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT)
];
CometChat.createGroupWithMembers(group, members, []).then(
(response) => {
console.log("Group created:", response.group);
console.log("Members added:", response.members);
}
);
async function createGroupWithMembers() {
const GUID = "team_chat";
const groupName = "Team Chat";
const groupType = CometChat.GROUP_TYPE.PRIVATE;
const group = new CometChat.Group(GUID, groupName, groupType);
const members = [
new CometChat.GroupMember("user1", CometChat.GROUP_MEMBER_SCOPE.ADMIN),
new CometChat.GroupMember("user2", CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT),
new CometChat.GroupMember("user3", CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT)
];
try {
const response = await CometChat.createGroupWithMembers(group, members, []);
console.log("Group created:", response.group);
console.log("Members added:", response.members);
return response;
} catch (error) {
console.log("Error:", error);
throw error;
}
}
Join a Public Group
const GUID = "public_group";
const groupType = CometChat.GROUP_TYPE.PUBLIC;
CometChat.joinGroup(GUID, groupType).then(
(group) => console.log("Joined group:", group),
(error) => console.log("Error:", error)
);
async function joinPublicGroup(GUID) {
try {
const group = await CometChat.joinGroup(GUID, CometChat.GROUP_TYPE.PUBLIC);
console.log("Joined group:", group);
return group;
} catch (error) {
console.log("Error:", error);
throw error;
}
}
Join a Password Group
const GUID = "password_group";
const groupType = CometChat.GROUP_TYPE.PASSWORD;
const password = "secret123";
CometChat.joinGroup(GUID, groupType, password).then(
(group) => console.log("Joined group:", group),
(error) => console.log("Error:", error)
);
async function joinPasswordGroup(GUID, password) {
try {
const group = await CometChat.joinGroup(
GUID,
CometChat.GROUP_TYPE.PASSWORD,
password
);
console.log("Joined group:", group);
return group;
} catch (error) {
console.log("Error:", error);
throw error;
}
}
Search Groups
const groupsRequest = new CometChat.GroupsRequestBuilder()
.setLimit(30)
.setSearchKeyword("team")
.build();
groupsRequest.fetchNext().then((groups) => {
console.log("Search results:", groups);
});
async function searchGroups(keyword) {
const groupsRequest = new CometChat.GroupsRequestBuilder()
.setLimit(30)
.setSearchKeyword(keyword)
.build();
try {
const groups = await groupsRequest.fetchNext();
console.log("Search results:", groups);
return groups;
} catch (error) {
console.log("Error:", error);
throw error;
}
}
Filter Joined Groups
const groupsRequest = new CometChat.GroupsRequestBuilder()
.setLimit(30)
.joinedOnly(true)
.build();
groupsRequest.fetchNext().then((groups) => {
console.log("My groups:", groups);
});
async function fetchJoinedGroups() {
const groupsRequest = new CometChat.GroupsRequestBuilder()
.setLimit(30)
.joinedOnly(true)
.build();
try {
const groups = await groupsRequest.fetchNext();
console.log("My groups:", groups);
return groups;
} catch (error) {
console.log("Error:", error);
throw error;
}
}
Real-Time Group Events
Listen for group changes:
const listenerID = "GROUP_LISTENER";
CometChat.addGroupListener(
listenerID,
new CometChat.GroupListener({
onGroupMemberJoined: (message, joinedUser, joinedGroup) => {
console.log(joinedUser.getName(), "joined", joinedGroup.getName());
},
onGroupMemberLeft: (message, leftUser, leftGroup) => {
console.log(leftUser.getName(), "left", leftGroup.getName());
},
onGroupMemberKicked: (message, kickedUser, kickedBy, kickedFrom) => {
console.log(kickedUser.getName(), "was kicked by", kickedBy.getName());
},
onGroupMemberBanned: (message, bannedUser, bannedBy, bannedFrom) => {
console.log(bannedUser.getName(), "was banned");
},
onGroupMemberUnbanned: (message, unbannedUser, unbannedBy, unbannedFrom) => {
console.log(unbannedUser.getName(), "was unbanned");
},
onGroupMemberScopeChanged: (message, changedUser, newScope, oldScope, changedGroup) => {
console.log(changedUser.getName(), "scope changed to", newScope);
},
onMemberAddedToGroup: (message, userAdded, userAddedBy, userAddedIn) => {
console.log(userAdded.getName(), "was added by", userAddedBy.getName());
}
})
);
// Remove when done
CometChat.removeGroupListener(listenerID);
Next Steps