Skip to main content
Quick Reference for AI Agents & Developers
  • Build request: GroupsRequest.GroupsRequestBuilder().set(limit:).build()
  • Fetch groups: groupsRequest.fetchNext(onSuccess:onError:)
  • Get single group: CometChat.getGroup(GUID:onSuccess:onError:)
  • Online count: CometChat.getOnlineGroupMemberCount(_:onSuccess:onError:)
  • Filters: .set(searchKeyword:), .set(joinedOnly:), .set(tags:), .withTags(_:)
  • Related: Create Group · Join Group · Groups Overview

Group Data Model

The Group class represents a CometChat group with all its properties.
let group = Group(guid: "group123", name: "My Group", groupType: .public, password: nil)

Group Properties

PropertyTypeDescription
guidStringUnique group identifier (required)
nameString?Group display name
groupTypeGroupType.public, .private, or .password
iconString?Group icon URL
groupDescriptionString?Group description
ownerString?UID of group owner
metadata[String: Any]?Custom metadata dictionary
createdAtIntCreation Unix timestamp
updatedAtIntLast update Unix timestamp
hasJoinedBoolWhether logged-in user is a member
joinedAtIntWhen user joined (Unix timestamp)
membersCountIntTotal number of members
scopeMemberScopeUser’s scope in group
tags[String]Array of group tags
passwordString?Password (for password-protected groups)
Group Object Properties:
ParameterTypeDescription
guidStringUnique group identifier. Example: "cometchat-guid-1"
nameString?Group display name. Example: "Hiking Group"
groupTypeGroupTypeType of group. Example: .public
iconString?URL to the group’s icon image. Example: "https://assets.cometchat.io/sampleapp/v2/groups/cometchat-guid-1.webp"
groupDescriptionString?Description of the group. Example: "Explore, connect, and chat with fellow outdoor enthusiasts"
ownerString?UID of the group owner. Example: "cometchat-uid-5"
membersCountIntTotal number of members in the group. Example: 5
hasJoinedBoolWhether the logged-in user is a member. Example: true
joinedAtIntUnix timestamp when user joined. Example: 1753861429
scopeMemberScopeUser’s scope in the group. Example: .participant
createdAtIntUnix timestamp when group was created. Example: 1753861429
updatedAtIntUnix timestamp of last update. Example: 0
tags[String]Array of tags associated with the group. Example: ["general", "public"]
metadata[String: Any]?Custom metadata dictionary. Example: ["category": "social", "isVerified": true]
Metadata Properties (when present):
ParameterTypeDescription
metadata.categoryStringCategory of the group. Example: "social"
metadata.isVerifiedBoolWhether the group is verified. Example: true

Retrieve List of Groups

In other words, as a logged-in user, how do I retrieve the list of groups I’ve joined and groups that are available? In order to fetch the list of groups, you can use the GroupsRequest class. To use this class i.e to create an object of the GroupsRequest class, you need to use the GroupsRequestBuilder class. The GroupsRequestBuilder class allows you to set the parameters based on which the groups are to be fetched.

GroupsRequestBuilder Methods

MethodParameterReturnsDescription
init(limit:)IntGroupsRequestBuilderConstructor with limit
set(limit:)IntGroupsRequestBuilderNumber of groups to fetch (1-100)
set(searchKeyword:)StringGroupsRequestBuilderSearch in group name
set(joinedOnly:)BoolGroupsRequestBuilderOnly joined groups
set(tags:)[String]GroupsRequestBuilderFilter by group tags
withTags(_:)BoolGroupsRequestBuilderInclude tags in response
build()-GroupsRequestBuild the request object
Builder Configuration:
ParameterTypeDescription
limitIntMaximum number of groups to fetch per request. Range: 1-100. Example: 30
searchKeywordStringSearch string to filter groups by name. Example: "team"
joinedOnlyBoolWhether to return only groups the user has joined. Example: true
tags[String]Filter groups by tags. Example: ["premium", "verified"]
withTagsBoolInclude tags data in response. Example: true
Common Filter Combinations:
Use CaseBuilder Configuration
Fetch all groups.set(limit: 30).build()
Fetch only joined groups.set(limit: 30).set(joinedOnly: true).build()
Search groups by name.set(limit: 30).set(searchKeyword: "team").build()
Fetch groups with specific tags.set(limit: 30).set(tags: ["premium", "verified"]).build()
Fetch groups with tags data.set(limit: 30).withTags(true).build()
Combined filters.set(limit: 30).set(joinedOnly: true).set(searchKeyword: "project").withTags(true).build()
  • fetchNext() returns public and password groups by default
  • Private groups only returned if user is a member
  • Use set(joinedOnly: true) to get only groups user has joined

Set Limit

