Insert Message (Electron)
This document is only applicable to the Electron solution and is limited to use with the Electron modules (@rongcloud/electron and @rongcloud/electron-renderer).
The SDK supports inserting messages into the local database of Electron. Messages inserted locally are not actually sent to the server or the recipient.
Insert a Single Message
Insert a message in the local conversation with either a sending or receiving direction. The message is stored in the local database but is not sent to the server or the recipient. The chatroom conversation type is not supported. The following example uses the electronExtension.insertMessage method to insert an IAReceivedMessage.
const conversation = {
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: "<Target ID>"
}
const message = {
senderUserId: '<userId>',
messageType: RongIMLib.MessageType.TEXT,
content: {
content: 'Insert a message'
},
messageDirection: RongIMLib.MessageDirection.SEND
}
RongIMLib.electronExtension.insertMessage(conversation, message).then(res => {
if (res.code === RongIMLib.ErrorCode.SUCCESS) {
// The message was inserted successfully, returning the message data of type IAReceivedMessage.
console.log('Message inserted successfully', res.data)
} else {
console.log('Failed to insert message', res.code, res.msg)
}
})
- When inserting IAReceivedMessage, the message direction can be controlled via
messageDirection
. - Starting from version 5.6.1, the SDK supports inserting BaseMessage. When inserting
BaseMessage
, the message direction cannot be controlled, and the SDK defaults to inserting a message in the sending direction. - Messages inserted into the local database that need to support message extension functionality (see Message Extension) must use IAReceivedMessage and enable the message's extensible property (
canIncludeExpansion
).
Parameter | Type | Description |
---|---|---|
conversation | IConversationOption | Conversation |
message | BaseMessage (recommended) or IAReceivedMessage | The message to be inserted. Support for inserting BaseMessage starts from version 5.6.1. |
options | IInsertOptions | Options |
options
defines some optional configurations in the sending behavior. See IInsertOptions
Parameter | Type | Description |
---|---|---|
isUnread | boolean | Whether the inserted message is counted as unread: true counts, false does not count, default is not counted |
searchContent | string | Message search keyword (optional parameter) |
disableUpdateLastMessage | boolean | Disable updating the last message of the conversation, default is false, supported starting from version 5.12.2. |
Batch Insert Messages
tip
- Supported starting from version 5.7.2.
- A maximum of 500 messages can be inserted at a time. Chatrooms and ultra groups are not supported.
Insert messages in bulk into the local database.
const messages = [{
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: '<Target ID>'
senderUserId: '<userId>',
messageType: RongIMLib.MessageType.TEXT,
content: {
content: 'Insert a message'
},
messageDirection: RongIMLib.MessageDirection.SEND,
messageUId: '<Message UID>',
}]
const checkDuplicate = false
RongIMLib.electronExtension.batchInsertMessage(messages, checkDuplicate).then(res => {
if (res.code === RongIMLib.ErrorCode.SUCCESS) {
// Messages inserted successfully
console.log('Batch insert successful', res.data)
} else {
// If the message format is incorrect, res.msg returns the index of the message in the array
console.log('Batch insert failed', res.code, res.msg)
}
})
Parameter | Type | Description |
---|---|---|
messages | IInsertMessage[] | Array of messages to be inserted in bulk |
checkDuplicate | Boolean | Whether to deduplicate messages |