Skip to main content
Quick Reference - Create a group:
Available via: SDK | REST API | UI Kits

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 using createGroup() 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:
  1. new Group(String GUID, String name, String groupType, String password)
  2. new Group(String GUID, String name, String groupType, String password, String icon, String description)
The 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
On SuccesscreateGroup() returns the created Group object:Group Object:
The createGroup() method takes the following parameters: After successful creation of the group, you will receive an instance of Group class which contains all the information about the particular group.
GUID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed. CometChat automatically converts any uppercase characters in the GUID to lowercase.

Add members while creating a group

You can create a group and add members at the same time using the createGroupWithMembers() 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:
  1. new Group(String GUID, String name, String groupType, String password)
  2. new Group(String GUID, String name, String groupType, String password, String icon, String description)
The 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
To create an object of Group Member class, you can use the below constructor:
  • new CometChat.GroupMember(String UID, String scope)
On SuccesscreateGroupWithMembers() returns an object with group and members keys:Response Object:
group Object:
members Object:Each key is a UID, and the value is either "success" or an error message describing why the operation failed.
This method returns an Object which has two keys: 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

Best Practices

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.
Use the same group identifier from your backend as the CometChat GUID. This simplifies mapping between your system and CometChat.
The createGroupWithMembers() response includes per-UID results (“success” or error). Check each result rather than assuming all members were added successfully.

Troubleshooting

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().
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.
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.

Next Steps

Retrieve Groups

Fetch group lists, search groups, and get group details

Join a Group

Join public or password-protected groups

Group Members

Manage members, roles, and permissions within groups

Send Messages

Send text, media, and custom messages to groups