Browse SDKs · HarmonyOS
Platform
SDKsHarmonyOSEnterprise

Send message

Send a prepared message object to a direct chat peer or a group chat.

Copy

sendMessage takes an IMMessage object returned by one of the message creation methods. For direct chats, pass recvID and set groupID to an empty string ""; for group chats, pass groupID and set recvID to "".

Parameters

ParameterTypeRequiredDescription
messageIMMessageYesPrepared message object to send.
recvIDstringConditionally requiredPeer user ID for direct chats; empty string "" for group chats.
groupIDstringConditionally requiredTarget group ID for group chats; empty string "" for direct chats.
isOnlineOnlybooleanYesWhether to deliver only to currently online clients; online-only messages do not persist in local history.

Code example

import sdk, {
  EventOnSendMsgProgressData,
  OpenIMSDKEvent,
  SendMessageReq,
  SendMessageResp,
} from '@openimsdk/imsdk';

const request: SendMessageReq = {
  message: message,
  recvID: receiverUserID,
  groupID: '',
  isOnlineOnly: false,
};
const response: SendMessageResp = await sdk.sendMessage(request);
replacePendingMessage(response.message.clientMsgID, response.message);

const handleSendProgress = (data: EventOnSendMsgProgressData): void => {
  updateMessageProgress(data.clientMsgID, data.progress);
};
const unsubscribeSendProgress = sdk.on(OpenIMSDKEvent.EventOnSendMsgProgress, handleSendProgress);

export function removeSendProgressListener(): void {
  unsubscribeSendProgress();
}

Return value

Returns a Promise resolving to SendMessageResp:

PropertyTypeDescription
messageIMMessageUpdated message object with server message ID, delivery status, and timestamps.

State changes after the call

When sending local media, upload progress events are tracked using clientMsgID; do not associate progress with list array indexes. Progress updates do not guarantee successful transmission; determine success from Promise resolution and final message status flags. Upon Promise resolution, swap the pending message placeholder with the returned message object. Receipt of the message by remote clients occurs in a separate stage. Call removeSendProgressListener() when tearing down the sending layer.