> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-feature-react-native-sdk-quotedmessage-a.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 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 Chat** 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?*

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.

<Tabs>
  <Tab title="Set Limit">
    ```javascript theme={null}
    let limit = 30;
    let conversationRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30,
      conversationRequest: CometChat.ConversationsRequest = new CometChat.ConversationsRequestBuilder()
        .setLimit(limit)
        .build();
    ```
  </Tab>
</Tabs>

### 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.

<Tabs>
  <Tab title="Set Conversation Type">
    ```javascript theme={null}
    let limit = 30;
    let conversationType = "group";
    let conversationRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .setConversationType(conversationType)
      .build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30,
      conversationType: string = "group",
      conversationRequest: CometChat.ConversationsRequest = new CometChat.ConversationsRequestBuilder()
        .setLimit(limit)
        .setConversationType(conversationType)
        .build();
    ```
  </Tab>
</Tabs>

### 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`.

<Tabs>
  <Tab title="With User and Group Tags">
    ```javascript theme={null}
    let limit = 30;
    let conversationRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .withUserAndGroupTags(true)
      .build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30,
      conversationRequest: CometChat.ConversationsRequest = new CometChat.ConversationsRequestBuilder()
        .setLimit(limit)
        .withUserAndGroupTags(true)
        .build();
    ```
  </Tab>
</Tabs>

### Set User Tags

This method fetches user conversations that have the specified tags.

<Tabs>
  <Tab title="JavaScript">
    ```js theme={null}
    let limit = 30;
    let userTags = ["tag1"];
    let conversationRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .setUserTags(userTags)
      .build();   
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30,
      userTags: Array<String> = ["tag1"],
      conversationRequest: CometChat.ConversationsRequest = new CometChat.ConversationsRequestBuilder()
        .setLimit(limit)
        .setUserTags(userTags)
        .build();  
    ```
  </Tab>
</Tabs>

### Set Group Tags

This method fetches group conversations that have the specified tags.

<Tabs>
  <Tab title="JavaScript">
    ```js theme={null}
    let limit = 30;
    let groupTags = ["tag1"];
    let conversationRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .setGroupTags(groupTags)
      .build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30,
      groupTags: Array<String> = ["tag1"],
      conversationRequest: CometChat.ConversationsRequest = new CometChat.ConversationsRequestBuilder()
        .setLimit(limit)
        .setGroupTags(groupTags)
        .build();
    ```
  </Tab>
</Tabs>

### 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`

<Tabs>
  <Tab title="With Tags">
    ```javascript theme={null}
    let limit = 30;
    let conversationRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .withTags(true)
      .build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30,
    conversationRequest: CometChat.ConversationsRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .withTags(true)
      .build();   
    ```
  </Tab>
</Tabs>

### Set Tags

This method helps you fetch the conversations based on the specified tags.

<Tabs>
  <Tab title="Set Tags">
    ```javascript theme={null}
    let limit = 30;
    let tags = ["archivedChat"];
    let conversationRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .setTags(tags)
      .build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30,
      tags: Array<String> = ["archivedChat"],
      conversationRequest: CometChat.ConversationsRequest = new CometChat.ConversationsRequestBuilder()
        .setLimit(limit)
        .setTags(tags)
        .build();
    ```
  </Tab>
</Tabs>

### Include Blocked Users

This method helps you fetch the conversations of users whom the logged-in user has blocked.

<Tabs>
  <Tab title="JavaScript">
    ```js theme={null}
    let limit = 30;
    let conversationRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .setIncludeBlockedUsers(true)
      .build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30,
      conversationRequest: CometChat.ConversationsRequest = new CometChat.ConversationsRequestBuilder()
        .setLimit(limit)
        .setIncludeBlockedUsers(true)
        .build();  
    ```
  </Tab>
</Tabs>

### With Blocked Info

This method can be used to fetch the blocked information of the blocked user in the `ConversationWith` object.

<Tabs>
  <Tab title="JavaScript">
    ```js theme={null}
    let limit = 30;
    let conversationRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .setWithBlockedInfo(true)
      .build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30,
      conversationRequest: CometChat.ConversationsRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .setWithBlockedInfo(true)
      .build();     
    ```
  </Tab>
</Tabs>

### Search Conversations

This method helps you search a conversation based on User or Group name.

