Quick Reference for AI Agents & Developers
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 theConversationsRequest 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.
The ConversationsRequestBuilder class allows you to set the below parameters:
Set Limit
This method sets the limit i.e. the number of conversations that should be fetched in a single iteration.- JavaScript
- TypeScript
Set Conversation Type
This method can be used to fetch user or group conversations specifically. TheconversationType variable can hold one of the below two values:
- user - Only fetches user conversation.
- group - Only fetches group conversations.
- JavaScript
- TypeScript
With User and Group Tags
This method can be used to fetch the user/group tags in theConversation Object. By default the value is false.
- JavaScript
- TypeScript
Set User Tags
This method fetches user conversations that have the specified tags.- JavaScript
- TypeScript
Set Group Tags
This method fetches group conversations that have the specified tags.- JavaScript
- TypeScript
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 isfalse.
- JavaScript
- TypeScript
Set Tags
This method helps you fetch the conversations based on the specified tags.- JavaScript
- TypeScript
Include Blocked Users
This method helps you fetch the conversations of users whom the logged-in user has blocked.- JavaScript
- TypeScript
With Blocked Info
This method can be used to fetch the blocked information of the blocked user in theConversationWith object.
- JavaScript
- TypeScript
Search Conversations
This method helps you search for a conversation based on a 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)- JavaScript
- TypeScript
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)- JavaScript
- TypeScript
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 users depending on the limit set.
A Maximum of only 50 Conversations can be fetched at once.
- JavaScript
- TypeScript
Conversation Object consists of the following fields:
| Field | Information |
|---|---|
| conversationId | ID of the conversation. |
| conversationType | Type of conversation. (user/group) |
| lastMessage | Last message in the conversation. |
| conversationWith | User or Group object containing the details of the user or group. |
| unreadMessageCount | Unread message count for the conversation. |
Tag Conversation
In other words, as a logged-in user, how do I tag a conversation? To tag a specific conversation, you can use thetagConversation() method. The tagConversation() method accepts three parameters.
-
conversationWith: UID/GUID of the user/group whose conversation you want to tag. -
conversationType: TheconversationTypevariable can hold one of the below two values:- user - Only fetches user conversation.
- group - Only fetches group conversations.
-
tags: Thetagsvariable will be a list of tags you want to add to a conversation.
- JavaScript
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.
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 thegetConversation method. The getConversation method accepts two parameters.
conversationWith: UID/GUID of the user/group whose conversation you want to fetch.conversationType: TheconversationTypevariable can hold one of the below two values:
- user - Only fetches user conversation.
- group - Only fetches group conversations.
- JavaScript
Convert Messages to Conversations
As per our receive messages guide, for real-time messages, you will always receiveMessage 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(BaseMessage message) method of the CometChatHelper class.
- JavaScript
While converting the
Message object to the 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.Best Practices & Troubleshooting
Use pagination for large conversation lists
Use pagination for large conversation lists
Always use
fetchNext() in a loop or on-scroll handler to paginate through conversations. Fetching all conversations at once is not supported — the maximum per request is 50. Store the ConversationsRequest object and call fetchNext() repeatedly until it returns an empty list.Keep conversation lists updated in real time
Keep conversation lists updated in real time
Use real-time message listeners to receive new messages, then call
CometChatHelper.getConversationFromMessage() to convert incoming messages into Conversation objects and update your list. Note that unreadMessageCount and tags are not available when converting from a message — manage those on the client side.Filter conversations effectively with tags
Filter conversations effectively with tags
Conversations list is empty or missing expected results
Conversations list is empty or missing expected results
Ensure the logged-in user has actually exchanged messages with the expected users or groups. Conversations only appear once at least one message has been sent. Also verify that any filters (conversation type, tags, unread) are not excluding the results you expect.
Search or unread filter not working
Search or unread filter not working
The
setSearchKeyword() and setUnread() methods require the Conversation & Advanced Search feature, which is only available on Advanced and Custom plans. Enable it from the CometChat Dashboard under Chats → Settings → General Configuration.