Browse SDKs · uni-app / uni-app x
Platform
SDKsuni-app / uni-app x

Send a message

Transmit a prepared message object to a single-chat recipient or group.

Copy

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

ParameterTypeRequiredDescription
messageOpenIMMessageItemYesNon-null message object generated by a creation method.
recvIDstringConditionallyPeer user ID for single chats; pass an empty string "" for group chats.
groupIDstringConditionallyTarget group ID for group chats; pass an empty string "" for single chats.
offlinePushInfoOpenIMOfflinePush | nullNoOffline push title, description, and sound configuration; pass null for defaults.
isOnlineOnlybooleanNoWhen 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.