Skip to main content
Version: v2

Retrieve Conversations

Conversations provide the last messages for every one-on-one and group conversation the logged-in user is a part of. This makes it easy for you to build a Recent Chats list.

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?

In order 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.

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.

let limit = 30;
let conversationRequest = new CometChat.ConversationsRequestBuilder()
.setLimit(limit)
.build();

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:

  • user - Only fetches user conversation.
  • group - Only fetches group conversations.

If none is set, the list of conversations will include both user and group conversations.

let limit = 30;
let conversationType = "group";
let conversationRequest = new CometChat.ConversationsRequestBuilder()
.setLimit(limit)
.setConversationType(conversationType)
.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 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.

let limit = 30;
let conversationsRequest = new CometChat.ConversationsRequestBuilder()
.setLimit(limit)
.build();

conversationsRequest.fetchNext().then(
conversationList => {
console.log("Conversations list received:", conversationList);
}, error => {
console.log("Conversations list fetching failed with error:", error);
}
);

The Conversation Object consists of the below fields:

FieldInformation
conversationIdID of the conversation.
conversationTypeType of conversation. (user/group)
lastMessageLast message the conversation.
conversationWithUser or Group object containing the details of the user or group.
unreadMessageCountUnread message count for the conversation.

Retrieve Single Conversation

In other words, as a logged-in user, how do I retrieve a specific conversation?

In order 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', 'conversationType').then(
conversation => {
console.log('conversation', conversation);
}, error => {
console.log('error while fetching a conversation', error);
}
);

Convert Messages to Conversations

As per our receive messages guide, for real-time messages, you will always receive Message objects and not Conversation objects. Thus, you will need a mechanism to convert the Message object to Conversation object. You can use the getConversationFromMessage(BaseMessage message) of the CometChatHelper class.

CometChat.CometChatHelper.getConversationFromMessage(message).then(
conversation => {
console.log("Conversation Object", conversation);
}, error => {
console.log("Error while converting message object", error);
}
);
danger

While converting Message object to Conversation object, the unreadMessageCount will not be available in the Conversation object. As this is for the real-time events, the unread message count needs to be managed in your client-side code.