Send a message
Transmit a prepared message object to a single-chat recipient or group.
Invoke sendMessage() to transmit an OpenIMMessageItem constructed by one of the message creation methods. Supply recvID for single chats, or groupID for group chats; provide an empty string "" for the unused destination property.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
message | OpenIMMessageItem | Yes | Non-null message object generated by a creation method. |
recvID | string | Conditionally | Peer user ID for single chats; pass an empty string "" for group chats. |
groupID | string | Conditionally | Target group ID for group chats; pass an empty string "" for single chats. |
offlinePushInfo | OpenIMOfflinePush | null | No | Offline push title, description, and sound configuration; pass null for defaults. |
isOnlineOnly | boolean | No | When true, delivers exclusively to currently connected online devices without saving to local history. |
Code example
import { sendMessage } from '@/uni_modules/unix-openim-sdk'
const sentMessage = await sendMessage({
message: message,
recvID: receiverUserID,
groupID: '',
offlinePushInfo: null,
isOnlineOnly: false,
})
if (sentMessage.clientMsgID != null) {
replacePendingMessage(sentMessage.clientMsgID, sentMessage)
}Return value
The Promise resolves with the sent OpenIMMessageItem. On the sending device, replace the pending local optimistic item with the resolved message object using clientMsgID. Arrival of the message on remote recipient devices is governed by subsequent network events and cannot be inferred solely from Promise resolution.
Listen for upload progress
When sending local image, audio, video, or file media, centrally register onSendMessageProgress to receive real-time upload progress updates. The callback receives an OpenIMSendMessageProgressEvent | null object containing clientMsgID and the upload percentage in progress.
import {
OpenIMSendMessageProgressEvent,
off,
onSendMessageProgress,
} from '@/uni_modules/unix-openim-sdk'
const handleSendProgress = (
progress: OpenIMSendMessageProgressEvent | null,
) => {
if (progress == null) return
updateMessageProgress(progress.clientMsgID, progress.progress)
}
const progressSubscription = onSendMessageProgress(handleSendProgress)
function removeSendProgressListener() {
off(progressSubscription)
}Always index progress updates in your UI state by clientMsgID rather than relying on list indices. Reaching 100% progress signifies that the binary upload to storage completed; final message delivery confirmation remains dependent on the resolution of the sendMessage() Promise. Unregister the listener when unmounting components, logging out, or switching accounts.
Was this page helpful?