# 发送消息 (/zh-CN/realtime-media/rtm/build/messaging/send-message/mini-program) > For AI agents: see the complete documentation index at [llms.txt](/llms.txt). 发送消息是 RTM 的基本能力之一,本文主要介绍如何在不同类型的频道中发送消息。同时,你也可以查看[实现收发消息](/zh-CN/realtime-media/rtm/get-started/quick-start)快速构建一个示例程序并体验如何收发消息。 RTM 目前只支持字符串和二进制类型的消息数据格式。如果你的业务中使用了 JSON、Object 等其他数据类型,或者 protobuf 等第三方数据构建工具,那么你需要在发送消息之前对消息数据进行序列化。关于如何有效构建负载数据结构及推荐的序列化方式可以参考[消息负载结构化](/zh-CN/realtime-media/rtm/build/message-design-and-history/constructed)。 ## 在消息频道中发送消息 [#在消息频道中发送消息] 在 Message Channel 中,你只需调用 `publish` 方法即可发送消息,无需提前订阅频道。该方法一次只能向一个频道发送消息,如需向多个频道发送消息,你需要多次调用该方法。RTM 并不限制你向多少个频道发送消息,也不限制有多少用户向一个频道发送消息,但是对你同时向一个频道发送消息的频率有一定的限制,详见 [API 调用限制](/zh-CN/realtime-media/rtm/reference/usage-limits-and-errors/api-limits)。 :::info[信息] `publish` 方法仅适用于 Message Channel 和 User Channel。 ::: 发送字符串消息的示例代码如下: ```javascript // 发送字符串消息 const payload = "Hello Agora!"; const channelName = "chat_room"; const options = { customType: "PlainTxt", channelType: "MESSAGE", }; try { const result = await rtm.publish(channelName, payload, options); } catch (status) { const { operation, reason, errorCode } = status; console.log(`${operation} failed, ErrorCode: ${errorCode}, due to: ${reason}.`); } ``` 方法调用成功后,RTM 会在 100 毫秒内将你的消息发送给订阅该频道的所有在线用户。 ## User Channel 中发送消息 [#user-channel-中发送消息-7] 在 User Channel 中,你只需调用 `publish` 方法,将 `channelType` 参数设为 `USER`,并将 `channelName` 参数设为指定用户的 `userId`,即可向指定用户发送点对点消息。该方法一次只能向一位用户发送消息,如需向多位用户发送消息,你需要多次调用该方法。RTM 并不限制你向多少用户发送消息,也不限制有多少用户向你发送消息,但是对你向用户发送消息的频率有一定的限制。 :::info[信息] `publish` 方法仅适用于 Message Channel 和 User Channel。 ::: 发送字符串消息和二进制消息的示例代码如下: ```javascript tab="字符串消息" // 发送字符串消息 const payload = "Hello Agora!"; const userId = "Tony"; const options = { customType: "PlainTxt", channelType: "USER", }; try { const result = await rtm.publish(userId, payload, options); } catch (status) { const { operation, reason, errorCode } = status; console.log(`${operation} failed, ErrorCode: ${errorCode}, due to: ${reason}.`); } ``` ```javascript tab="二进制消息" // 发送二进制消息 const message = "Hello Agora!"; const payload = new TextEncoder().encode(message); const userId = "Tony"; const options = { customType:"ByteArray", channelType: "USER" } try { const result = await rtm.publish(userId, payload, options); } catch (status) { const { operation, reason, errorCode } = status; console.log(`${operation} failed, ErrorCode: ${errorCode}, due to: ${reason}.`); } ``` ## 在流频道中发送消息 [#在流频道中发送消息-8] :::note[不支持] RTM 微信小程序 SDK 不支持 Stream Channel。 ::: ## 消息负载包体积限制 [#消息负载包体积限制] RTM 对在 Message 类型频道中发送的消息负载包体积有限制,限制为 32 KB。消息负载包体积包含消息负载自身加上 `customType` 字段大小。当消息负载包体积超出限制时,你会收到以下返回值: ```javascript // Status { error = true; errorCode = -11010; operation = "publish"; reason = "Publish too long message."; } ``` 为避免因为消息负载包体积超限而导致发送失败,你可以在消息发送之前对消息负载包体积进行检查。