Skip to main content

Retrieve History Messages

Activate Service

The Cloud Storage for One-to-One and Group Messages service is enabled by default. You can retrieve historical messages for one-to-one and group chats from the RC server directly. If you are using an App Key in the production environment, this service is available for Chat Starter Plan or Chat Pro Plan. For specific features and pricing, refer to the Billing Guide document.

Note: Please distinguish between historical message records and offline messages?. RC provides default offline message caching for one-to-one chats, group chats, and system messages for up to 7 days (adjustable). The SDK automatically retrieves messages received during offline periods when the client comes online, with no need for app-level API calls. For details, see Manage Offline Message Storage Configuration.

Retrieve Historical Messages

tip

Since the Web client lacks local persistent storage capabilities and cannot save historical message records or conversation lists locally, data must be retrieved from RC's server. The Cloud Storage for One-to-One and Group Messages service is enabled by default.

Call getHistoryMessages to fetch historical message records for a specified conversation.

Interface

RongIMLib.getHistoryMessages(conversation, option)


#### Parameter Description

| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| conversation | [IConversationOption] | Yes | Target conversation |
| option | [GetHistoryMessageOption](https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#GetHistoryMessageOption) | No | Configuration for retrieving historical messages |


- **GetHistoryMessageOption** Description

| Parameter | Type | Description |
|:-------|:---------|:------|
| timestamp | number | (Optional) Controls the boundary for paginated message queries. Queries use this timestamp as a reference, with `order` determining whether to retrieve messages before or after it. Passing `0` starts retrieval from the current time. |
| count | number | (Optional) Number of messages to retrieve. For SDK versions < 5.7.4, range is [1-20]; for SDK versions ≥ 5.7.4, range is [1-100]. Default is `20`. |
| order | number | (Optional) Message query direction: `0` or `1`. `0` indicates descending order (messages sent before `timestamp`), while `1` indicates ascending order (messages sent after `timestamp`). |


#### Code Example

```js
const conversation = {
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: "<Target User ID>"
}

// Query backward from current time
const option = {
timestamp: 0,
count: 20,
order: 0
}

RongIMLib.getHistoryMessages(conversation, option).then(res => {
if (res.code === 0) {
console.log(res.data.list)
console.log(res.data.hasMore)
} else {
console.log(res.code, res.msg)
}
})

Retrieve First Unread Message Information

tip
  • This feature is supported starting from version 5.9.0.

  • When a conversation has no unread messages, the returned data is null. If the first unread message is recalled, its information is not automatically updated.

Call the getFirstUnreadMessageInfo method to retrieve information about the first unread message in a specified conversation.

Interface

RongIMLib.getFirstUnreadMessageInfo(conversation)

Parameter Description

ParameterTypeRequiredDescription
conversationIConversationOptionYesTarget conversation

Example Code

const conversation = {
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: "<Target User ID>"
}

RongIMLib.getFirstUnreadMessageInfo(conversation).then(res => {
if (res.code === 0) {
console.log(res.data)
} else {
console.log(res.code, res.msg)
}
})