This method sets the limit i.e. the number of groups that should be fetched in a single iteration.
let groupsRequest = GroupsRequest.GroupsRequestBuilder()
    .set(limit: 30)
    .build()

Set Search Keyword

This method allows you to set the search string based on which the groups are to be fetched.
let groupsRequest = GroupsRequest.GroupsRequestBuilder()
    .set(searchKeyword: "abc")
    .set(limit: 30)
    .build()

Joined Only

This method when used, will ask the SDK to only return the groups that the user has joined or is a part of.
let groupsRequest = GroupsRequest.GroupsRequestBuilder()
    .set(joinedOnly: true)
    .set(limit: 30)
    .build()

Set Tags

This method accepts a list of tags based on which the list of groups is to be fetched. The list fetched will only contain the groups that have been tagged with the specified tags.
let groupsRequest = GroupsRequest.GroupsRequestBuilder()
    .set(limit: 30)
    .set(tags: ["tag1", "tag2"])
    .build()

With Tags

This property when set to true will fetch tags data along with the list of groups.
let groupsRequest = GroupsRequest.GroupsRequestBuilder()
    .set(limit: 30)
    .withTags(true)
    .build()
Finally, once all the parameters are set to the builder class, you need to call the build() method to get the object of the GroupsRequest class. Once you have the object of the GroupsRequest class, you need to call the fetchNext() method. Calling this method will return a list of Group objects containing n number of groups where n is the limit set in the builder class. The list of groups fetched will only have the public and password type groups. The private groups will only be available if the user is a member of the group.
let limit = 30

let groupsRequest = GroupsRequest.GroupsRequestBuilder(limit: limit).build()

groupsRequest.fetchNext(onSuccess: { (groups) in
    for group in groups {
        print("Group: \(group.stringValue())")
    }
}, onError: { (error) in
    print("Error: \(error?.errorDescription)")
})
Request Parameters:
ParameterTypeDescription
limitIntMaximum number of groups to fetch. Example: 30
Success Response (Array of Group Objects):
ParameterTypeDescription
guidStringUnique group identifier. Example: "_dbhbcdsh_b211ad"
nameString?Group display name. Example: "Dbhbcdsh"
groupTypeGroupTypeType of group. Example: .public
iconString?URL to the group’s icon image. Example: nil
groupDescriptionString?Description of the group. Example: nil
ownerString?UID of the group owner. Example: "sanket"
membersCountIntTotal number of members. Example: 9
hasJoinedBoolWhether the logged-in user is a member. Example: true
joinedAtIntUnix timestamp when user joined. Example: 1753861429
scopeMemberScopeUser’s scope in the group. Example: .participant
createdAtIntUnix timestamp when group was created. Example: 1753861429
tags[String]Array of tags. Example: []
metadata[String: Any]?Custom metadata dictionary. Example: [:]
Response Summary:
ParameterTypeDescription
typeStringResponse type. Example: "[Group]"
countIntNumber of groups returned. Example: 30
Sample Group Entries:
guidnamegroupTypemembersCounthasJoinedscopeowner
_dbhbcdsh_b211adDbhbcdsh.public9true.participantsanket
09f095a4-02e4-4097-9c1c-3df2620f61ffBcvdsghc.public4true.participantcometchat-uid-5
18f0078c-0f10-4fd5-9331-1a7676c8bcb2Bros.public8true.participantcometchat-uid-5
1ba8a356-dde5-4b09-aa36-24b5457e9274Bc dagh c.public3true.participantcometchat-uid-5
Error Response (CometChatException):
ParameterTypeDescription
errorCodeStringUnique error code. Example: "ERR_NOT_LOGGED_IN"
errorDescriptionStringHuman-readable error message. Example: "User is not logged in"
Request Parameters:
ParameterTypeDescription
limitIntMaximum number of groups to fetch. Example: 30
joinedOnlyBoolFilter to only joined groups. Example: true
Success Response (Array of Group Objects):
ParameterTypeDescription
guidStringUnique group identifier. Example: "_dbhbcdsh_b211ad"
nameString?Group display name. Example: "Dbhbcdsh"
groupTypeGroupTypeType of group. Example: .public
iconString?URL to the group’s icon image. Example: nil
ownerString?UID of the group owner. Example: "sanket"
membersCountIntTotal number of members. Example: 9
hasJoinedBoolAlways true for joined groups filter. Example: true
scopeMemberScopeUser’s scope in the group. Example: .participant
tags[String]Array of tags. Example: []
Response Summary:
ParameterTypeDescription
typeStringResponse type. Example: "[Group]"
countIntNumber of joined groups returned. Example: 30
filterStringApplied filter. Example: "joinedOnly = true"
Request Parameters:
ParameterTypeDescription
limitIntMaximum number of groups to fetch. Example: 30
withTagsBoolInclude tags data in response. Example: true
Success Response (Array of Group Objects):
ParameterTypeDescription
guidStringUnique group identifier. Example: "_dbhbcdsh_b211ad"
nameString?Group display name. Example: "Dbhbcdsh"
groupTypeGroupTypeType of group. Example: .public
membersCountIntTotal number of members. Example: 9
hasJoinedBoolWhether the logged-in user is a member. Example: true
scopeMemberScopeUser’s scope in the group. Example: .participant
ownerString?UID of the group owner. Example: "sanket"
tags[String]Array of tags (populated when withTags is true). Example: ["premium", "verified"]
Response Summary:
ParameterTypeDescription
typeStringResponse type. Example: "[Group]"
countIntNumber of groups returned. Example: 30
filterStringApplied filter. Example: "withTags = true"

