Retrieve History Messages (Electron)
-
This document applies only to the Electron Solution and must be used with the Electron modules (@rongcloud/electron and @rongcloud/electron-renderer).
-
Chatroom and ultra group conversation types are currently not supported.
Enable 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 up to 7 days (configurable) for one-to-one chats, group chats, and system messages. The SDK automatically retrieves messages received during offline periods when the client comes online, without requiring API calls at the application layer. See Manage Offline Message Storage Configuration for details.
Retrieve Messages from Local Database
Get Single Local Message
Call the electronExtension.getLocalMessage interface to query and return complete message data from the local database using either the message's messageId or messageUId.
- Supported in SDK ≥ 5.16.0.
- This interface supports two parameter types:
numberandstring.- If the parameter is of type
number, it will be treated asmessageIdfor local database query. - If the parameter is of type
string, it will be treated asmessageUIdfor local database query.
- If the parameter is of type
Interface
RongIMLib.electronExtension.getLocalMessage(messageUId);
Parameters
This method supports two parameter types:
- When the parameter is a number type, it will be treated as messageId to query local messages by message ID.
- When the parameter is a string type, it will be treated as messageUId to query local messages by message unique ID.
| Parameter | Type | Required | Description |
|---|---|---|---|
| messageUId | string or number | Yes | Message identifier |
Example Code
const messageUId = 'BS4O-P5AO-D1O6-9GPP';
const res = await RongIMLib.electronExtension.getLocalMessage(messageUId);
console.info('Message retrieval result:', res);
Get All Message Types in Conversation
Call getHistoryMessages to retrieve historical message records for a specified conversation.
Interface
RongIMLib.getHistoryMessages(conversation, option)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| conversation | IConversationOption | Yes | Target conversation |
| option | GetHistoryMessageOption | No | Configuration parameters |
- GetHistoryMessageOption Details
Parameter Type Description timestamp number (Optional) Time boundary for paginated queries. Determines whether to retrieve messages before or after this timestamp based on order. Passing0starts from the current time.count number (Optional) Number of messages to retrieve per request. Range: [1-20] for SDK < 5.7.4, [1-100] for SDK ≥ 5.7.4. Default: 20.order number (Optional) Query direction: 0 for descending (messages before timestamp), 1 for ascending (messages after timestamp).
Example Code
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)
}
})
Get Specified Message Types in Conversation
Call electronExtension.getHistoryMessagesByMessageTypes to retrieve historical messages of specified types within a conversation.
Interface
RongIMLib.electronExtension.getHistoryMessagesByMessageTypes(conversation, option)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| conversation | IConversationOption | Yes | Target conversation |
| option | GetHistoryMessageOption | Yes | Configuration parameters |
Example Code
const conversation = {
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: "<Target User ID>"
}
// Query backward from 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 deprecated since 5.6.1, use res.data.list for 5.6.1+
console.log(res.data.list)
console.log(res.data.hasMore)
} else {
console.log(res.code, res.msg)
}
})
Retrieve Remote History Messages
Call getRemoteHistoryMessages to retrieve remote historical message records for a specified conversation.
Interface
RongIMLib.getRemoteHistoryMessages(conversation, option)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| conversation | IConversationOption | Yes | Target conversation |
| option | GetHistoryMessageOption | No | Configuration parameters |
Example Code
const conversation = {
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: "<Target User ID>"
}
// Query backward from 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)
}
})
Retrieve Local and Remote History Messages
Call getContinuousMessages to retrieve continuous message records (including both local and remote messages) for a specified conversation.
Differences between getContinuousMessages and getRemoteHistoryMessages:
-
Prioritizes querying local database messages;
-
If local messages are insufficient, continues querying from remote to ensure returning a continuous, adjacent message list.
Interface
RongIMLib.getContinuousMessages(conversation, option)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| conversation | IConversationOption | Yes | Target conversation |
| option | GetHistoryMessageOption | No | Configuration parameters |
For subsequent historical message queries, we recommend using res.data.timestamp as the option.timestamp for the next call.
Sample Code
const conversation = {
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: "<Target User ID>"
}
// Query backward from 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)
// For subsequent historical message queries, we recommend using res.data.timestamp as the option.timestamp for the next call
console.log(res.data.timestamp)
} else {
console.log(res.code, res.msg)
}
})