Skip to main content

Retrieve Historical Messages

Enable the Service

Retrieving historical messages for one-to-one and group chats from the remote server refers to fetching historical messages from RC's 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 enable 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 enable this service. For specific features and pricing, please refer to the Billing Instructions document.

Please distinguish between historical message records and offline messages?. RC provides a default offline message caching service for one-to-one chats, group chats, and system messages for up to 7 days (adjustable). The SDK automatically retrieves messages from the offline period when the client goes online, and no API calls are required at the App level. For more details, see Manage Offline Message Storage Configuration.

tip
  • Whether users can retrieve historical group chat messages sent before they joined the group depends on the App's settings in the Console. You can enable New Users Can Retrieve Historical Messages Sent Before Joining the Group on the Basic Features page in the Console. Enabling this option allows new group members to retrieve all group chat messages sent before they joined. If not enabled, new group members can only see group chat messages sent after they joined.
  • By default, users cannot retrieve historical messages from a group they are not in. If you want users to retrieve historical messages from a specified group even when they are not in the group, you can allow users not in the group to retrieve the group's historical messages by navigating to Configuration > Chat settings > Basic features > One-to-One and Group Chats > Can Users Retrieve Historical Messages When Not in the Group in the RC Console.

Retrieve Historical Messages

Developers can use this API to retrieve historical messages for a specific conversation.

Method


getMessages(
type: RCIMIWConversationType,
targetId: string,
channelId: string,
sentTime: number,
order: RCIMIWTimeOrder,
policy: RCIMIWMessageOperationPolicy,
count: number,
callback: IRCIMIWGetMessagesCallback
): Promise<number>;

Parameter Description

ParameterTypeDescription
typeRCIMIWConversationTypeConversation type
targetIdstringConversation ID
channelIdstringChannel ID, only supported for ultra groups. Pass null for other conversation types.
sentTimenumberCurrent message timestamp
orderRCIMIWTimeOrderDirection for retrieving messages. BEFORE: Retrieve messages before sentTime (in descending order), AFTER: Retrieve messages after sentTime (in ascending order)
policyRCIMIWMessageOperationPolicyMessage loading strategy. LOCAL: Load only local messages, REMOTE: Load only remote messages, LOCAL_REMOTE: Load both local and remote messages
countnumberNumber of messages to retrieve, count must be greater than 0 and less than or equal to 20.
callbackIRCIMIWGetMessagesCallbackCallback for API call result.

Return Value

Return ValueDescription
numberStatus code for the current API operation. 0 indicates a successful call, and the specific result needs to be implemented in the callback. Non-zero indicates that the current API call operation failed, and the callback will not be triggered. For detailed errors, refer to the error codes.

Code Example


const callback = {
onSuccess: (t: Array<RCIMIWMessage>) => {
//...
},
onError: (code: number) => {
//...
},
};
let code = await engine.getMessages(type, targetId, channelId, sentTime, order, policy, count, callback);

Retrieve Message by messageId

Developers can use this API to retrieve a specific message.

Method


getMessageById(
messageId: number,
callback: IRCIMIWGetMessageCallback
): Promise<number>;

Parameter Description

ParameterTypeDescription
messageIdnumberThe messageId of the message, which can be obtained from the message object.
callbackIRCIMIWGetMessageCallbackCallback for API call result.

Return Value

Return ValueDescription
numberStatus code for the current API operation. 0 indicates a successful call, and the specific result needs to be implemented in the callback. Non-zero indicates that the current API call operation failed, and the callback will not be triggered. For detailed errors, refer to the error codes.

Code Example


const callback = {
onSuccess: (t: RCIMIWMessage) => {
//...
},
onError: (code: number) => {
//...
},
};
let code = await engine.getMessageById(messageId, callback);

Retrieve Message by messageUId

Developers can use this API to retrieve a specific message.

Method


getMessageByUId(
messageUId: string,
callback: IRCIMIWGetMessageCallback
): Promise<number>;

Parameter Description

ParameterTypeDescription
messageUIdstringThe messageUid of the message, which can be obtained from the message object. Only successfully sent messages will have this value.
callbackIRCIMIWGetMessageCallbackCallback for API call result.

Return Value

Return ValueDescription
numberStatus code for the current API operation. 0 indicates a successful call, and the specific result needs to be implemented in the callback. Non-zero indicates that the current API call operation failed, and the callback will not be triggered. For detailed errors, refer to the error codes.

Code Example


const callback = {
onSuccess: (t: RCIMIWMessage) => {
//...
},
onError: (code: number) => {
//...
},
};
let code = await engine.getMessageByUId(messageUId, callback);