Join a Group
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.
- Join Group
- Typescript
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);
}
);
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);
}
);
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. a. CometChatConstants.GROUP_TYPE_PUBLIC (public) b. CometChatConstants.GROUP_TYPE___PASSWORD (password) c. CometChatConstants.GROUP_TYPE_PRIVATE (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.
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.
- Group Listener
- Typescript
CometChat.addGroupListener(
"UNIQUE_LISTNER_ID",
new CometChat.GroupListener({
onGroupMemberJoined: (message, joinedUser, joinedGroup) => {
console.log("User joined", { message, joinedUser, joinedGroup });
}
})
);
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 });
}
})
);
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-
action
-joined
actionBy
- User object containing the details of the user who joined the groupactionFor
- Group object containing the details of the group the user has joined