Retrieve Conversations

Learn how to build a recent chats list using 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:

  1. setLimit(int limit) - This method sets the limit i.e. the number of conversations that should be fetched in a single iteration.
ConversationsRequest conversationRequest = ConversationsRequest.ConversationsRequestBuilder()
.set(limit: 50)
.build();
  1. setConversationType(String conversationType) - This method can be used to fetch user or group conversations specifically. The conversationType variable can hold one of the below two values:
    a. CometChatConstants.CONVERSATION_TYPE_USER (user) - Only fetches one-on-one conversations.
    b. CometChatConstants.CONVERSATION_TYPE_GROUP (group) - Only fetches group conversations.

If neither are set, the list of conversations will include both user and group conversations.

ConversationsRequest conversationsRequest = new ConversationsRequest.ConversationsRequestBuilder()
.setLimit(50)
.setConversationType(CometChatConstants.CONVERSATION_TYPE_USER)
.build();
  1. withUserAndGroupTags(boolean withUserAndGroupTags) - This method can be used to fetch the user/group tags in the Conversation Object. By default the value is false
ConversationsRequest conversationsRequest = new ConversationsRequest.ConversationsRequestBuilder()
.setLimit(50)
.withUserAndGroupTags(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 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.

A Maximum of only 50 Conversations can be fetched at once.

ConversationsRequest conversationsRequest = new ConversationsRequest.ConversationsRequestBuilder().setLimit(50).build();

conversationsRequest.fetchNext(new CometChat.CallbackListener<List<Conversation>>() {
  @Override
    public void onSuccess(List<Conversation> conversations) {
    // Hanlde list of conversations
                       
  }

  @Override
    public void onError(CometChatException e) {
    // Hanlde failure
  }
});
var conversationRequest: ConversationsRequest? = null
val LIMIT:Int=30
  
conversationRequest = ConversationsRequest.ConversationsRequestBuilder().setLimit(LIMIT).build()
conversationRequest?.fetchNext(object : CometChat.CallbackListener<List<Conversation>>() {
            override fun onSuccess(p0: List<Conversation>?) {
                //Handle List of Conversations
            }
            override fun onError(p0: CometChatException?) {
                //Handle Failure
            }
        })

The Conversationobject consists of the below fields:

Field

Information

conversationId

ID of the conversation

conversationType

Type of conversation (user/group)

lastMessage

Last message the conversation

conversationWith

User or Group object containing the details of the user or group

unreadMessageCount

Unread message count for the conversation

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 a Conversation object. You can use the getConversationFromMessage method for this purpose.

Conversation conversation = CometChatHelper.getConversationFromMessage(message);

❗️

While converting a 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.


Did this page help you?