Skip to main content

Get Conversation

Note

  • The Web platform does not have persistent data storage capabilities, so it cannot locally store historical message records and conversation lists. Therefore, data needs to be retrieved from the RC server. To fetch historical one-to-one and group chat messages from the remote server, you must enable the Cloud Storage for One-to-One and Group Messages service for the current App Key in the Chat pricing plans page of the Console. This service is available for Chat Premium Plan or Chat Ultimate Plan. For specific features and pricing, refer to the Billing Guide.
  • Fetching remote conversations must be executed after successfully establishing a connection by calling RongIMLib.connect().
  • The server conversation list storage limit is 1000 conversations, and the storage validity period is consistent with the Cloud Storage for One-to-One and Group Messages, which defaults to 6 months.

Fetch Conversation List

tip

This interface ignores parameter values when called on the Electron platform and always returns all conversation lists. Electron platform users are advised to consider using the Electron-specific interface for fetching conversation lists.

Usage Recommendations

When calling this interface on the Web platform, the data retrieved is stored remotely. To enhance performance and reduce interface call risks, we recommend handling it as follows:

Initial Data Fetch: After pulling offline messages, the business layer can actively call this interface once to assign the returned conversation data to the rendering data, ensuring the integrity of the initial state.

Data Update Method: Subsequent updates to conversation data will be returned via the Conversation Change Notification Event Listener. The business layer can merge the returned data with local rendering data without frequently calling the getConversationList interface.

Avoid Frequent Interface Calls: Do not actively call the getConversationList interface every time a message is sent or received to fetch remote conversation list data, as this may lead to the following issues:

  1. It may cause interface errors or request failures.
  2. The returned conversation data may not be the latest.
  3. Multiple remote data fetches will increase network load and degrade application performance.

Call getConversationList to fetch conversations, returning a list of IAReceivedConversation.

const startTime = 0;
const count = 10;
const order = 0;

RongIMLib.getConversationList({
count: count,
startTime: startTime,
order: order
}).then(res => {
if (res.code === 0) {
console.log(res.code, res.data)
} else {
console.log(res.code, res.msg)
}
})

The server sorts conversations based on the time of the last message. Fetching methods are as follows:

  • To fetch the latest conversation list, set order to 0 and startTime to 0, indicating that the query should use the current time as the boundary to fetch conversations earlier than the current time. The interface will return the latest conversation list.
  • To fetch the earliest conversations from the server, set order to 1 and startTime to 0. Here, startTime = 0 means starting the query from the earliest conversation on the server, fetching the specified number of conversations. The interface will return the earliest conversation list from the server's 1000 conversations.
  • For paginated fetching, the second call should pass the sendTime of the latestMessage from the last IAReceivedConversation in the returned result array.
ParameterTypeRequiredDefaultDescription
countNumberNo300Number of conversations to fetch, default 300, maximum 1000.
startTimeNumberNo0Time boundary for fetching conversations, a timestamp in milliseconds, used in conjunction with order. For paginated fetching, the second call can pass the sendTime of the latestMessage from the last IAReceivedConversation in the returned result array.
orderNumberNo0Direction of fetching conversations. 0 means fetching conversations earlier than the time boundary (startTime). 1 means fetching conversations later than the time boundary (startTime).

Description of IAReceivedConversation properties in the returned result:

Field NameTypeDescription
conversationTypeNumberConversation type
targetIdStringThe userId of the recipient
unreadMessageCountNumberNumber of unread messages in the current conversation
latestMessageObjectThe last message in the conversation
hasMentionedBooleanWhether the conversation contains messages that mention you. This data is only valid when conversationType is ConversationType.GROUP.
mentionedInfoMentionedInfoMention information.
notificationStatusNumberCurrent conversation's Do Not Disturb status. This field is deprecated in the Web platform as of version 5.3.0 and in the Electron platform as of version 5.8.4. Please use notificationLevel.
notificationLevelNotificationLevelCurrent conversation's Do Not Disturb level. Supported in the Web platform as of version 5.3.0 and in the Electron platform as of version 5.8.4.
isTopBooleanCurrent conversation's pin status
lastUnreadTimeNumberLast unread time of messages in the conversation
draftStringDraft information, added in version 5.9.0

Fetch a Specific Conversation

Call getConversation to fetch a specific conversation IAReceivedConversation.

tip

The conversation fetched via this method may not exist in the current conversation list. This is only a functional syntactic sugar encapsulation.

const conversationType = RongIMLib.ConversationType.PRIVATE;
const targetId = 'The userId of the recipient';

RongIMLib.getConversation({
conversationType,
targetId,
}).then(res => {
if (res.code === 0) {
console.log(res.code, res.data)
} else {
console.log(res.code, res.msg)
}
})
ParameterTypeRequiredDescription
targetIdStringYesThe userId of the recipient
conversationTypeNumberYesConversation type, refer to ConversationType.

Fetch Pinned Conversation List

Call getTopConversationList to fetch the pinned conversation list.

tip

Supported since SDK version 5.6.0. Note: The returned data on the Web platform will not include the latestMessage field.

const conversationTypes = [RongIMLib.ConversationType.PRIVATE, RongIMLib.ConversationType.GROUP, RongIMLib.ConversationType.SYSTEM];

RongIMLib.getTopConversationList(conversationTypes).then(res => {
if (res.code === 0) {
console.log(res.code, res.data)
} else {
console.log(res.code, res.msg)
}
})
ParameterTypeRequiredDescription
conversationTypesConversationType[]NoConversation types, refer to ConversationType. Defaults to all conversation types.

Fetch Unread Conversation List

tip

Supported since SDK version 5.7.0. Note: The returned data on the Web platform will not include the latestMessage field.

Call getUnreadConversationList to fetch the list of conversations with unread messages.

const conversationTypes = [RongIMLib.ConversationType.PRIVATE, RongIMLib.ConversationType.GROUP, RongIMLib.ConversationType.SYSTEM];

RongIMLib.getUnreadConversationList(conversationTypes).then(res => {
if (res.code === 0) {
console.log(res.code, res.data)
} else {
console.log(res.code, res.msg)
}
})
ParameterTypeRequiredDescription
conversationTypesConversationType[]NoConversation types. Refer to ConversationType, only supports one-to-one chat, group chat, and system conversation types.