Retrieve History Messages (Electron)
This document is only applicable to the Electron solution and is limited to use with the Electron modules (@rongcloud/electron and @rongcloud/electron-renderer).
Activate Service
Fetching historical messages for one-to-one and group chats from the remote server refers to retrieving historical messages from the RC server. This feature requires that the Cloud Storage for One-to-One and Group Messages service provided by RC is enabled for your App Key. You can activate this service for the current 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 instructions document.
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.
Fetch Messages from the Local Database
Fetch All Types of Messages in a Conversation
Call getHistoryMessages to retrieve 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 to retrieve messages |
options | GetHistoryMessageOption | Configuration parameters. See the GetHistoryMessageOption Description below |
-
GetHistoryMessageOption Description
Parameter Type Description timestamp number (Optional) Controls the boundary for paginated message queries. Queries are based on this timestamp, and the order
determines whether to retrieve messages before or after this timestamp. Passing0
fortimestamp
means starting from the current time.count number (Optional) The number of messages to retrieve. If SDK < 5.7.4, the range is [1-20]; if SDK ≧ 5.7.4, the range is [1-100]. Default 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 decreasing order of message send time.1
indicates ascending order, i.e., retrieving messages sent after thetimestamp
in increasing order of message send time.
Fetch Messages of Specified Types in a Conversation
Call electronExtension.getHistoryMessagesByMessageTypes to retrieve historical message records of specified types for a specified conversation.
const conversation = {
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: "<Target User ID>"
}
// Query forward from the current time
const options = {
timestamp: 0,
count: 20,
order: 0,
messageTypes: ['RC:TxtMsg']
}
RongIMLib.electronExtension.getHistoryMessagesByMessageTypes(conversation, option).then(res => {
if (res.code === 0) {
// res.data.messages is deprecated starting from 5.6.1. For versions 5.6.1 and later, use res.data.list
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 to retrieve messages |
options | IGetHistoryMessagesByTypesOption | Configuration parameters |
Fetch Remote Historical Messages
Call getRemoteHistoryMessages to retrieve remote 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.getRemoteHistoryMessages(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 to retrieve messages |
options | GetHistoryMessageOption | Configuration parameters |
Fetch Local and Remote Historical Messages
The getContinuousMessages
method differs from getRemoteHistoryMessages
in that getContinuousMessages
first queries the messages stored in the local database for the specified conversation. If the local messages do not meet the query conditions, it then queries the historical messages in the Cloud Storage for One-to-One and Group Messages to return a continuous and adjacent list of message objects.
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.electronExtension.getContinuousMessages(conversation, option).then(res => {
if (res.code === 0) {
console.log(res.data.list)
console.log(res.data.hasMore)
// If you want to continue querying more historical messages, it is recommended to use res.data.timestamp as the option.timestamp for the next call
console.log(res.data.timestamp)
} else {
console.log(res.code, res.msg)
}
})
Parameter | Type | Description |
---|---|---|
conversation | IConversationOption | The conversation for which to retrieve messages |
options | GetHistoryMessageOption | Configuration parameters |