Skip to main content
Quick Reference - Add members to a group:
const members = [
  new CometChat.GroupMember("UID", CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT),
];
await CometChat.addMembersToGroup("GUID", members, []);
Available via: SDK | REST API

Add Members to Group

You can add members to the group using the addMembersToGroup() method. This method takes the below parameters:
  1. GUID - GUID of the group the members are to be added to.
  2. members - This is a list of GroupMember objects. In order to add members, you need to create an object of the GroupMember class. The UID and the scope of the GroupMember are mandatory.
  3. bannedMembers - This is the list of UID's that need to be banned from the Group. This can be set to null if there are no members to be banned.
let GUID = "GUID";
let UID = "UID";
let membersList = [
  new CometChat.GroupMember(UID, CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT),
];

CometChat.addMembersToGroup(GUID, membersList, []).then(
  (response) => {
    console.log("response", response);
  },
  (error) => {
    console.log("Something went wrong", error);
  }
);
It will return a Array which will contain the UID of the users and the value will either be success or an error message describing why the operation to add the user to the group.
On SuccessaddMembersToGroup() returns an object with each UID as key and result as value:
ParameterTypeDescriptionSample Value
[UID]stringResult for each UID (“success” or error message)"success"
Example:
ParameterTypeDescriptionSample Value
cometchat-uid-7stringResult for the added member"success"

Real-Time Group Member Added Events

In other words, as a member of a group, how do I know when someone is added to the group when my app is running?
When a group member is added by another member, this event is triggered. When a user joins a group on their own, the joined event is triggered.
To receive real-time events whenever a new member is added to a group, you need to implement the onMemberAddedToGroup() methods of the GroupListener class. onMemberAddedToGroup() - This method is triggered when any user is added to the group so that the logged in user is informed of the other members added to the group.
var listenerID = "UNIQUE_LISTENER_ID";

CometChat.addGroupListener(
  listenerID,
  new CometChat.GroupListener({
    onMemberAddedToGroup: (message, userAdded, userAddedBy, userAddedIn) => {
      console.log("User joined", {
        message,
        userAdded,
        userAddedBy,
        userAddedIn,
      });
    },
  })
);
Always remove group listeners when the component unmounts using CometChat.removeGroupListener(listenerId). Failing to remove listeners can cause memory leaks and duplicate event handling.
On EventonMemberAddedToGroup listener receives the following parameters:Listener Parameters:
ParameterTypeDescriptionSample Value
messageobjectAction message objectSee message Object ↓
userAddedobjectUser who was addedSee userAdded Object ↓
userAddedByobjectUser who performed the addSee userAddedBy Object ↓
userAddedInobjectGroup to which user was addedSee userAddedIn Object ↓

message Object:
ParameterTypeDescriptionSample Value
idstringUnique message ID"25544"
conversationIdstringConversation ID"group_group_1772430752261"
typestringMessage type"groupMember"
receiverTypestringReceiver type"group"
categorystringMessage category"action"
actionstringAction performed"added"
messagestringHuman-readable action message"Henry Marino added Ronald Jerry"
sentAtnumberUnix timestamp when sent1772430763
updatedAtnumberUnix timestamp when updated1772430763

userAdded Object:
ParameterTypeDescriptionSample Value
uidstringUnique identifier of the added user"cometchat-uid-6"
namestringDisplay name"Ronald Jerry"
avatarstringURL to avatar image"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-6.webp"
statusstringOnline status"online"
rolestringUser’s role"default"
lastActiveAtnumberUnix timestamp of last activity1772430654
hasBlockedMebooleanWhether this user has blocked the current userfalse
blockedByMebooleanWhether the current user has blocked this userfalse
deactivatedAtnumberTimestamp when deactivated (0 if active)0

userAddedBy Object:
ParameterTypeDescriptionSample Value
uidstringUnique identifier of the user who added"cometchat-uid-7"
namestringDisplay name"Henry Marino"
avatarstringURL to avatar image"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-7.webp"
statusstringOnline status"online"
rolestringUser’s role"default"
lastActiveAtnumberUnix timestamp of last activity1772430649
hasBlockedMebooleanWhether this user has blocked the current userfalse
blockedByMebooleanWhether the current user has blocked this userfalse
deactivatedAtnumberTimestamp when deactivated (0 if active)0

userAddedIn Object:
ParameterTypeDescriptionSample Value
guidstringUnique identifier of the group"group_1772430752261"
namestringName of the group"Hike"
typestringType of the group"public"
membersCountnumberTotal members in the group2
hasJoinedbooleanWhether logged-in user has joinedfalse
isBannedbooleanWhether logged-in user is bannedfalse
conversationIdstringConversation ID for the group"group_group_1772430752261"
createdAtnumberUnix timestamp when created1772430752
ownerstringUID of the group owner"cometchat-uid-7"
onlineMembersCountnumberNumber of online members1

Member Added to Group event in Message History

In other words, as a member of a group, how do I know when someone is added to the group when my app is not running? When you retrieve the list of previous messages if a member has been added to any group that the logged-in user is a member of, the list of messages will contain an Action message. An Action message is a sub-class of BaseMessage class. For the group member added event, in the Action object received, the following fields can help you get the relevant information-
  1. action - added
  2. actionOn - User object containing the details of the user who was added to the group
  3. actionBy - User object containing the details of the user who added the member to the group
  4. actionFor - Group object containing the details of the group to which the member was added

Best Practices

The addMembersToGroup() response includes per-UID results (“success” or error). Check each result rather than assuming all members were added successfully.
The logged-in user must have Admin or Moderator scope to add members. Verify scope before attempting the operation.

Troubleshooting

Verify the logged-in user has Admin or Moderator scope in the group. Participants cannot add members.
Check the per-UID results in the response object. Common causes include invalid UIDs or users that don’t exist in your CometChat app.
Ensure the group listener is registered before the add event occurs. Also verify the listenerId is unique.

Next Steps