The Agora 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.
This page introduces how to use the Agora Chat SDK to retrieve messages from the server.
The Agora Chat SDK uses ChatManager
to retrieve historical messages from the server. Followings are the core methods:
fetchAllConversations
: Retrieve a list of conversations stored on the server.fetchHistoryMessages
: Retrieve the historical messages in the specified conversation from the server.Before proceeding, ensure that you meet the following requirements:
This section shows how to implement retrieving conversations and messages.
Call fetchAllConversations
to retrieve all the conversations from the server. 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.
ChatClient.getInstance()
.chatManager.fetchAllConversations()
.then(() => {
console.log("load conversions success");
})
.catch((reason) => {
console.log("load conversions fail.", reason);
});
By default, the SDK retrieves the last ten conversations in the past seven days, and each conversation contains one last historical message. To adjust the time limit or the number of conversations retrieved, contact support@agora.io.
After retrieving conversations, you can retrieve historical messages by pagination from the server.
To ensure data reliablity, 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.
// Specify the conversation ID.
const convId = "convId";
// Specify the conversation type. For details, see descriptions in ChatConversationType.
const convType = ChatConversationType.PeerChat;
// Specify the maximum count of the retrieved messages.
const pageSize = 10;
// Specify the message ID from which to retrieve the historical messages.
const startMsgId = "";
ChatClient.getInstance()
.chatManager.fetchHistoryMessages(convId, chatType, pageSize, startMsgId)
.then((messages) => {
console.log("get message success: ", messages);
})
.catch((reason) => {
console.log("load conversions fail.", reason);
});
After implementing retrieving messages, you can refer to the following documents to add more messaging functionalities to your app: