Retrieve History Messages
Activate Service
Retrieving historical messages for one-to-one and group chats from the remote server refers to fetching historical messages from the RC server. This feature requires that the App Key has enabled the Cloud Storage for One-to-One and Group Messages service provided by RC. You can activate this service for the currently used App Key on the Chat pricing plans page in the Console. If you are using a production environment App Key, please note that only the Chat Premium Plan or Chat Ultimate Plan can activate this service. For specific features and pricing, refer to the Billing Documentation.
Note: Please distinguish between historical message records and offline messages?. RC provides a default offline message caching service for up to 7 days (adjustable) for one-to-one chats, group chats, and system messages. When the client goes online, the SDK automatically retrieves messages received during the offline period, without requiring the app layer to call an API. For more details, see Manage Offline Message Storage Configuration.
Retrieve Historical Messages
The web client does not have persistent data storage capabilities and cannot locally store historical message records and conversation lists persistently. Therefore, data needs to be retrieved from the RC server. Ensure that the Cloud Storage for One-to-One and Group Messages service is activated.
Call getHistoryMessages to pull historical message records for a specified conversation.
const conversation = {
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: "<Target User ID>"
}
// Query forward from the 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)
}
})
Parameter | Type | Description |
---|---|---|
conversation | IConversationOption | The conversation for which messages are to be retrieved |
options | GetHistoryMessageOption | Optional. See the GetHistoryMessageOption description below |
-
GetHistoryMessageOption Description
Parameter Type Description timestamp number (Optional) Controls the boundary for paginated message queries. Queries use this timestamp as a reference, and the order
determines whether to retrieve messages before or after this timestamp. Passing0
fortimestamp
means starting the query from the current time.count number (Optional) The number of messages to retrieve. For SDK < 5.7.4, the range is [1-20]; for SDK ≧ 5.7.4, the range is [1-100]. Default value is 20
.order number (Optional) The direction of the message query, with values 0
or1
.0
indicates descending order, i.e., retrieving messages sent before thetimestamp
in descending order of message send time.1
indicates ascending order, i.e., retrieving messages sent after thetimestamp
in ascending order of message send time.
Retrieve First Unread Message Information
This feature is supported starting from version 5.9.0. When there are no unread messages, the returned data is null. If the first unread message is recalled, this information will not be updated.
Call getFirstUnreadMessageInfo to retrieve the first unread message information.
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)
}
})
Parameter | Type | Description |
---|---|---|
conversation | IConversationOption | The conversation for which the first unread message is to be retrieved |