Skip to main content
Quick Reference for AI Agents & Developers
  • Build request: ConversationRequest.ConversationRequestBuilder(limit:).build()
  • Fetch conversations: conversationsRequest.fetchNext(onSuccess:onError:)
  • Filters: .setConversationType(conversationType:), .setTags(_:), .withUserAndGroupTags()
  • Related: Delete Conversation · Messaging Overview
CometChat Allows you to fetch the list of conversations the logged-in user is a part of. This list of conversations consists of both user and group conversations.

Object Structures

Conversation Object

The Conversation object represents a conversation in CometChat.
ParameterTypeDescription
conversationIdStringUnique conversation identifier
conversationTypeCometChat.ConversationType.user (0) or .group (1)
lastMessageBaseMessageLast message in the conversation
conversationWithUser or GroupUser or Group object
unreadMessageCountIntNumber of unread messages
unreadMentionsCountIntNumber of unread mentions
lastReadMessageIdIntID of the last read message
updatedAtDoubleUnix timestamp of last update
tags[String]?Array of conversation tags
For other object structures, see Send Message - Object Structures.

Retrieve List of Conversations

In other words, as a logged-in user, how do I retrieve the latest conversations that I’ve been a part of? To fetch the list of conversations, you can use the ConversationsRequest class. To use this class i.e. to create an object of the ConversationsRequest class, you need to use the ConversationsRequestBuilder class. The ConversationsRequestBuilder class allows you to set the parameters based on which the conversations are to be fetched.

Set Limit

This method sets the limit i.e. the number of conversations that should be fetched in a single iteration.
var conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: 30)
  .build()
A Maximum of only 50 Conversations can be fetched at once. If you want to fetch more conversations, you can use the fetchNext() on the same conversationsRequest object.

Set Conversation Type

This method can be used to fetch user or group conversations specifically. The conversationType variable can hold one of the below two values:
  • CometChat.ConversationType.user - Only fetches user conversations.
  • CometChat.ConversationType.group - Only fetches group conversations.
If none is set, the list of conversations will include both user and group conversations.
var conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: 50)
  .setConversationType(conversationType: .user)
  .build()

With User and Group Tags

This method can be used to fetch the user/group tags in the Conversation Object. By default the value is false.
var conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: 50)
  .withUserAndGroupTags(true)
  .build()

Set User Tags

This method fetches user conversations which have the specified tags.
let limit = 30
let userTags = ["tag1"]
let conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: limit)
  .setUserTags(userTags)
  .build()

Set Group Tags

This method fetches group conversations which have the specified tags.
let limit = 30
let groupTags = ["tag1"]
let conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: limit)
  .setGroupTags(groupTags)
  .build()

With Tags

This method makes sure that the tags associated with the conversations are returned along with the other details of the conversations. The default value for this parameter is false.
var conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: 50)
  .withTags(true)
  .build()

Set Tags

This method helps you fetch the conversations based on the specified tags.
let tags = ["pinned", "archived"]
var conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: 50)
  .setTags(tags)
  .build()

Include Blocked Users

This method helps you fetch the conversations of users whom the logged-in user has blocked.
var conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: 50)
  .includeBlockedUsers(true)
  .build()

With Blocked Info

This method helps you fetch the blocked information in the Conversation object.
var conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: 50)
  .withBlockedInfo(true)
  .build()

Search Conversations

This method helps you search a conversation based on User or Group name.
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Custom plans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
var conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: 50)
  .setSearchKeyword("Hiking")
  .build()

Unread Conversations

This method helps you fetch unread conversations.
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Custom plans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
var conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: 50)
  .setUnread(true)
  .build()

Fetch Conversations

Finally, once all the parameters are set to the builder class, you need to call the build() method to get the object of the ConversationsRequest class. Once you have the object of the ConversationsRequest class, you need to call the fetchNext() method. Calling this method will return a list of Conversation objects containing X number of conversations depending on the limit set.
let convRequest = ConversationRequest.ConversationRequestBuilder(limit: 20)
  .setConversationType(conversationType: .user)
  .build()

convRequest.fetchNext(onSuccess: { (conversationList) in
  print("success of convRequest \(conversationList)")            
}) { (exception) in
  print("here exception \(String(describing: exception?.errorDescription))")
}
The Conversation object consists of the following fields:
FieldInformation
conversationIdID of the conversation
conversationTypeType of conversation (user/group)
lastMessageLast message in the conversation
conversationWithUser or Group object containing the details of the user or group
unreadMessageCountUnread message count for the conversation
Method: conversationsRequest.fetchNext(onSuccess:onError:)Object Type: ConversationRequest
ParameterTypeValue
limitInt20
conversationTypeCometChat.ConversationType?0 (.user)
Object Type: Conversation
ParameterTypeValue
conversationIdString"group_cometchat-guid-101"
conversationTypeCometChat.ConversationType1 (.group)
unreadMessageCountInt12
unreadMentionsCountInt2
lastReadMessageIdInt37650
updatedAtDouble1771915000.0
tags[String]?nil
lastMessage (TextMessage):
ParameterTypeValue
idInt37662
muidString""
conversationIdString"group_cometchat-guid-101"
senderUidString"cometchat-uid-4"
receiverUidString"cometchat-guid-101"
receiverTypeCometChat.ReceiverType1 (.group)
messageTypeCometChat.MessageType0 (.text)
messageCategoryCometChat.MessageCategory0 (.message)
sentAtInt1771915000
textString"Team meeting at 3 PM"
conversationWith (Group):
ParameterTypeValue
guidString"cometchat-guid-101"
nameString"Development Team"
iconString"https://assets.cometchat.io/sampleapp/v2/groups/cometchat-guid-101.webp"
descriptionString"Team for development discussions"
typeCometChat.GroupType"public"
ownerString"cometchat-uid-1"
membersCountInt15
createdAtDouble1700000000.0
updatedAtDouble1771915000.0
hasJoinedBooltrue
joinedAtDouble1705000000.0
scopeCometChat.GroupMemberScope"participant"
metadata[String: Any]?nil
tags[String]?nil

