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
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:
- It may cause interface errors or request failures.
- The returned conversation data may not be the latest.
- 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
to0
andstartTime
to0
, 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
to1
andstartTime
to0
. 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 thelatestMessage
from the last IAReceivedConversation in the returned result array.
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
count | Number | No | 300 | Number of conversations to fetch, default 300 , maximum 1000 . |
startTime | Number | No | 0 | Time 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. |
order | Number | No | 0 | Direction 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 Name | Type | Description |
---|---|---|
conversationType | Number | Conversation type |
targetId | String | The userId of the recipient |
unreadMessageCount | Number | Number of unread messages in the current conversation |
latestMessage | Object | The last message in the conversation |
hasMentioned | Boolean | Whether the conversation contains messages that mention you. This data is only valid when conversationType is ConversationType.GROUP . |
mentionedInfo | MentionedInfo | Mention information. |
notificationStatus | Number | Current 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 . |
notificationLevel | NotificationLevel | Current 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. |
isTop | Boolean | Current conversation's pin status |
lastUnreadTime | Number | Last unread time of messages in the conversation |
draft | String | Draft information, added in version 5.9.0 |
Fetch a Specific Conversation
Call getConversation to fetch a specific conversation IAReceivedConversation.
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)
}
})
Parameter | Type | Required | Description |
---|---|---|---|
targetId | String | Yes | The userId of the recipient |
conversationType | Number | Yes | Conversation type, refer to ConversationType. |
Fetch Pinned Conversation List
Call getTopConversationList to fetch the pinned conversation list.
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)
}
})
Parameter | Type | Required | Description |
---|---|---|---|
conversationTypes | ConversationType[] | No | Conversation types, refer to ConversationType. Defaults to all conversation types. |
Fetch Unread Conversation List
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)
}
})
Parameter | Type | Required | Description |
---|---|---|---|
conversationTypes | ConversationType[] | No | Conversation types. Refer to ConversationType, only supports one-to-one chat, group chat, and system conversation types. |