Skip to main content

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

ParameterTypeDescription
messageTypestringMessage type identifier (Object Name), e.g., RC:TxtMsg. For Object Name values of RC built-in message types, refer to Message Type Overview.
contentObjectMessage 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.
senderUserIdstringUser ID of the message sender
targetIdstringConversation ID (or Target ID), used to identify the conversation counterpart.
  1. For one-to-one chat, the Target ID is the user ID of the counterpart. Therefore, the Target ID of all messages sent and received must be the user ID of the counterpart. Please note that the Target ID stored in the local message data for messages received by the local user is not the current user ID, but the user ID of the counterpart (the message sender).
  2. In group, chatroom, and ultra group conversations, the Target ID is the corresponding group, chatroom, or ultra group ID.
  3. For system conversations, since client users cannot reply to system conversation messages, no Target ID is needed.
channelIdstringBusiness identifier of the conversation.
conversationTypeConversationTypeConversation type.
sentTimenumberTime when the message was sent on the server.
receivedTimenumberTime when the message was received.
messageUIdstringMessage ID stored on the server.
messageDirectionMessageDirectionMessage direction: 1: Sent, 2: Received.
isPersitedbooleanWhether it is stored.
isCountedbooleanWhether it is counted.
isOffLineMessagebooleanWhether it is an offline message.
isMentionedbooleanWhether it is an @ message.
disableNotificationbooleanWhether the message is silent.
isStatusMessagebooleanWhether it is a status message.
canIncludeExpansionbooleanWhether message expansion is supported.
expansion{ [key: string]: any }|nullMessage expansion.
receivedStatusReceivedStatusMessage receive status (Electron exclusive field, deprecated starting from version 5.9.3, recommended to use receivedStatusInfo).
receivedStatusInfoIReceivedStatusInfoDetailed message receive status (Electron exclusive field, supports multiple statuses).
pushConfigIPushConfigPush extension (similar to MessagePushConfig on Android and iOS).
messageIdnumberLocal message ID (Electron exclusive field).
directedUserIdsstring[]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

tip

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);