This document describes how to query the message history and specific messages of one-to-one, group, and in-room chats with the ZIM SDK.
This feature allows you to get historical messages of types other than command messages and pop-up messages.
Before getting message history, please make sure:
After logging in to ZIM SDK, users can use the queryHistoryMessage method to retrieve the message history of one-to-one chats
, group chats
, and rooms chats
by providing the parameters conversationID and config.
Here is an example of how to retrieve the conversation history between client A and client B:
queryHistoryMessage(
conversationID: string,
conversationType: ZIMConversationType,
config: ZIMMessageQueryConfig,
): Promise<ZIMMessageQueriedResult>
Parameter/Callback | Type | Required | Description |
---|---|---|---|
conversationID | string | Yes | The conversation ID.
|
conversationType | ZIMConversationType | Yes | The conversation type.
Please note that by default, offline message caching and retrieval are not supported in the "Room" scenario. If you need this feature, please contact ZEGOCLOUD technical support for assistance. |
config | ZIMMessageQueryConfig | Yes | Advanced settings for retrieving historical messages. Please refer to the table below for details. |
Promise | ZIMMessageQueriedResult | Yes | The result callback for retrieving historical messages. |
The config
parameter needs to be configured using the following parameters of the ZIMMessageQueryConfig class:
Parameter | Type | Required | Description |
---|---|---|---|
nextMessage |
Yes |
The paging identifier. Set nextMessage to null for the first query. In subsequent paging queries, the nextMessage is the last message in the list of messages currently queried.
|
|
messageCount |
Number |
Yes |
The number of messages that can be retrieved in one query. It is recommended to retrieve less than 100 messages in each query to reduce costs. |
reverse | Boolean |
Yes |
When querying messages:
|
// Retrieve historical messages for one-on-one chats
var curMessageList = [];
var conversationID = '';
var conversationType = 0;
// Retrieve 30 messages each time, starting from the latest message
var config = {
nextMessage: null, // Set nextMessage to null for the first retrieval
count: 30,
reverse: true
}
function queryMessageCallback({ messageList }) {
curMessageList.push(...messageList);
// When scrolling down to the topmost message on the screen, retrieve earlier messages
if (fetchMore && messageList.length > 0) {
// For subsequent pagination, set nextMessage to the first message in the current retrieved message list
config.nextMessage = messageList[0];
zim.queryHistoryMessage(conversationID, conversationType, config).then(queryMessageCallback);
}
}
zim.queryHistoryMessage(conversationID, conversationType, config).then(queryMessageCallback);
ZIM supports querying specific messages in a one-to-one or group conversation based on messageSeq
(the sequence number of the message in the conversation) list (up to a maximum of 10) by calling queryMessages.
This interface is used when you only know the messageSeq
of a message and do not know the complete structure of the message. For example, if a message in a conversation replies to a historical message, members of the conversation can use the repliedInfo.messageSeq
of the reply to obtain the messageSeq
of the historical message. At this time, you can call this interface to obtain the complete structure of the historical message.
var messageSeqs = []; // The maximum length is 10
var conversationID = '';
var conversationType = 0; // Conversation type: one-to-one: 0, group: 2
zim.queryMessages(messageSeqs, conversationID, conversationType)
.then(({ messageList }) => {
// Query successful
})
.catch((err) => {
// Query failed
});