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

Message overview

OpenIM uni-app / uni-app x SDK guide for Message overview.

Copy

The uni-app / uni-app x plugin represents every message with OpenIMMessageItem. Sending has two phases: create an outgoing message object for the desired content, then send that object to a user or group. A create method does not send anything, and a successful send Promise does not prove that another client has received the message.

Realtime delivery, history, search, and message management are all conversation-scoped. Merge send results, realtime events, and history idempotently with the composite key conversationID:clientMsgID: conversationID identifies the conversation and clientMsgID identifies the message.

Message processing flow

PhaseMain operationNotes
CreateCall the appropriate create*Message() methodReturns an OpenIMMessageItem; it does not write to the server or emit a new-message event.
SendCall sendMessage() or sendMessageNotOss()For a single chat, set recvID; for a group chat, set groupID. Pass an empty string for the unused target.
ReceiveSubscribe to new-message eventsResolve the target conversation from the routing fields and merge by clientMsgID.
QueryLoad history, search, or find messages by IDQueries return a snapshot and do not emit new-message events.
UpdateDelete, revoke, modify, pin, or report read statusHandle the Promise, related events, and any required reconciliation query separately.

Messages created from a readable native image, audio, video, or file path are uploaded by sendMessage(). If your application already uploaded the media and has a URL, use the corresponding create*MessageByURL() method and send it with sendMessageNotOss() to avoid uploading it again.

OpenIMMessageItem structure

FieldTypeDescription
clientMsgIDstring or nullStable client ID used for deduplication, state updates, lookup, and pagination cursors.
serverMsgIDstring or nullServer message ID; an unsent or failed message may not have one.
sessionTypeOpenIMSessionTypeConversation type.
sendID, recvID, groupIDstring or nullSender and single/group routing fields.
contentTypeOpenIMMessageTypeContent type that determines which elem field to read.
createTime, sendTimenumberCreation and send times.
seqnumberServer sequence number.
senderPlatformIDOpenIMPlatformSender platform.
senderNickname, senderFaceUrlstring or nullSender profile snapshot.
statusOpenIMMessageStatusCurrent send status.
isReadbooleanCurrent read-state snapshot.
offlinePushOpenIMOfflinePush or nullOffline-push settings used for the send.
content, attachedInfostring or nullSDK-serialized content and attached information.
exstring or nullExtension string synchronized with the message.
localExstring or nullExtension string stored only on this device.

Read message bodies from the elem that matches contentType: textElem for text; pictureElem, soundElem, videoElem, and fileElem for media; atTextElem and quoteElem for mentions and replies; mergeElem and customElem for merged and custom messages; cardElem, locationElem, and faceElem for cards, locations, and emoji; and advancedTextElem, typingElem, and notificationElem for advanced text, typing, and notifications. Do not infer the message type from display text or array position.

conversationID identifies the containing conversation but is not an OpenIMMessageItem field. Obtain it from the active conversation, query condition, search result, or event context, then merge state by conversationID:clientMsgID.

Create messages with different content types

ContentPageNotes
Text and MarkdownCreate a text message, Create a Markdown messageRender Markdown safely on the receiver.
Group mentionsCreate an @ messageCan only be sent to a group.
Images, audio, video, and filesCreate an image from a full path, Create an image from a URLOther media types use the same local-path or pre-uploaded URL flow.
Cards, locations, and emojiCreate a card message, Create a location message, Create a face messageCreation stores a content snapshot.
Replies, forwarding, and mergingCreate a quote message, Create a forwarded message, Create a merged messageThe returned object still must be sent explicitly.
Custom business contentCreate a custom messageReceivers must validate the business schema.

Put device-only presentation state in localEx, not in business content that must synchronize to other users. See Set a local message extension.

Progress events for message sends, file uploads, and log uploads belong to this page:

import {
  off,
  onSendMessageProgress,
  onUploadFileProgress,
  onUploadLogsProgress,
  type OpenIMSDKEventSubscription,
} from '@/uni_modules/unix-openim-sdk'

const sendProgressSubscription = onSendMessageProgress((event) => {
  updateMessageProgress(event.clientMsgID, event.progress)
})
const subscriptions : Array<OpenIMSDKEventSubscription> = [
  sendProgressSubscription,
  onUploadFileProgress((event) => updateCurrentUpload(event.progress)),
  onUploadLogsProgress((event) => updateLogUpload(event.progress)),
]

function removeProgressListeners() {
  subscriptions.forEach((subscription) => off(subscription))
}

Progress may repeat, skip values, or arrive before or after the final Promise. Display it monotonically and use the API result as the source of truth. Call removeProgressListeners() when logging out, switching accounts, or disposing the progress state.

A local media path must be readable by the native layer. Resolve unifile:// to a real sandbox path; use the by-URL create method for network URLs.

Find a page by task

TaskPage
Send a normal or pre-uploaded media messageSend a message, Send pre-uploaded media
Receive online, offline, and online-only messagesReceive messages
Load history or surrounding contextLoad older messages, Load message context
Find by ID or search local messagesFind messages by ID, Search messages
Delete, revoke, modify, or pinDelete saved messages, Revoke a message, Modify a message, Pin a message
Group member-level read statusSend group read receipts, Get group message readers
Typing status or speech recognitionUpdate typing status, Transcribe audio

State synchronization boundaries

The complete listeners for new messages, deletion, revocation, modification, pinning, group read status, and typing status live on their task pages. Message creation and pure query methods only establish snapshots from their Promise result and do not emit shared message events. For mutations, treat Promise completion, event delivery, and reconciliation queries as separate phases.

Conversation unread counts, total unread counts, and group mention reminders are conversation state. Maintain them with Mark a conversation as read, Maintain the total unread count, and the events on Retrieve the conversation list.