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 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:
new Group(String GUID, String name, String groupType, String password)
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.CometChatConstants.GROUP_TYPE_PUBLIC
(public)
2.CometChatConstants.GROUP_TYPE_PASSWORD
(password)
3.CometChatConstants.GROUP_TYPE_PRIVATE
(private)
- Create Group
- Typescript
var GUID = "GUID";
var groupName = "Hello Group!";
var groupType = CometChat.GROUP_TYPE.PUBLIC;
var password = "";
var 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);
}
);
var GUID: string = "GUID";
var groupName: string = "Hello Group!";
var groupType: string = CometChat.GROUP_TYPE.PUBLIC;
var password: string = "";
var group: CometChat.Group = new CometChat.Group(GUID, groupName, groupType, password);
CometChat.createGroup(group).then(
(group: CometChat.Group) => {
console.log("Group created successfully:", group);
}, (error: CometChat.CometChatException) => {
console.log("Group creation failed with exception:", error);
}
);
The createGroup() method takes the following parameters:
Parameter | Description |
---|---|
group | An instance of Group class |
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.
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:
new Group(String GUID, String name, String groupType, String password)
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:
CometChat.GROUP_TYPE.PUBLIC
CometChat.GROUP_TYPE.PASSWORD
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)
- Javascript
- Typescript
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)
}
);
let GUID: string = "cometchat-guid-11";
let UID: string = "cometchat-uid-1";
let groupName: string = "Hello Group!";
let groupType: string = CometChat.GROUP_TYPE.PUBLIC;
let group: CometChat.Group = new CometChat.Group(GUID, groupName, groupType);
let members: Array<CometChat.GroupMember> = [
new CometChat.GroupMember(UID, CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT)
];
let banMembers: Array<String> = ["cometchat-uid-2"];
CometChat.createGroupWithMembers(group, members, banMembers).then(
(response: Object) => {
console.log("Group created successfully", response);
}, (error: CometChat.CometChatException) => {
console.log("Some error occured while creating group", error)
}
);
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
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. |