Custom Message
In addition to using the built-in messages provided by the SDK, developers can customize messages according to their business needs. The message type and structure of custom messages must be consistent across multiple platforms; otherwise, interoperability issues may arise.
Notes:
- The code for registering custom messages must be executed before sending or receiving the custom messages.
- It is recommended to place all custom message registration code after the
init
method and before theconnect
method. - The message type names must be consistent across multiple platforms; otherwise, the messages cannot be interoperable.
- Do not use type names starting with
RC:
to avoid conflicts with the default message names in the SDK.
Registering Custom Messages
Use the RongIMLib.registerMessageType
method to register custom messages. This method returns the constructor for the custom message.
const PersonMessage = RongIMLib.registerMessageType('s:person', true, true, [], false)
Parameter | Type | Description |
---|---|---|
messageType | string | Message type |
isPersited | boolean | Whether to store the message |
isCounted | boolean | Whether to count the message |
searchProps | string[] | Search fields. Not required for web. If the search field value is a number, its range should be (-Math.pow(2, 64), Math.pow(2, 64)) and it must be an integer. |
isStatusMessage | boolean | Whether it is a status message. Status messages are not stored or counted and can only be received when the recipient is online. |
tip
RongIMLib.registerMessageType
must be called beforeconnect
; otherwise, it may cause abnormal message receiving behavior.- It is recommended to register all custom messages involved in the application at once, and call the registration for the same type of message only once for easier management.
Sending Custom Messages
API Reference: sendMessage
// Construct the custom message to be sent
const message = new PersonMessage({ name: 'someone', age: 18 })
// Send the message
RongIMLib.sendMessage({
conversationType: RongIMLib.ConversationType.PRIVATE,
targetId: '<targetId>'
}, message).then(res => {
if (res.code === 0) {
console.log(res.code, res.data)
} else {
console.log(res.code)
}
})