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

# Join A Group

> Join public or password-protected groups, listen for real-time join events, and handle missed join events using the CometChat React Native SDK.

<Info>
  **Quick Reference** - Join a group:

  ```javascript theme={null}
  // Join a public group
  await CometChat.joinGroup("GUID", CometChat.GROUP_TYPE.PUBLIC, "");

  // Join a password-protected group
  await CometChat.joinGroup("GUID", CometChat.GROUP_TYPE.PASSWORD, "password123");
  ```
</Info>

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

## Join a Group

In order to start participating in group conversations, you will have to join a group. You can do so using the `joinGroup()` method.

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    var GUID = "GUID";
    var password = "";
    var groupType = CometChat.GROUP_TYPE.PUBLIC;

    CometChat.joinGroup(GUID, groupType, password).then(
    group => {
      console.log("Group joined successfully:", group);
    }, error => {
      console.log("Group joining failed with exception:", error);
    }
    );
    ```
  </Tab>

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

    CometChat.joinGroup(GUID, CometChat.GroupType.Public).then(
      (group: CometChat.Group) => {
          console.log("Group joined successfully:", group);
      }, (error: CometChat.CometChatException) => {
          console.log("Group joining failed with exception:", error);
      }
    );
    ```
  </Tab>
</Tabs>

The `joinGroup()` method takes the below parameters

| Parameter   | Description                                                                                                                                                      |
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `GUID`      | The GUID of the group you would like to join.                                                                                                                    |
| `groupType` | Type of the group. CometChat provides 3 types of groups viz. 1. CometChat.GROUP\_TYPE.PUBLIC 2. CometChat.GROUP\_TYPE.PASSWORD 3. CometChats.GROUP\_TYPE.PRIVATE |
| `password`  | Password is mandatory in case of a password protected group.                                                                                                     |

Once you have joined a group successfully, you can send and receive messages in that group.

<Accordion title="Response">
  **On Success** — `joinGroup()` returns a `Group` object:

  | Parameter            | Type    | Description                                         | Sample Value                  |
  | -------------------- | ------- | --------------------------------------------------- | ----------------------------- |
  | `hasJoined`          | boolean | Whether the logged-in user has joined the group     | `true`                        |
  | `membersCount`       | number  | Total number of members in the group                | `2`                           |
  | `isBanned`           | boolean | Whether the logged-in user is banned from the group | `false`                       |
  | `guid`               | string  | Unique identifier of the group                      | `"group_1772427551785"`       |
  | `name`               | string  | Name of the group                                   | `"Comet Group"`               |
  | `type`               | string  | Type of the group (public, private, password)       | `"public"`                    |
  | `scope`              | string  | Scope of the logged-in user in the group            | `"participant"`               |
  | `joinedAt`           | number  | Unix timestamp when the user joined                 | `1772427667`                  |
  | `conversationId`     | string  | Conversation ID for the group                       | `"group_group_1772427551785"` |
  | `createdAt`          | number  | Unix timestamp when the group was created           | `1772427556`                  |
  | `owner`              | string  | UID of the group owner                              | `"cometchat-uid-6"`           |
  | `onlineMembersCount` | number  | Number of online members in the group               | `1`                           |
</Accordion>

CometChat keeps a track of the groups joined and you do not need to join the group every time you want to communicate in the group.

You can identify if a group is joined using the `hasJoined` parameter in the `Group` object.

## Real-time Group Member Joined Events

*In other words, as a member of a group, how do I know if someone joins the group when my app is running?*

If a user joins any group, the members of the group receive a real-time event in the `onGroupMemberJoined()` method of the `GroupListener` class.

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    CometChat.addGroupListener(
      "UNIQUE_LISTNER_ID",
      new CometChat.GroupListener({
          onGroupMemberJoined: (message, joinedUser, joinedGroup) => {
              console.log("User joined", { message, joinedUser, joinedGroup });
          }
      })
    );
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    CometChat.addGroupListener(
      "UNIQUE_LISTNER_ID",
      new CometChat.GroupListener({
          onGroupMemberJoined: (message: CometChat.Action, joinedUser: CometChat.User, joinedGroup: CometChat.Group) => {
              console.log("User joined", { message, joinedUser, joinedGroup });
          }
      })
    );
    ```
  </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>

## Missed Group Member Joined Events

*In other words, as a member of a group, how do I know if someone joins the group when my app is not running?*

When you retrieve the list of previous messages if a member has joined 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 joined event, in the `Action` object received, the following fields can help you get the relevant information-

1. `action` - `joined`
2. `actionBy` - User object containing the details of the user who joined the group
3. `actionFor`- Group object containing the details of the group the user has joined

## Best Practices

<AccordionGroup>
  <Accordion title="Check hasJoined before joining">
    Before calling `joinGroup()`, check the `hasJoined` property on the `Group` object. If the user has already joined, calling `joinGroup()` again will return an error.
  </Accordion>

  <Accordion title="Handle password-protected groups in your UI">
    For password-protected groups, prompt the user for the password before calling `joinGroup()`. Pass the password as the third parameter.
  </Accordion>
</AccordionGroup>

## Troubleshooting

<AccordionGroup>
  <Accordion title="joinGroup fails for private groups">
    Private groups cannot be joined directly. Members must be added by an admin or owner using the group members API. Only public and password-protected groups support `joinGroup()`.
  </Accordion>

  <Accordion title="Wrong password error for password group">
    Ensure the password string matches exactly. Passwords are case-sensitive. If the user enters an incorrect password, the SDK returns an error.
  </Accordion>

  <Accordion title="onGroupMemberJoined not firing">
    Verify the group listener is registered with `addGroupListener()` before the join event occurs. Also ensure the `listenerId` is unique and hasn't been overwritten.
  </Accordion>
</AccordionGroup>

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Leave a Group" icon="right-from-bracket" href="/sdk/react-native/leave-group">
    Leave groups and stop receiving updates
  </Card>

  <Card title="Group Members" icon="users" href="/sdk/react-native/group-members">
    Manage members, roles, and permissions within groups
  </Card>

  <Card title="Send Messages" icon="paper-plane" href="/sdk/react-native/send-message">
    Send text, media, and custom messages to groups
  </Card>

  <Card title="Retrieve Groups" icon="layer-group" href="/sdk/react-native/retrieve-groups">
    Fetch group lists and group details
  </Card>
</CardGroup>
