Skip to main content

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).
ParameterTypeDescription
conversationIConversationOptionConversation
messageBaseMessage (recommended) or IAReceivedMessageThe message to be inserted. Support for inserting BaseMessage starts from version 5.6.1.
optionsIInsertOptionsOptions
  • options defines some optional configurations in the sending behavior. See IInsertOptions
ParameterTypeDescription
isUnreadbooleanWhether the inserted message is counted as unread: true counts, false does not count, default is not counted
searchContentstringMessage search keyword (optional parameter)
disableUpdateLastMessagebooleanDisable 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)
}
})
ParameterTypeDescription
messagesIInsertMessage[]Array of messages to be inserted in bulk
checkDuplicateBooleanWhether to deduplicate messages