Skip to main content
Quick Reference - Join a group:
// 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");
Available via: SDK | REST API

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.
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);
}
);
The joinGroup() method takes the below parameters
ParameterDescription
GUIDThe GUID of the group you would like to join.
groupTypeType 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
passwordPassword 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. 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.
CometChat.addGroupListener(
  "UNIQUE_LISTNER_ID",
  new CometChat.GroupListener({
      onGroupMemberJoined: (message, joinedUser, joinedGroup) => {
          console.log("User joined", { message, joinedUser, joinedGroup });
      }
  })
);
Always remove group listeners when the component unmounts using CometChat.removeGroupListener(listenerId). Failing to remove listeners can cause memory leaks and duplicate event handling.

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

Before calling joinGroup(), check the hasJoined property on the Group object. If the user has already joined, calling joinGroup() again will return an error.
For password-protected groups, prompt the user for the password before calling joinGroup(). Pass the password as the third parameter.

Troubleshooting

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().
Ensure the password string matches exactly. Passwords are case-sensitive. If the user enters an incorrect password, the SDK returns an error.
Verify the group listener is registered with addGroupListener() before the join event occurs. Also ensure the listenerId is unique and hasn’t been overwritten.

Next Steps