Receive Messages
The app can listen to messages received by the SDK and perform corresponding business operations.
- It is recommended to register the message listener during the app's lifecycle.
- It is recommended to remove the message listener when the app's lifecycle ends.
Setting up Message Listener
Call addEventListener to set up a message receiver listener. All received messages will be called back in this interface method. You can listen multiple times anywhere.
const Events = RongIMLib.Events
const listener = (evt) => {
console.log(evt.messages)
};
RongIMLib.addEventListener(Events.MESSAGES, listener)
In the above example, the listener returns an IMessageEvent object, and its messages
property contains a list of received messages (IAReceivedMessage).
IAReceivedMessage
Parameter Description
Parameter | Type | Description |
---|---|---|
messageType | string | Message type identifier (Object Name), e.g., RC:TxtMsg . For Object Name values of RC built-in message types, refer to Message Type Overview. |
content | Object | Message content. For example, if a text message is received, the content field value is { content: 'text message inner content' } . For details, see the Send Message document. |
senderUserId | string | User ID of the message sender |
targetId | string | Conversation ID (or Target ID), used to identify the conversation counterpart.
|
channelId | string | Business identifier of the conversation. |
conversationType | ConversationType | Conversation type. |
sentTime | number | Time when the message was sent on the server. |
receivedTime | number | Time when the message was received. |
messageUId | string | Message ID stored on the server. |
messageDirection | MessageDirection | Message direction: 1: Sent, 2: Received. |
isPersited | boolean | Whether it is stored. |
isCounted | boolean | Whether it is counted. |
isOffLineMessage | boolean | Whether it is an offline message. |
isMentioned | boolean | Whether it is an @ message. |
disableNotification | boolean | Whether the message is silent. |
isStatusMessage | boolean | Whether it is a status message. |
canIncludeExpansion | boolean | Whether message expansion is supported. |
expansion | { [key: string]: any }|null | Message expansion. |
receivedStatus | ReceivedStatus | Message receive status (Electron exclusive field, deprecated starting from version 5.9.3, recommended to use receivedStatusInfo). |
receivedStatusInfo | IReceivedStatusInfo | Detailed message receive status (Electron exclusive field, supports multiple statuses). |
pushConfig | IPushConfig | Push extension (similar to MessagePushConfig on Android and iOS). |
messageId | number | Local message ID (Electron exclusive field). |
directedUserIds | string[] | List of target users for directed messages. If this list is empty, it indicates that this message is not a directed message. SDK supports getting the list of target users for directed messages starting from version 5.9.6. |
Removing Message Listener
Call removeEventListener to remove the message receiver listener.
RongIMLib.removeEventListener(Events.MESSAGES, listener)
Disabling Message Deduplication (Electron Only)
The message deduplication mechanism automatically removes duplicate messages when the SDK receives one-to-one chat, group chat, and system messages. When there are a large number of messages locally in the app, the SDK's default deduplication mechanism may cause message reception lag due to performance issues. Therefore, when message reception lag occurs, you can try turning off the SDK's deduplication mechanism.
Why Duplicate Messages May Occur During Message Reception
This issue may occur when the sender is in a weak network condition. After A sends a message to B, the message successfully reaches the server and is delivered to the receiver B. However, A may not receive the ack returned by the server due to network issues, causing A to believe that the message was not sent successfully. If A resends the message, B will receive a duplicate message (the message content is the same, but the Message UID is different).
Turning Off Message Deduplication
The SDK supports turning off message deduplication starting from version 5.7.1. It does not support turning off message deduplication for chatroom and ultra group conversation types.
Please call this after SDK initialization and before establishing an IM connection. Multiple calls will take the last call as the final setting.
const enableCheck = false;
RongIMLib.electronExtension.setCheckDuplicateMessage(enableCheck);