Quick Reference - Create a group:
Create a Group
In other words, as a logged-in user, how do I create a public, private or password-protected group? You can create a group usingcreateGroup() method. This method takes a Group object as input.
To create an object of Group class, you can use either of the below two constructors:
new Group(String GUID, String name, String groupType, String password)new Group(String GUID, String name, String groupType, String password, String icon, String description)
groupType needs to be either of the below 3 values:
1.CometChat.GROUP_TYPE.PUBLIC
2.CometChat.GROUP_TYPE.PASSWORD
3.CometChat.GROUP_TYPE.PRIVATE
- JavaScript
- TypeScript
Response
Response
On Success —
createGroup() returns the created Group object:Group Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
guid | string | Unique group identifier | "group_1772435275327" |
name | string | Group name | "Creation" |
type | string | Group type | "public" |
owner | string | UID of the group owner | "cometchat-uid-7" |
scope | string | Logged-in user’s scope in the group | "admin" |
membersCount | number | Number of members in the group | 1 |
hasJoined | boolean | Whether logged-in user has joined | true |
isBanned | boolean | Whether logged-in user is banned | false |
joinedAt | number | Unix timestamp when user joined | 1772435275 |
createdAt | number | Unix timestamp when group was created | 1772435275 |
conversationId | string | Conversation identifier for the group | "group_group_1772435275327" |
| Parameter | Description |
|---|---|
group | An instance of Group class |
Group class which contains all the information about the particular group.
Add members while creating a group
You can create a group and add members at the same time using thecreateGroupWithMembers() method. This method takes the Group Object, Array of Group Member Object to be added & Array of UIDs to be banned.
To create an object of Group class, you can use either of the below two constructors:
new Group(String GUID, String name, String groupType, String password)new Group(String GUID, String name, String groupType, String password, String icon, String description)
groupType needs to be either of the below 3 values:
CometChat.GROUP_TYPE.PUBLICCometChat.GROUP_TYPE.PASSWORDCometChat.GROUP_TYPE.PRIVATE
Group Member class, you can use the below constructor:
- new CometChat.GroupMember(String UID, String scope)
- JavaScript
- TypeScript
Response
Response
On Success —
Each key is a UID, and the value is either
createGroupWithMembers() returns an object with group and members keys:Response Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
group | object | The created Group object | See below ↓ |
members | object | Per-UID results for member additions | See below ↓ |
group Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
guid | string | Unique group identifier | "group_with_members_1772435203225" |
name | string | Group name | "Test Group With Members" |
type | string | Group type | "public" |
owner | string | UID of the group owner | "cometchat-uid-7" |
scope | string | Logged-in user’s scope in the group | "admin" |
membersCount | number | Number of members in the group | 4 |
hasJoined | boolean | Whether logged-in user has joined | true |
isBanned | boolean | Whether logged-in user is banned | false |
joinedAt | number | Unix timestamp when user joined | 1772435203 |
createdAt | number | Unix timestamp when group was created | 1772435203 |
conversationId | string | Conversation identifier for the group | "group_group_with_members_1772435203225" |
members Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
cometchat-uid-1 | string | Result for this UID | "success" |
cometchat-uid-2 | string | Result for this UID | "success" |
cometchat-uid-3 | string | Result for this UID | "success" |
"success" or an error message describing why the operation failed.group & members . The group key has the Group Object which contains all the information of the group which is created. The members key has the UID of the users and the value will either be success or an error message describing why the operation to add/ban the user failed.
Group Class
| Field | Editable | Information |
|---|---|---|
| guid | Needs to be specified at group creation. Cannot be edited later | A unique identifier for a group |
| name | Yes | Name of the group |
| type | No | Type of the group: Can be 1. Public 2. Password 3. Private |
| password | No | Password for the group in case the group is of type password. |
| icon | Yes | An URL to group icon |
| description | Yes | Description about the group |
| owner | Yes | UID of the owner of the group. |
| metadata | Yes | Additional data for the group as JSON |
| createdAt | No | The unix timestamp of the time the group was created |
| updatedAt | No | The unix timestamp of the time the group was last updated |
| hasJoined | No | A boolean to determine if the logged in user is a member of the group. |
| joinedAt | No | The unix timestamp of the time the logged in user joined the group. |
| scope | Yes | Scope of the logged in user. Can be: 1. Admin 2. Moderator 3. Participant |
| membersCount | No | The number of members in the groups |
| tags | Yes | A list of tags to identify specific groups. |
Best Practices
Use createGroupWithMembers for initial setup
Use createGroupWithMembers for initial setup
If you know the initial members at creation time, use
createGroupWithMembers() instead of creating the group first and then adding members separately. This reduces API calls and ensures atomic group setup.Keep GUIDs consistent with your system
Keep GUIDs consistent with your system
Use the same group identifier from your backend as the CometChat GUID. This simplifies mapping between your system and CometChat.
Check member add results individually
Check member add results individually
The
createGroupWithMembers() response includes per-UID results (“success” or error). Check each result rather than assuming all members were added successfully.Troubleshooting
createGroup fails with 'GUID already exists'
createGroup fails with 'GUID already exists'
Each GUID must be unique across your CometChat app. If the group already exists, use a different GUID or retrieve the existing group with
getGroup().GUID validation error
GUID validation error
GUIDs can only contain alphanumeric characters, underscores, and hyphens. Spaces, punctuation, and other special characters are not allowed. Uppercase characters are automatically converted to lowercase.
Members not added during createGroupWithMembers
Members not added during createGroupWithMembers
Check the
members key in the response object for per-UID error messages. Common causes include invalid UIDs or users that don’t exist in your CometChat app.