Browse SDKs · React Native
Platform
SDKsReact Native

Send message

Send a message to a one-to-one or group conversation with automatic media upload using the OpenIM React Native SDK.

Copy

Dispatch a MessageItem constructed by a message creation method to a designated one-to-one or group conversation:

When sending image, audio, video, or file messages created with local filesystem paths, the SDK automatically uploads the media files prior to dispatch. If media files are already hosted on your own server or object storage and created via URL, use Send uploaded media message.

const sentMessage = await OpenIMSDK.sendMessage({
  recvID: receiverUserID,
  groupID: '',
  message,
});

Parameters

ParameterTypeRequiredDescription
recvIDstringConditionalRecipient user ID for one-to-one chats; pass an empty string "" for group chats.
groupIDstringConditionalTarget group ID for group chats; pass an empty string "" for one-to-one chats.
messageMessageItemYesMessage object generated by a message creation method.
offlinePushInfoOfflinePushNoCustom offline push notification title, content, and passthrough payload.
isOnlineOnlybooleanNoWhether to deliver solely to currently online clients without storing in server offline history.

Return value

A resolved Promise returns the dispatched MessageItem containing server-assigned properties such as serverMsgID, sendTime, and updated status.

State changes and lifecycle phases

  1. Promise resolution: Indicates the message has been written to the local database and accepted by the OpenIM server dispatch queue. It does not signify that the counterparty has received or read the message.
  2. Event arrival: During media uploads, the SDK periodically emits SendMessageProgress events. When the counterparty receives or reads the message, corresponding delivery or read-receipt events are triggered.
  3. Re-query reconciliation: If disconnected or encountering timeout errors, invoke Load older messages upon reconnecting to verify the final message status.

Always use clientMsgID as the unique key to transition message state from "sending" to "success" or "failed" in local UI state.

Listen for media upload progress

Track upload progress when dispatching large files, high-resolution videos, or audio:

interface SendMessageProgressPayload {
  clientMsgID: string;
  progress: number;
}

const handleSendMessageProgress = ({
  clientMsgID,
  progress,
}: SendMessageProgressPayload) => {
  // Locate the pending message by clientMsgID and update UI upload percentage
};

// Register listener
OpenIMSDK.on(OpenIMEvent.SendMessageProgress, handleSendMessageProgress);

// Unregister listener on cleanup
OpenIMSDK.off(OpenIMEvent.SendMessageProgress, handleSendMessageProgress);

Unregister the listener using the same function reference when unmounting screens or signing out.