Skip to main content

Send Messages

This document explains how to send messages using IMLib.

The client SDK has a rate limit for sending messages, with a maximum of 5 messages per second.

Send Regular Messages

Developers can use the following interface to send text messages, quoted messages, and custom messages.

Method


sendMessage(
message: RCIMIWMessage,
callback: RCIMIWSendMessageCallback
): Promise<number>;

Parameter Description

ParameterTypeDescription
messageRCIMIWMessageThe message object to be sent
callbackRCIMIWSendMessageCallbackCallback for the result of the interface call.

Return Value

Return ValueDescription
numberThe status code of the current interface operation. 0 indicates success. For specific results, implement the interface callback. Non-zero values indicate failure, and the callback will not be triggered. Refer to the error codes for details.

Code Example


const callback = {
onMessageSaved: (message: RCIMIWMessage) => {
//...
},
onMessageSent: (code: number, message: RCIMIWMessage) => {
//...
},
};
let code = await engine.sendMessage(message, callback);

Send Media Messages

Developers can use the following interface to send image messages, voice messages, video messages, file messages, and GIF messages.

Method


sendMediaMessage(
message: RCIMIWMediaMessage,
listener: RCIMIWSendMediaMessageListener
): Promise<number>;

Parameter Description

ParameterTypeDescription
messageRCIMIWMediaMessageThe media message object to be sent
listenerRCIMIWSendMediaMessageListenerListener for media message sending events

Return Value

Return ValueDescription
numberThe status code of the current interface operation. 0 indicates success. For specific results, implement the interface callback. Non-zero values indicate failure, and the callback will not be triggered. Refer to the error codes for details.

Code Example


const listener = {
onMediaMessageSaved: (message: RCIMIWMediaMessage) => {
currentSendingMediaMessage = message;
//...
},
onMediaMessageSending: (message: RCIMIWMediaMessage, progress: number) => {
//...
},
onSendingMediaMessageCanceled: (message: RCIMIWMediaMessage) => {
//...
},
onMediaMessageSent: (code: number, message: RCIMIWMediaMessage) => {
//...
},
};
let code = await engine.sendMediaMessage(message, listener);

Handling Message Sending Failures

For message types that are stored locally on the client (refer to Message Introduction), if the message storage callback is triggered (setOnMessageAttachedListener / setOnMediaMessageAttachedListener), it means the message has been stored in the local database and added to the local message list.

  • If the message fails to send after being stored (the setOnMessageSentListener / setOnMediaMessageSentListener callback is not triggered), the app can temporarily display this failed message in the UI and cache the message instance returned by onMessageAttached. The app can then retry sending the message by calling sendMessage / sendMediaMessage at an appropriate time. Note that if a new message instance is created instead of reusing the cached one, the local message list will store two duplicate messages.
  • If storage fails, check if the parameters are valid and if the device storage is writable.

For messages that are not stored locally on the client, if sending fails (the setOnMessageSentListener/setOnMediaMessageSentListener callback is not triggered), the app can cache the message instance and retry sending, or create a new message instance for sending.

Implementing Message Forwarding and Bulk Sending

Message Forwarding: IMLib SDK does not provide a forwarding interface. To implement message forwarding, apps can call the send message interface.

Bulk Sending: Bulk sending refers to sending messages to multiple users. More accurately, it means sending messages to multiple Target IDs?, where a Target ID may represent a user, a group, or a chatroom. The client SDK does not provide a direct API for bulk sending. Consider the following implementation methods:

  • The app can loop through the client's send message API. Note that the client SDK limits the message sending rate to a maximum of 5 messages per second.
  • The app can integrate the IM Server API on the business server side and let the business server handle bulk sending. The IM Server API's Send one-to-one chat messages interface supports sending one-to-one messages to multiple users in one go. You can also use the Send Group Message and Send Chatroom Message interfaces to send messages to multiple groups or chatrooms in one go.