Skip to main content

Retrieve Chatroom Message History

Enabling Services

Retrieving Chatroom History Messages requires enabling the Cloud Storage for Chatroom Messages service. To enable this service, please [submit a ticket]. Once enabled, chatroom history messages will be stored in the cloud with a default retention period of 2 months.

Retrieving Chatroom History Messages

Call the [getChatroomHistoryMessages] method to retrieve chatroom history messages stored on the server.

Interface

getChatroomHistoryMessages(targetId, options)


#### Parameter Description

| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| targetId | string | Yes | Chatroom ID |
| options | [GetHistoryMessageOption] | Yes | Parameters for retrieving history messages |


- **options Details**

| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| timestamp | number | No | Timestamp for retrieval. 0 indicates pulling from the latest current time (unit: milliseconds). |
| count | number | No | Number of messages to retrieve (range: 1-20) |
| order | number | No | Retrieval order. Default is 0: 0 = ascending (retrieves messages with timestamps earlier than sentTime); 1 = descending (retrieves messages with timestamps later than sentTime) |


#### Sample Code

```js
const chatRoomId = "Chatroom ID";
const timestamp = 0;
const count = 10
const order = 1
RongIMLib.getChatroomHistoryMessages(chatRoomId, {
timestamp: timestamp,
count: count,
order: order
}).then(res => {
// Successfully retrieved chatroom history messages
if (res.code === 0){
console.log(res.code, res.data)
} else {
console.log(res.code, res.msg)
}
}).catch(error => {
console.log(error)
})

[getChatroomHistoryMessages]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#getChatroomHistoryMessages
[GetHistoryMessageOption]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#GetHistoryMessageOption
[submit a ticket]: https://console.rongcloud.io/agile/formwork/ticket/create