> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-v6-beta2-flutter-uikit.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Add Members To A Group

> Add members to a group, listen for real-time member added events, and handle missed events using the CometChat React Native SDK.

<Info>
  **Quick Reference** - Add members to a group:

  ```javascript theme={null}
  const members = [
    new CometChat.GroupMember("UID", CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT),
  ];
  await CometChat.addMembersToGroup("GUID", members, []);
  ```
</Info>

<Note>
  **Available via:** [SDK](/sdk/react-native/group-add-members) | [REST API](/rest-api/group-members/add-members)
</Note>

## 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.

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    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);
      }
    );
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let GUID: string = "GUID";
    let UID: string = "UID";
    let membersList: CometChat.GroupMember[] = [
      new CometChat.GroupMember(UID, CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT),
    ];

    CometChat.addMembersToGroup(GUID, membersList, []).then(
      (response: Object) => {
        console.log("response", response);
      },
      (error: CometChat.CometChatException) => {
        console.log("Something went wrong", error);
      }
    );
    ```
  </Tab>
</Tabs>

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.

<Accordion title="Response">
  **On Success** — `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"`  |

  **Example:**

  | Parameter         | Type   | Description                 | Sample Value |
  | ----------------- | ------ | --------------------------- | ------------ |
  | `cometchat-uid-7` | string | Result for the added member | `"success"`  |
</Accordion>

## 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?*

<Note>
  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.
</Note>

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.

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    var listenerID = "UNIQUE_LISTENER_ID";

    CometChat.addGroupListener(
      listenerID,
      new CometChat.GroupListener({
        onMemberAddedToGroup: (message, userAdded, userAddedBy, userAddedIn) => {
          console.log("User joined", {
            message,
            userAdded,
            userAddedBy,
            userAddedIn,
          });
        },
      })
    );
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    var listenerID: string = "UNIQUE_LISTENER_ID";

    CometChat.addGroupListener(
      listenerID,
      new CometChat.GroupListener({
        onMemberAddedToGroup: (
          message: CometChat.Action,
          userAdded: CometChat.User,
          userAddedBy: CometChat.User,
          userAddedIn: CometChat.Group
        ) => {
          console.log("User joined", {
            message,
            userAdded,
            userAddedBy,
            userAddedIn,
          });
        },
      })
    );
    ```
  </Tab>
</Tabs>

<Warning>
  Always remove group listeners when the component unmounts using `CometChat.removeGroupListener(listenerId)`. Failing to remove listeners can cause memory leaks and duplicate event handling.
</Warning>

<Accordion title="Response (onMemberAddedToGroup)">
  **On Event** — `onMemberAddedToGroup` listener receives the following parameters:

  <span id="on-member-added-response" style={{scrollMarginTop: '100px'}} />

  **Listener Parameters:**

  | Parameter     | Type   | Description                   | Sample Value                                        |
  | ------------- | ------ | ----------------------------- | --------------------------------------------------- |
  | `message`     | object | Action message object         | [See `message` Object ↓](#added-message-object)     |
  | `userAdded`   | object | User who was added            | [See `userAdded` Object ↓](#user-added-object)      |
  | `userAddedBy` | object | User who performed the add    | [See `userAddedBy` Object ↓](#user-added-by-object) |
  | `userAddedIn` | object | Group to which user was added | [See `userAddedIn` Object ↓](#user-added-in-object) |

  ***

  <span id="added-message-object" style={{scrollMarginTop: '100px'}} />

  **`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`                        |

  ***

  <span id="user-added-object" style={{scrollMarginTop: '100px'}} />

  **`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`                                                                     |

  ***

  <span id="user-added-by-object" style={{scrollMarginTop: '100px'}} />

  **`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`                                                                     |

  ***

  <span id="user-added-in-object" style={{scrollMarginTop: '100px'}} />

  **`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`                           |
</Accordion>

## 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

<AccordionGroup>
  <Accordion title="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.
  </Accordion>

  <Accordion title="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.
  </Accordion>
</AccordionGroup>

## Troubleshooting

<AccordionGroup>
  <Accordion title="addMembersToGroup fails with permission error">
    Verify the logged-in user has Admin or Moderator scope in the group. Participants cannot add members.
  </Accordion>

  <Accordion title="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.
  </Accordion>

  <Accordion title="onMemberAddedToGroup not firing">
    Ensure the group listener is registered before the add event occurs. Also verify the `listenerId` is unique.
  </Accordion>
</AccordionGroup>

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Retrieve Group Members" icon="users" href="/sdk/react-native/retrieve-group-members">
    Fetch group member lists with filtering and pagination
  </Card>

  <Card title="Kick/Ban Members" icon="gavel" href="/sdk/react-native/group-kick-ban-members">
    Kick, ban, and unban group members
  </Card>

  <Card title="Change Member Scope" icon="shield" href="/sdk/react-native/group-change-member-scope">
    Update member roles within a group
  </Card>

  <Card title="Join a Group" icon="right-to-bracket" href="/sdk/react-native/join-group">
    Join public or password-protected groups
  </Card>
</CardGroup>
