Quick Reference - Add members to a group:
Add Members to Group
You can add members to the group using theaddMembersToGroup() method. This method takes the below parameters:
GUID- GUID of the group the members are to be added to.members- This is a list ofGroupMemberobjects. In order to add members, you need to create an object of theGroupMemberclass. The UID and the scope of theGroupMemberare mandatory.bannedMembers- This is the list ofUID'sthat need to be banned from the Group. This can be set tonullif there are no members to be banned.
- JavaScript
- TypeScript
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.
Response
Response
On Success —
Example:
addMembersToGroup() returns an object with each UID as key and result as value:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
[UID] | string | Result for each UID (“success” or error message) | "success" |
| Parameter | Type | Description | Sample Value |
|---|---|---|---|
cometchat-uid-7 | string | Result 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.
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.
- JavaScript
- TypeScript
Response (onMemberAddedToGroup)
Response (onMemberAddedToGroup)
On Event —
onMemberAddedToGroup listener receives the following parameters:Listener Parameters:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
message | object | Action message object | See message Object ↓ |
userAdded | object | User who was added | See userAdded Object ↓ |
userAddedBy | object | User who performed the add | See userAddedBy Object ↓ |
userAddedIn | object | Group to which user was added | See userAddedIn Object ↓ |
message Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
id | string | Unique message ID | "25544" |
conversationId | string | Conversation ID | "group_group_1772430752261" |
type | string | Message type | "groupMember" |
receiverType | string | Receiver type | "group" |
category | string | Message category | "action" |
action | string | Action performed | "added" |
message | string | Human-readable action message | "Henry Marino added Ronald Jerry" |
sentAt | number | Unix timestamp when sent | 1772430763 |
updatedAt | number | Unix timestamp when updated | 1772430763 |
userAdded Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the added user | "cometchat-uid-6" |
name | string | Display name | "Ronald Jerry" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-6.webp" |
status | string | Online status | "online" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1772430654 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when deactivated (0 if active) | 0 |
userAddedBy Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user who added | "cometchat-uid-7" |
name | string | Display name | "Henry Marino" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-7.webp" |
status | string | Online status | "online" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1772430649 |
hasBlockedMe | boolean | Whether this user has blocked the current user | false |
blockedByMe | boolean | Whether the current user has blocked this user | false |
deactivatedAt | number | Timestamp when deactivated (0 if active) | 0 |
userAddedIn Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
guid | string | Unique identifier of the group | "group_1772430752261" |
name | string | Name of the group | "Hike" |
type | string | Type of the group | "public" |
membersCount | number | Total members in the group | 2 |
hasJoined | boolean | Whether logged-in user has joined | false |
isBanned | boolean | Whether logged-in user is banned | false |
conversationId | string | Conversation ID for the group | "group_group_1772430752261" |
createdAt | number | Unix timestamp when created | 1772430752 |
owner | string | UID of the group owner | "cometchat-uid-7" |
onlineMembersCount | number | Number of online members | 1 |
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 anAction 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-
action-addedactionOn- User object containing the details of the user who was added to the groupactionBy- User object containing the details of the user who added the member to the groupactionFor- Group object containing the details of the group to which the member was added
Best Practices
Check per-UID results after adding
Check per-UID results after adding
The
addMembersToGroup() response includes per-UID results (“success” or error). Check each result rather than assuming all members were added successfully.Only Admins and Moderators can add members
Only Admins and Moderators can add members
The logged-in user must have Admin or Moderator scope to add members. Verify scope before attempting the operation.
Troubleshooting
addMembersToGroup fails with permission error
addMembersToGroup fails with permission error
Verify the logged-in user has Admin or Moderator scope in the group. Participants cannot add members.
Some UIDs return error in response
Some UIDs return error in response
Check the per-UID results in the response object. Common causes include invalid UIDs or users that don’t exist in your CometChat app.
onMemberAddedToGroup not firing
onMemberAddedToGroup not firing
Ensure the group listener is registered before the add event occurs. Also verify the
listenerId is unique.