<Note>
  This feature is only available with `Conversation & Advanced Search`. The `Conversation & Advanced Search` is only available in `Advanced` & `Custom` [plans](https://www.cometchat.com/pricing). If you're already on one of these plans, please enable the `Conversation & Advanced Search` from [CometChat Dashboard](https://app.cometchat.com) (Open your app, navigate to Chats -> Settings -> General Configuration)
</Note>

<Tabs>
  <Tab title="JavaScript">
    ```js theme={null}
    let limit = 30;
    let conversationRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .setSearchKeyword("Hiking")
      .build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30,
      conversationRequest: CometChat.ConversationsRequest = new CometChat.ConversationsRequestBuilder()
        .setLimit(limit)
        .setSearchKeyword("Hiking")
        .build();  
    ```
  </Tab>
</Tabs>

### Unread Conversations

This method helps you fetch unread conversations.

<Note>
  This feature is only available with `Conversation & Advanced Search`. The `Conversation & Advanced Search` is only available in `Advanced` & `Custom` [plans](https://www.cometchat.com/pricing). If you're already on one of these plans, please enable the `Conversation & Advanced Search` from [CometChat Dashboard](https://app.cometchat.com) (Open your app, navigate to Chats -> Settings -> General Configuration)
</Note>

<Tabs>
  <Tab title="JavaScript">
    ```js theme={null}
    let limit = 30;
    let conversationRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .setUnread(true)
      .build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30,
      conversationRequest: CometChat.ConversationsRequest = new CometChat.ConversationsRequestBuilder()
        .setLimit(limit)
        .setUnread(true)
        .build();  
    ```
  </Tab>
</Tabs>

### Hide Agentic Conversations

This method allows you to exclude agent conversations from the conversation list. When set to `true`, conversations with AI agents will be filtered out.

<Tabs>
  <Tab title="JavaScript">
    ```js theme={null}
    let limit = 30;
    let conversationRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .setHideAgentic(true)
      .build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30,
      conversationRequest: CometChat.ConversationsRequest = new CometChat.ConversationsRequestBuilder()
        .setLimit(limit)
        .setHideAgentic(true)
        .build();  
    ```
  </Tab>
</Tabs>

### Only Agentic Conversations

This method allows you to fetch only agent conversations. When set to `true`, only conversations with AI agents will be returned in the list.

<Tabs>
  <Tab title="JavaScript">
    ```js theme={null}
    let limit = 30;
    let conversationRequest = new CometChat.ConversationsRequestBuilder()
      .setLimit(limit)
      .setOnlyAgentic(true)
      .build();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30,
      conversationRequest: CometChat.ConversationsRequest = new CometChat.ConversationsRequestBuilder()
        .setLimit(limit)
        .setOnlyAgentic(true)
        .build();  
    ```
  </Tab>
</Tabs>

<Note>
  The `setHideAgentic()` and `setOnlyAgentic()` methods are mutually exclusive. You should only use one of them in a single request builder instance.
</Note>

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.

<Tabs>
  <Tab title="Conversations Request">
    ```javascript theme={null}
    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);
      }
    );
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let limit: number = 30,
      conversationsRequest: CometChat.ConversationsRequest = new CometChat.ConversationsRequestBuilder()
        .setLimit(limit)
        .build();

    conversationsRequest.fetchNext().then(
      (conversationList: CometChat.Conversation[]) => {
        console.log("Conversations list received:", conversationList);
      },
      (error: CometChat.CometChatException) => {
        console.log("Conversations list fetching failed with error:", error);
      }
    );
    ```
  </Tab>
</Tabs>

The `Conversation` Object consists of the following 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.                        |

## 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 fetch.

2. `conversationType`: The `conversationType` variable can hold one of the below two values:

   1. user - Only fetches user conversation.
   2. group - Only fetches group conversations.

3. `tags`: The `tags` variable will be a list of tags you want to add to a conversation.

<Tabs>
  <Tab title="Tag Conversation">
    ```javascript theme={null}
    let tags = ["archivedChat"];

    CometChat.tagConversation("conversationWith", "conversationType", tags).then(
      (conversation) => {
        console.log("conversation", conversation);
      },
      (error) => {
        console.log("error while fetching a conversation", error);
      }
    );
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let conversationWith: string = "UID",
      tags: Array<String> = ["archivedChat"],
      conversationType: string = "user";

    CometChat.tagConversation(conversationWith, conversationType, tags).then(
      (conversation: CometChat.Conversation) => {
        console.log("conversation", conversation);
      },
      (error: CometChat.CometChatException) => {
        console.log("error while fetching a conversation", error);
      }
    );
    ```
  </Tab>
</Tabs>

<Note>
  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.
</Note>

## 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:

   1. user - Only fetches user conversation.
   2. group - Only fetches group conversations.

<Tabs>
  <Tab title="Get Conversation">
    ```javascript theme={null}
    CometChat.getConversation("conversationWith", "conversationType").then(
      (conversation) => {
        console.log("conversation", conversation);
      },
      (error) => {
        console.log("error while fetching a conversation", error);
      }
    );
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let conversationWith: string = "UID",
      conversationType: string = "user";

    CometChat.getConversation(conversationWith, conversationType).then(
      (conversation: CometChat.Conversation) => {
        console.log("conversation", conversation);
      },
      (error: CometChat.CometChatException) => {
        console.log("error while fetching a conversation", error);
      }
    );
    ```
  </Tab>
</Tabs>

## Convert Messages to Conversations

As per our [receive messages](/sdk/javascript/receive-message) 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 the `Conversation` object. You can use the `getConversationFromMessage(BaseMessage message)` of the `CometChatHelper` class.

<Tabs>
  <Tab title="Convert Message to Conversation">
    ```javascript theme={null}
    CometChat.CometChatHelper.getConversationFromMessage(message).then(
      (conversation) => {
        console.log("Conversation Object", conversation);
      }, (error) => {
        console.log("Error while converting message object", error);
      }
    );
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let message: CometChat.TextMessage | CometChat.MediaMessage | CometChat.CustomMessage;

    CometChat.CometChatHelper.getConversationFromMessage(message).then(
      (conversation: CometChat.Conversation) => {
        console.log("Conversation Object", conversation);
      },(error: CometChat.CometChatException) => {
        console.log("Error while converting message object", error);
      }
    );
    ```
  </Tab>
</Tabs>

<Note>
  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.
</Note>
