Skip to main content
// Create a group
let group = new CometChat.Group("GUID", "Group Name", CometChat.GROUP_TYPE.PUBLIC, "");
let createdGroup = await CometChat.createGroup(group);

// Create group with members
let members = [new CometChat.GroupMember("UID", CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT)];
let result = await CometChat.createGroupWithMembers(group, members, []);
Group types: PUBLIC | PASSWORD | PRIVATE Member scopes: ADMIN | MODERATOR | PARTICIPANT

Create a Group

Use createGroup() to create a new group. Pass a Group object with the group details. Group constructors:
  • new Group(GUID, name, groupType, password)
  • new Group(GUID, name, groupType, password, icon, description)
Group types:
  • CometChat.GROUP_TYPE.PUBLIC
  • CometChat.GROUP_TYPE.PASSWORD
  • CometChat.GROUP_TYPE.PRIVATE
const GUID = "GUID";
const groupName = "Hello Group!";
const groupType = CometChat.GROUP_TYPE.PUBLIC;
const password = "";

const group = new CometChat.Group(GUID, groupName, groupType, password);

CometChat.createGroup(group).then(
  group => {
  	console.log("Group created successfully:", group);
  }, error => {
  	console.log("Group creation failed with exception:", error);
  }
);
ParameterDescription
groupAn instance of Group class
On success, returns a Group object with the created group’s details. The method returns a Group object. Access the response data using getter methods:
FieldGetterReturn TypeDescription
guidgetGuid()stringUnique group ID
namegetName()stringDisplay name of the group
typegetType()stringGroup type ("public", "private", or "password")
ownergetOwner()stringUID of the group owner
membersCountgetMembersCount()numberTotal number of members in the group
createdAtgetCreatedAt()numberTimestamp when the group was created
GUID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed.

Add Members While Creating a Group

Use createGroupWithMembers() to create a group and add members in one operation. Parameters:
  • group — The Group object
  • members — Array of GroupMember objects to add
  • bannedMembers — Array of UIDs to ban (can be empty)
Create a GroupMember with: new CometChat.GroupMember(UID, scope)
let GUID = "cometchat-guid-11";
let UID = "cometchat-uid-1";
let groupName = "Hello Group!";
let groupType = CometChat.GROUP_TYPE.PUBLIC;

let group = new CometChat.Group(GUID, groupName, groupType);
let members = [
new CometChat.GroupMember(UID, CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT)
];
let banMembers = ["cometchat-uid-2"];

CometChat.createGroupWithMembers(group, members, banMembers).then(
  response => {
  	console.log("Group created successfully", response);
  }, error => {
  	console.log("Some error occured while creating group", error)
  }
);
Returns an object with two keys:
  • group — The created Group object
  • members — Object with UIDs as keys and "success" or error message as values

Group Class

FieldEditableInformation
guidNeeds to be specified at group creation. Cannot be edited laterA unique identifier for a group
nameYesName of the group
typeNoType of the group: Can be 1. Public 2. Password 3. Private
passwordNoPassword for the group in case the group is of type password.
iconYesAn URL to group icon
descriptionYesDescription about the group
ownerYesUID of the owner of the group.
metadataYesAdditional data for the group as JSON
createdAtNoThe unix timestamp of the time the group was created
updatedAtNoThe unix timestamp of the time the group was last updated
hasJoinedNoA boolean to determine if the logged in user is a member of the group.
joinedAtNoThe unix timestamp of the time the logged in user joined the group.
scopeYesScope of the logged in user. Can be: 1. Admin 2. Moderator 3. Participant
membersCountNoThe number of members in the groups
tagsYesA list of tags to identify specific groups.

Next Steps

Join a Group

Join public, private, or password-protected groups

Add Members

Add users to an existing group

Retrieve Groups

Fetch and filter group lists

Groups Overview

Overview of all group management features