Send Message
This document explains how to send messages from the client SDK, applicable to one-to-one chat, group chat, and chatroom scenarios.
The client SDK has a message frequency limit - a maximum of 5 messages can be sent per second.
Sending Messages
sendMessage is the fundamental interface in IMLib for message transmission, supporting both built-in and custom message types. For specific message types, IMLib also provides multiple convenient syntactic sugar methods.
Interface
RongIMLib.sendMessage(conversation, message, options)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| conversation | IConversationOption | Yes | Target conversation |
| message | BaseMessage | Yes | Message instance to be sent. Must be a subclass of BaseMessage, either IMLib built-in messages (e.g., RongIMLib.TextMessage) or custom message instances registered via RongIMLib.registerMessageType() |
| options | ISendMessageOptions | No | Sending configuration options. Defines optional behaviors during message sending, such as expandability and push notifications |
Example
The following example demonstrates how to use sendMessage to send a text message mentioning "Zhang San" in a group chat.
1. Define Target Conversation and Message Content
Since this is a group @ message, mentionedInfo needs to be added.
// Define target conversation - here we define a group conversation
const conversation = { conversationType: RongIMLib.ConversationType.GROUP, targetId: '<Target ID>' }
// Instantiate the message to be sent - RongIMLib.TextMessage is a built-in text message type
const message = new RongIMLib.TextMessage({
// Text content
content: 'Text content',
// (Optional) Additional information attached to the message, transparently transmitted to the recipient
extra: 'Additional message information',
// For group messages requiring @ mentions, add the mentionedInfo field
mentionedInfo: {
// @ type: ALL or INDIVIDUAL
type: RongIMLib.MentionedType.SINGAL,
// List of @ users
userIdList: ['zhangsan'],
// @ content
mentionedContent: ''
}
})