Browse SDKs · HarmonyOS
Platform
SDKsHarmonyOSEnterprise

Load older messages

Paginate backward chronologically to retrieve older message history in a conversation.

Copy

Pass an empty string "" for startClientMsgID when fetching the initial page; for subsequent backward pages, pass the client message ID of the oldest currently rendered message.

Code example

import sdk, {
  ErrorCode,
  GetHistoryMessageListReq,
  GetHistoryMessageListResp,
  ViewType,
} from '@openimsdk/imsdk';

const request: GetHistoryMessageListReq = {
  conversationID: conversationID,
  startClientMsgID: oldestClientMsgID,
  count: 30,
  isReverse: false,
  viewType: ViewType.ViewHistory,
};
const response: GetHistoryMessageListResp = await sdk.getHistoryMessageList(request);
if (response.errCode === ErrorCode.NoError) {
  prependMessages(response.messageList);
}

Parameters

ParameterTypeRequiredDescription
conversationIDstringYesTarget conversation identifier.
startClientMsgIDstringYesClient message ID of the oldest message, or "" for the first page.
countnumberYesNumber of messages to retrieve per page.
isReversebooleanYesPass false for backwards chronological paging.
viewTypeViewTypeYesMessage view mode, typically ViewType.ViewHistory.

Return value

Returns a Promise resolving to GetHistoryMessageListResp:

PropertyTypeDescription
errCodeErrorCodeSDK error code enum value (ErrorCode.NoError indicates success).
errMsgstringError description if errCode is non-zero.
messageListIMMessage[]Array of older messages retrieved.
isEndbooleantrue if the oldest message in history has been reached.

State changes after the call

Cease paging backward when isEnd becomes true. Treat rejected Promises and responses with non-zero errCode as load failures. This query does not trigger new message events; when prepending results, scope mutations to the current conversationID and deduplicate by clientMsgID.