Skip to main content

Receive Messages

The app can listen to messages received by the SDK and perform corresponding operations based on business requirements.

Recommendation
  • Register message listeners during the app's lifecycle.

  • Remove message listeners when the app lifecycle ends.

Setting Up Message Listeners

Call the [addEventListener] method to set up a listener for SDK message reception events. All received messages will be returned via this callback method.

Sample Code

const Events = RongIMLib.Events
const listener = (evt) => {
console.log(evt.messages)
};
RongIMLib.addEventListener(Events.MESSAGES, listener)
In the above example, the parameter of the listener callback function is an [IMessageEvent] object. The `messages` property contains the list of received messages, with each message being of type [IAReceivedMessage].

**`IAReceivedMessage` Parameter Description**

| Parameter | Type | Description |
|:----|:----|:----|
|messageType | string | Message type identifier (Object Name), e.g., `RC:TxtMsg`. For built-in message types, refer to [Message Type Overview].|
|content | Object | Message content. For text messages, the format is `{ content: 'text content' }`. See the [Sending Messages](https://docs.rongcloud.cn/web-imlib/message/send) documentation for details.|
|senderUserId| string | User ID of the message sender. |
|targetId|string| Conversation ID (or Target ID), used to identify the conversation counterpart. <ol><li>For one-to-one chats, the Target ID is the user ID of the counterpart. Thus, all sent and received messages will have the counterpart's user ID as the Target ID. **Note**: The Target ID stored in the local message data for messages received by the current user is not the current user's ID but the user ID of the counterpart (the message sender).</li><li>For group chats, chatrooms, and ultra groups, the Target ID is the corresponding group, chatroom, or ultra group ID.</li><li>For system conversations, since client users cannot reply to system messages, no Target ID is required.</li></ol>|
|channelId|string| Business identifier for the conversation.|
|conversationType| [ConversationType] |Conversation type.|
|sentTime|number|Message sending time on the server.|
|receivedTime|number|Message reception time.|
|messageUId|string|Message ID stored on the server. |
|messageDirection|[MessageDirection]|Message direction: 1: Sent, 2: Received.|
|isPersited|boolean|Whether the message is stored.|
|isCounted|boolean|Whether the message is counted.|
|isOffLineMessage|boolean|Whether the message is an offline message.|
|isMentioned|boolean|Whether the message is an @ mention.|
|disableNotification|boolean|Whether the message is silent.|
|isStatusMessage|boolean|Whether the message is a status message.|
|canIncludeExpansion|boolean|Whether message extensions are supported.|
|expansion|`{ [key: string]: any }|null`|Message extensions.|
|receivedStatus|[ReceivedStatus] | Message reception status (Electron-only field, deprecated as of version 5.9.3; use receivedStatusInfo instead).|
|receivedStatusInfo|[IReceivedStatusInfo] | Detailed message reception status (Electron-only field, supports multiple concurrent statuses).|
|pushConfig|[IPushConfig]|Push extensions (similar to MessagePushConfig on Android and iOS).|
|messageId|number| Local message ID (Electron-only field).|
|directedUserIds|string[]| List of target users for directed messages. If empty, the message is not a directed message. SDK supports retrieving the target user list for directed messages starting from version 5.9.6.|


## Removing Message Listeners \{#removeEventListener}

Call [removeEventListener] to remove the message reception listener.


#### Sample Code

```js
RongIMLib.removeEventListener(Events.MESSAGES, listener)


## Disabling Message Deduplication (Electron Only) \{#disableMessageDuplicate}

The SDK enables message deduplication by default, automatically removing duplicate one-to-one, group, and system messages. When local message data volume is large, deduplication logic may cause message reception delays. In such cases, you can try disabling deduplication.


### How to Disable Message Deduplication \{#setCheckDuplicateMessage}

:::tip


- Starting from SDK version 5.7.1, message deduplication can be disabled.


- Chatroom and ultra group conversation types do not support disabling deduplication.
:::

Ensure you call [setCheckDuplicateMessage](https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules/electronExtension.html#setCheckDuplicateMessage) after SDK initialization is complete and before establishing an IM connection. For multiple calls, the last setting takes effect.


#### Interface

```javascript
RongIMLib.electronExtension.setCheckDuplicateMessage(enableCheck);


#### Parameter Description

| Parameter | Type | Required | Description |
|:--------|:-------|:-------|:------ |
| enableCheck | Boolean | No | Whether to use RC's message deduplication mechanism. true: Enable, false: Disable |


#### Sample Code

```javascript
const enableCheck = false;
RongIMLib.electronExtension.setCheckDuplicateMessage(enableCheck);


### Why Duplicate Messages Occur

When the sender is in a weak network environment, message resending may occur. Example scenario: