Skip to main content

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 the connect 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)
ParameterTypeDescription
messageTypestringMessage type
isPersitedbooleanWhether to store the message
isCountedbooleanWhether to count the message
searchPropsstring[]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.
isStatusMessagebooleanWhether it is a status message. Status messages are not stored or counted and can only be received when the recipient is online.
tip
  1. RongIMLib.registerMessageType must be called before connect; otherwise, it may cause abnormal message receiving behavior.
  2. 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)
}
})