# Manage server-side messages (/en/realtime-media/im/build/build-core-messaging/messages/retrieve-messages/react-native)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

The Chat SDK stores historical messages on the chat server. When a chat user logs in from a different device, you can retrieve the historical messages from the server, so that the user can also browse these messages on the new device. Additionally, the Chat SDK supports adding tags to conversation, with a maximum of 20 tags allowed per conversation.

    This page introduces how to use the Chat SDK to retrieve, delete, and tag messages and conversations.

    ## Understand the tech [#understand-the-tech-4]

    The Chat SDK uses `ChatManager` to retrieve historical messages from the server. The following are the core methods:

    * `asyncFetchConversationsFromServer`: Retrieves a list of conversations stored on the server.
    * `asyncFetchHistoryMessages`: Retrieves historical messages of a conversation from the server according to `FetchMessageOption`, the parameter configuration class for retrieving historical messages.
    * `asyncPinConversation`: Pins conversations.
    * `asyncFetchPinnedConversationsFromServer`: Retrieves pinned conversations.
    * `asyncPinMessage`: Pins a message in a conversation.
    * `asyncUnPinMessage`: Unpin a message in a conversation.
    * `asyncGetPinnedMessagesFromServer`: Get a list of pinned messages in a conversation.
    * `removeMessagesFromServer`: One-way deletion of historical messages on the server based on message time or message ID.
    * `deleteConversationFromServer`: Deletes conversations and their historical messages from the server.
    * `asyncAddConversationMark`: Tags a conversation.
    * `asyncRemoveConversationMark`: Removes a conversation tag.
    * `asyncGetConversationsFromServerWithCursor`: Queries conversations from the server by a conversation tag.

    The Chat SDK uses `ChatManager` to retrieve historical messages from the server. The following are the core methods:

    * `fetchAllConversations`: Retrieves a list of conversations stored on the server.
    * `fetchHistoryMessagesByOptions`: Retrieves historical messages of a conversation from the server according to `ChatFetchMessageOptions`, the parameter configuration class for retrieving historical messages.
    * `pinConversation`: Pins a conversation.
    * `fetchPinnedConversationsFromServerWithCursor`: Retrieves a list of pinned conversations.
    * `removeMessagesFromServerWithTimestamp`/`removeMessagesFromServerWithMsgIds`: Deletes historical messages from the server unidirectionally.
    * `removeConversationFromServer`: Deletes conversations and related messages from the server.

    ## Prerequisites [#prerequisites-4]

    Before proceeding, ensure that you meet the following requirements:

    * You have integrated the Chat SDK, initialized the SDK and implemented the functionality of registering accounts and login. For details, see [Chat SDK quickstart](../../../../get-started-sdk).
    * You understand the API call frequency limits as described in [Limitations](../../limitations).

    ## Implementation [#implementation-4]

    This section shows how to implement retrieving conversations and messages.

    ### Retrieve a list of conversations from the server [#retrieve-a-list-of-conversations-from-the-server-4]

    Call `fetchConversationsFromServerWithCursor` to retrieve conversations from the server with pagination. The SDK returns the conversation list in the reverse chronological order of when conversations are active (the timestamp of the last message in the conversation). In the conversation list, each conversation object contains the conversation ID, conversation type, whether the conversation is pinned, the pinned time (the value is 0 for an unpinned conversation), and the last message in the conversation. After the conversation list is retrieved from the server, the local conversation list will be updated accordingly. We recommend calling this method when the app is first installed, or when there is no conversation on the local device. Otherwise, you can call `getAllConversations` to retrieve conversations on the local device.

    For each end user, the server stores 100 conversations by default. When this limit is exceeded, new conversations will start overwriting the old ones. If the entire message history in a conversation expires, the conversation becomes empty. When pulling the conversation list from the server, these empty conversations are not included by default. To include them, set `ChatOptions#enableEmptyConversation` to `true` when initializing the SDK. In this case, empty conversations will occupy the conversations pull quota, regardless of whether they are needed when pulling. To change this, contact [support@agora.io](mailto\:support@agora.io).

    ```java
    // pageSize: The number of conversations that you expect to get on each page. The value range is [1,50].
    // cursor: If `cursor` is an empty string, the SDK retrieves from the latest conversation.
    ChatClient.getInstance()
      .chatManager.fetchConversationsFromServerWithCursor(cursor, pageSize)
      .then(() => {
        console.log("get conversions success");
      })
      .catch((reason) => {
        console.log("get conversions fail.", reason);
      });
    ```

    If you do not support `fetchConversationsFromServerWithCursor`, call `fetchConversationsFromServerWithPage` to retrieve the conversations from the server. Altogether, the SDK can retrieve the last 100 conversations in the past seven days. To adjust the time limit or the number of conversations retrieved, contact [support@agora.io](mailto\:support@agora.io).

    ### Retrieve historical messages of the specified conversation [#retrieve-historical-messages-of-the-specified-conversation-1]

    After retrieving conversations, you can retrieve historical messages from the server.

    You can set the search direction to retrieve messages in the chronological or reverse chronological order of when the server receives them, the message type, the time period, the message sender, as well as whether to save the retrieved message to the local database.

    If you have integrated Chat SDK after June 8, 2023, you can retrieve historical messages even before joining the Chat Group. For earlier implementations, contact [support@agora.io](mailto\:support@agora.io) to enable this.

    The Agora Chat server stores the full message history for a certain period of time depending on your subscribed [Chat plan](./message-overview#limitations-of-message-storage-duration). After an end user logs back into Agora Chat, the servers automatically send offline messages to them, that is, messages transmitted when that end user was offline. Offline messages are a subset of the full message history stored on Agora Chat server. Sending only a subset of messages prevents distributing too many messages to a single device, which can overwhelm it and slow down the end user login. Agora Chat server stores and manages these offline messages for every end user in the following way:

    * 1:1 private chat: Store 500 offline messages by default;
    * Chat Group: Store 200 offline messages by default;
    * Chatroom: Doesn't store offline messages. However, whenever an end user joins a chatroom, Agora Chat servers push the 10 latest messages/chatroom to them, by default. This number can be adjusted to 200 messages/chatroom without additional charges.

    For users to receive more offline messages, use the client API or a webhook to sync with Agora Chat's server. End users can also store additional messages on their local database.

    To ensure data reliability, we recommend retrieving less than 50 historical messages for each method call. To retrieve more than 50 historical messages, call this method multiple times. Once the messages are retrieved, the SDK automatically updates these messages in the local database.

    We recommend that you retrieve 20 messages each time, with a maximum of 50. During paginated query, if the total number of messages that meet the query conditions is greater than the number of `pageSize`, the number of messages of `pageSize` will be returned. If it is less than the number of `pageSize`, the actual number will be returned. When the message query is completed, the number of returned messages is less than the number of `pageSize`.

    Since SDK v1.4.0, for a single group conversation you can retrieve messages sent by specific members (rather than all members) by setting the `senders` array in `ChatFetchMessageOptions`.

    Refer to the following code sample:

    ```typescript
    ChatClient.getInstance()
      .chatManager.fetchHistoryMessagesByOptions(convId, convType, {
        cursor: cursor,
        pageSize: pageSize,
        options: options as ChatFetchMessageOptions,
      })
      .then((result) => {
        console.log("get history message success", result);
      })
      .catch((reason) => {
        console.log("get history message fail.", reason);
      });
    ```

    ### Search local messages sent by specific members [#search-local-messages-sent-by-specific-members-3]

    Since SDK v1.4.0, for a single conversation you can load messages from the local database that are sent by specific members, using `getConvMsgsWithKeyword`.

    ```typescript
    const conversationId = '<YOUR_CONVERSATION_ID>';
    const conversationType = ChatConversationType.GroupChat;
    const senders = ['user1', 'user2'];
    ChatClient.getInstance()
      .chatManager.getConvMsgsWithKeyword({
        convId: conversationId,
        convType: conversationType,
        senders: senders,
        keywords: '',
      })
      .then((messages) => console.log('Messages:', messages))
      .catch((error) => console.error('Error:', error));
    ```

    ### Search local conversations by keyword [#search-local-conversations-by-keyword-3]

    Since SDK v1.4.0, you can call `getConvsMsgsWithKeyword` to search across all local conversations for messages that contain a keyword. The SDK returns the matching conversation IDs and message IDs, ordered by message timestamp in ascending or descending order according to the `direction` parameter.

    ```typescript
    ChatClient.getInstance()
      .chatManager.getConvsMsgsWithKeyword({
        keywords: 'hello',
        timestamp: -1,
        from: '<MESSAGE_SENDER_ID>',
        direction: ChatSearchDirection.UP,
        searchScope: ChatMessageSearchScope.All,
      })
      .then((result) => console.log('Result:', result))
      .catch((error) => console.error('Error:', error));
    ```

    ### Retrieve local messages by message ID [#retrieve-local-messages-by-message-id-3]

    Since SDK v1.4.0, you can call `getMessagesWithIds` to retrieve one or more messages from a single local conversation by message ID.

    ```typescript
    ChatClient.getInstance()
      .chatManager.getMessagesWithIds({
        convId: '<YOUR_CONVERSATION_ID>',
        convType: ChatConversationType.GroupChat,
        msgIds: ['<MSG_ID_1>', '<MSG_ID_2>'],
      })
      .then((messages) => console.log('Messages:', messages))
      .catch((error) => console.error('Error:', error));
    ```

    ### Pin a conversation [#pin-a-conversation-4]

    To keep track of an important conversation, you can pin it to the top of your conversation list. You can pin up to 50 conversations. The pinned state is stored on the server. In a multi-device login use-case, if you pin or unpin a conversation, other login devices will receive the `CONVERSATION_PINNED` or `CONVERSATION_UNPINNED` events.

    Refer to the following code example to pin a conversation:

    ```typescript
    // isPinned: Sets whether to pin a conversation.
    ChatClient.getInstance()
      .chatManager.pinConversation(convId, isPinned)
      .then(() => {
        console.log("pin conversions success");
      })
      .catch((reason) => {
        console.log("pin conversions fail.", reason);
      });
    ```

    ### Retrieve the pinned conversations from the server with pagination [#retrieve-the-pinned-conversations-from-the-server-with-pagination-3]

    End users can pin up to 50 conversations. After you call this API, the SDK returns the pinned conversations in the reverse chronological order of when they are pinned.

    Agora Chat servers store a list of conversations that remain active in the past 7 days, regardless of Agora Chat package subscription. A conversation is considered active if it is pinned or there are new messages in a conversation.

    Refer to the following code example to get a list of pinned conversations from the server with pagination:

    ```typescript
    // pageSize: The number of sessions returned per page. The value range is [1,50]。
    // cursor：The cursor position to start getting data.
    ChatClient.getInstance()
      .chatManager.fetchPinnedConversationsFromServerWithCursor(cursor, pageSize)
      .then(() => {
        console.log("get conversions success");
      })
      .catch((reason) => {
        console.log("get conversions fail.", reason);
      });
    ```

    ### Pin a message [#pin-a-message-4]

    You can call `ChatManager#pinMessage` to pin a message to the top of a one-to-one chat, chat group, or chat room. When the pinned status of a message changes, other members in the group or chat room conversation will receive the `MessageListener#onMessagePinChanged` event. In the case of multi-device login, the updated top status will be synchronized to other logged-in devices, and other devices will receive the `MessageListener#onMessagePinChanged` event, respectively.

    In group and chat room conversations, multiple users can pin the same message to the top. The latest pinned message will overwrite the earlier information. That is, the `ChatMessagePinInfo` user ID and pin time will correspond to the latest pinned message.

    For a single conversation, 20 messages can be pinned to the top by default.

    ```typescript
    ChatClient.getInstance()
      .chatManager.pinMessage(
        messageId // message ID
      )
      .then(() => {
        // todo: operation completed
      })
      .catch((error) => {
        // todo: operation failed
      });
    ```

    ### Delete historical messages from the server unidirectionally [#delete-historical-messages-from-the-server-unidirectionally-4]

    Call `removeMessagesFromServerWithTimestamp` or `removeMessagesFromServerWithMsgIds` to delete historical messages one way from the server. You can remove a maximum of 50 messages from the server each time. Once the messages are deleted, you can no longer retrieve them from the server. The deleted messages are automatically removed from your local device. Other chat users can still get the messages from the server.

    <CalloutContainer type="info">
      <CalloutDescription>
        To use this function, you need to contact [support@agora.io](mailto\:support@agora.io) to enable it.
      </CalloutDescription>
    </CalloutContainer>

    ```typescript
    // Delete messages by message ID
    ChatClient.getInstance()
      .chatManager.removeMessagesFromServerWithMsgIds(convId, convType, msgIds)
      .then((result) => {
        console.log("test:success:", result);
      })
      .catch((error) => {
        console.warn("test:error:", error);
      });
    // Delete messages by timestamp
    ChatClient.getInstance()
      .chatManager.removeMessagesFromServerWithTimestamp(
        convId,
        convType,
        timestamp
      )
      .then((result) => {
        console.log("test:success:", result);
      })
      .catch((error) => {
        console.warn("test:error:", error);
      });
    ```

    ### Delete conversations and related messages from the server unidirectionally [#delete-conversations-and-related-messages-from-the-server-unidirectionally-4]

    Call `removeConversationFromServer` to delete conversations and their historical messages unidirectionally from the server. After the conversations and messages are deleted from the server, you can no longer get them from the server. The deleted conversations still exist on the local device, but the messages are automatically removed from the device. Other chat users can still get the conversations and their historical messages from the server.

    ```typescript
    // convId: conversation ID
    // convType: conversation type.
    // isDeleteMessage: Whether to delete historical messages from the server and local storage with the conversation.
    ChatClient.getInstance()
      .chatManager.removeConversationFromServer(convId, convType, isDeleteMessage)
      .then(() => {
        console.log("remove conversions success");
      })
      .catch((reason) => {
        console.log("remove conversions fail.", reason);
      });
    ```

    ## Next steps [#next-steps-4]

    After implementing retrieving messages, you can refer to the following documents to add more messaging functionalities to your app:

    * [Message receipts](./message-receipts)

    
  
      
  
      
  
