Skip to main content
Quick Reference for AI Agents & Developers
// Create a public group
val group = Group("GUID", "Group Name", CometChatConstants.GROUP_TYPE_PUBLIC, "")
CometChat.createGroup(group, object : CometChat.CallbackListener<Group>() {
    override fun onSuccess(group: Group) { }
    override fun onError(e: CometChatException) { }
})

// Create with members
val groupMembers = listOf(
    GroupMember("uid1", CometChatConstants.SCOPE_ADMIN),
    GroupMember("uid2", CometChatConstants.SCOPE_PARTICIPANT)
)
CometChat.createGroupWithMembers(group, groupMembers, emptyList(), callback)
Available via: SDK | REST API | UI Kits

Create a Group

As a logged-in user, how do I create a public, private, or password-protected group? You can create a group using the createGroup() method. This method takes a Group object as input. To create an object of the Group class, you can use either of the following 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 one of the following values:
  1. CometChatConstants.GROUP_TYPE_PUBLIC (public)
  2. CometChatConstants.GROUP_TYPE_PASSWORD (password)
  3. CometChatConstants.GROUP_TYPE_PRIVATE (private)
private String GUID = "GUID";
private String groupName = "Hello Group!";
private String groupType = CometChatConstants.GROUP_TYPE_PUBLIC;
private String password = "";

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

CometChat.createGroup(group, new CometChat.CallbackListener<Group>(){
@Override
public void onSuccess(Group group) {
  Log.d(TAG, "Group created successfully: " + group.toString());
}
@Override
public void onError(CometChatException e) {
  Log.d(TAG, "Group creation failed with exception: " + e.getMessage());
}
});
The createGroup() method takes the following parameters:
ParameterDescription
groupAn instance of Group class
After the successful creation of the group, you will receive an instance of the 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.

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, an array of GroupMember objects to be added, and an array of UIDs to be banned. To create an object of the Group class, you can use either of the following 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 one of the following values:
  1. CometChat.GROUP_TYPE.PUBLIC
  2. CometChat.GROUP_TYPE.PASSWORD
  3. CometChat.GROUP_TYPE.PRIVATE
To create an object of the GroupMember class, you can use the following constructor:
  • new GroupMember(String UID, String scope)
Group group = new Group("test_1", "Test 1", CometChatConstants.GROUP_TYPE_PUBLIC,null);

List<GroupMember> groupMembers = new ArrayList<>();
groupMembers.add(new GroupMember("cometchat-uid-1",CometChatConstants.SCOPE_ADMIN));
groupMembers.add(new GroupMember("cometchat-uid-2",CometChatConstants.SCOPE_MODERATOR));
groupMembers.add(new GroupMember("cometchat-uid-3",CometChatConstants.SCOPE_PARTICIPANT));

List<String> bannedUIDs = new ArrayList<>();
bannedUIDs.add("cometchat-uid-4");

CometChat.createGroupWithMembers(group, groupMembers, bannedUIDs, new CometChat.CreateGroupWithMembersListener() {
@Override
public void onSuccess(Group group, HashMap<String, String> hashMap) {
  Logger.error(TAG, group.toString());
  Logger.error(TAG, hashMap.toString());
}

@Override
public void onError(CometChatException e) {
  Logger.error(TAG, e.getMessage());
}
});
The onSuccess() block of this method provides you with 2 sets of information:
  1. Group: The group object containing information about the group that was created.
  2. HashMap<String, String>: A HashMap that contains the UID of the user that was supposed to be added as the key and success or an error message as the value.

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.

Best Practices

Choose meaningful GUIDs and names that help identify groups. GUIDs should be alphanumeric with underscores/hyphens only - avoid spaces and special characters.
Use PUBLIC for open communities, PRIVATE for invite-only groups, and PASSWORD for semi-private groups. Choose based on your privacy requirements.
Use the metadata field to store custom group properties like categories, tags, or business-specific data without creating custom group types.

Troubleshooting

Symptom: createGroup() fails with “Invalid GUID” error.Cause: GUID contains spaces, punctuation, or special characters other than underscore and hyphen.Solution: Use only alphanumeric characters, underscores, and hyphens in GUIDs. Example: "my_group_123" or "group-abc-456".
Symptom: createGroup() fails with “Group already exists” error.Cause: A group with the same GUID already exists in your app.Solution: Use unique GUIDs for each group. Consider using UUIDs or timestamp-based identifiers to ensure uniqueness.

Next Steps