Tag Conversation

In other words, as a logged-in user, how do I tag a conversation? To tag a specific conversation, you can use the tagConversation() method. The tagConversation() method accepts three parameters:
  1. conversationWith: UID/GUID of the user/group whose conversation you want to tag.
  2. conversationType: The conversationType variable can hold one of the below two values:
    • user - Only fetches user conversation.
    • group - Only fetches group conversations.
  3. tags: The tags variable will be a list of tags you want to add to a conversation.
let id = "cometchat-uid-1" // id of the user/group
let tags = ["pinned"]

CometChat.tagConversation(conversationWith: id, conversationType: .user, tags: tags, onSuccess: { conversation in
  print("conversation", conversation)
}, onError: { error in
  print("error", error)
})
The tags for conversations are one-way. This means that if user A tags a conversation with user B, that tag will be applied to that conversation only for user A.
Method: CometChat.tagConversation(conversationWith:conversationType:tags:onSuccess:onError:)
ParameterTypeValue
conversationWithString"cometchat-uid-3"
conversationTypeCometChat.ConversationType0 (.user)
tags ([String]):
IndexTypeValue
0String"pinned"

Retrieve Single Conversation

In other words, as a logged-in user, how do I retrieve a specific conversation? To fetch a specific conversation, you can use the getConversation method. The getConversation method accepts two parameters:
  1. conversationWith: UID/GUID of the user/group whose conversation you want to fetch.
  2. conversationType: The conversationType variable can hold one of the below two values:
    • user - Only fetches user conversation.
    • group - Only fetches group conversations.
CometChat.getConversation(conversationWith: "cometchat-uid-3", conversationType: .user, onSuccess: { conversation in
  print("success \(String(describing: conversation?.stringValue()))")
}) { error in
  print("error \(String(describing: error?.errorDescription))")
}
Method: CometChat.getConversation(conversationWith:conversationType:onSuccess:onError:)
ParameterTypeValue
conversationWithString"cometchat-uid-3"
conversationTypeCometChat.ConversationType0 (.user)
Object Type: Conversation
ParameterTypeValue
conversationIdString"group_cometchat-guid-101"
conversationTypeCometChat.ConversationType1 (.group)
unreadMessageCountInt12
unreadMentionsCountInt2
lastReadMessageIdInt37650
updatedAtDouble1771915000.0
tags[String]?nil
lastMessage (TextMessage):
ParameterTypeValue
idInt37662
muidString""
conversationIdString"group_cometchat-guid-101"
senderUidString"cometchat-uid-4"
receiverUidString"cometchat-guid-101"
receiverTypeCometChat.ReceiverType1 (.group)
messageTypeCometChat.MessageType0 (.text)
sentAtInt1771915000
textString"Team meeting at 3 PM"
conversationWith (Group):
ParameterTypeValue
guidString"cometchat-guid-101"
nameString"Development Team"
iconString"https://assets.cometchat.io/sampleapp/v2/groups/cometchat-guid-101.webp"
descriptionString"Team for development discussions"
typeCometChat.GroupType"public"
ownerString"cometchat-uid-1"
membersCountInt15
hasJoinedBooltrue
scopeCometChat.GroupMemberScope"participant"

Get Conversation From Message

For real-time events, you will always receive Message objects and not Conversation objects. Thus, you will need a mechanism to convert the Message object to a Conversation object. You can use the getConversationFromMessage() method of the CometChat class.
let myConversation = CometChat.getConversationFromMessage(message)
While converting a Message object to a Conversation object, the unreadMessageCount & tags will not be available in the Conversation object. The unread message count needs to be managed in your client-side code.
Method: CometChat.getConversationFromMessage(_:)Input Object Type: TextMessage
ParameterTypeValue
idInt37798
muidString""
conversationIdString"cometchat-uid-2_user_cometchat-uid-3"
senderUidString"cometchat-uid-3"
receiverUidString"cometchat-uid-2"
receiverTypeCometChat.ReceiverType0 (.user)
messageTypeCometChat.MessageType0 (.text)
messageCategoryCometChat.MessageCategory0 (.message)
sentAtInt1771917734
textString"Hello"

Common Error Codes

Error CodeDescriptionResolution
ERR_NOT_LOGGED_INUser is not logged inCall CometChat.login() first
ERR_CONVERSATION_NOT_FOUNDConversation does not existVerify the conversationWith and conversationType parameters
ERR_INVALID_CONVERSATION_TYPEInvalid conversation type providedUse .user or .group
ERR_SDK_NOT_INITIALIZEDSDK not initializedCall CometChat.init() first
ERR_NETWORK_ERRORNetwork connectivity issueCheck internet connection and retry
ERR_FEATURE_NOT_ENABLEDFeature not enabled for your planEnable the feature from CometChat Dashboard or upgrade plan