Retrieve Particular Group Details

In other words, as a logged-in user, how do I retrieve information for a specific group? To get the information of a group, you can use the getGroup() method.
let guid = "cometchat-guid-1"

CometChat.getGroup(GUID: guid, onSuccess: { (group) in
    print("Group: \(group.stringValue())")
}, onError: { (error) in
    print("Error: \(error?.errorDescription)")
})
ParameterDescription
GUIDThe GUID of the group for whom the details are to be fetched.
On success, the Group object containing the details of the group is returned.
Request Parameters:
ParameterTypeDescription
GUIDStringUnique identifier of the group to fetch. Example: "cometchat-guid-1"
Success Response (Group Object):
ParameterTypeDescription
guidStringUnique group identifier. Example: "cometchat-guid-1"
nameString?Group display name. Example: "Hiking Group"
groupTypeGroupTypeType of group. Example: .private
iconString?URL to the group’s icon image. Example: "https://assets.cometchat.io/sampleapp/v2/groups/cometchat-guid-1.webp"
groupDescriptionString?Description of the group. Example: "Explore, connect, and chat with fellow outdoor enthusiasts, thanks to our CometChat-enabled community platform."
ownerString?UID of the group owner. Example: "cometchat-uid-5"
membersCountIntTotal number of members. Example: 5
hasJoinedBoolWhether the logged-in user is a member. Example: true
joinedAtIntUnix timestamp when user joined. Example: 1753861429
scopeMemberScopeUser’s scope in the group. Example: .participant
createdAtIntUnix timestamp when group was created. Example: 1753861429
updatedAtIntUnix timestamp of last update. Example: 0
tags[String]Array of tags. Example: []
metadata[String: Any]?Custom metadata dictionary. Example: [:]
Error Response (CometChatException):
ParameterTypeDescription
errorCodeStringUnique error code. Example: "ERR_GUID_NOT_FOUND"
errorDescriptionStringHuman-readable error message. Example: "Group with specified GUID does not exist"

Get Online Group Member Count

To get the total count of online users in particular groups, you can use the getOnlineGroupMemberCount() method.
let guids = ["cometchat-guid-1", "cometchat-guid-2"]

CometChat.getOnlineGroupMemberCount(guids, onSuccess: { countData in
    // countData: [String: Int] - GUID as key, count as value
    for (guid, count) in countData {
        print("Group \(guid): \(count) online members")
    }
}, onError: { error in
    print("Error: \(error?.errorDescription)")
})
This method returns a [String: Int] dictionary with the GUID as the key and the online member count for that group as the value.
Request Parameters:
ParameterTypeDescription
GUIDs[String]Array of group GUIDs to check. Example: ["cometchat-guid-1"]
Success Response ([String: Int] Dictionary):
ParameterTypeDescription
IntKey is the GUID, value is the online member count. Example: "cometchat-guid-1": 0
Response Format:
GUIDOnline Members
cometchat-guid-10
cometchat-guid-25
cometchat-guid-312
Error Response (CometChatException):
ParameterTypeDescription
errorCodeStringUnique error code. Example: "ERR_NOT_LOGGED_IN"
errorDescriptionStringHuman-readable error message. Example: "User is not logged in"

GroupType Enum

ValueDescription
.publicAnyone can join without approval
.privateRequires invitation or approval to join
.passwordRequires password to join

MemberScope Enum

ValueDescription
.adminFull control over group
.moderatorCan manage members and messages
.participantRegular member with basic permissions

Common Error Codes

Error CodeDescriptionResolution
ERR_NOT_LOGGED_INUser is not logged inLogin first using CometChat.login()
ERR_GUID_NOT_FOUNDGroup with specified GUID does not existVerify the GUID is correct
ERR_INVALID_LIMITInvalid limit value providedUse a limit between 1-100
ERR_ALREADY_JOINEDUser is already a member of the groupCheck membership before joining
ERR_NOT_A_MEMBERUser is not a member of the groupJoin the group first