Browse SDKs · WASM
SDKsWASMEnterprise

Load message context

Retrieve messages before and after a specified message with the WASM SDK.

Copy

When navigating from a search result or referenced message into its chat context, use the MessageItem you have already retrieved as the anchor.

Parameters

ParameterTypeRequiredDescription
startMessageMessageItemYesPreviously retrieved anchor message.
viewTypeViewTypeYesContext viewing direction. Use ViewType.History for message history.
beforenumberYesMaximum number of messages to retrieve before the anchor.
afternumberYesMaximum number of messages to retrieve after the anchor.
import { ViewType } from '@openim/wasm-client-sdk';

const { data: surrounding } = await openimsdk.fetchSurroundingMessages({
  startMessage: targetMessage,
  viewType: ViewType.History,
  before: 20,
  after: 20,
});

renderMessages(surrounding.messageList);

When the Promise resolves, data.messageList is the MessageItem[] retrieved before and after the anchor. For message fields, see Message overview.

before and after independently limit the number of messages on either side. The result can contain fewer messages than the requested total when the anchor is near a history boundary or some messages have been deleted. Merge by conversationID:clientMsgID; do not rely on fixed array positions. If you first need to locate the message, see Find messages by ID.