Real-time Messaging and Signaling
Feature Introduction
ZEGO provides multiple text message sending and receiving functions to implement sending room broadcast messages, bullet screen messages, simple IM text chat, likes, gift-giving, quizzes, and other functions.
| Message Type | Feature Description | Application Scenarios | Sending Frequency Limit |
|---|---|---|---|
| Broadcast Message | Send text messages to all users in the same room. | Can be used for simple in-room chat room scenarios. | Please refer to Restrictions. |
| Barrage Message | During live streaming, viewers can express their opinions, which are displayed as scrolling subtitles, increasing interactivity among viewers. | Generally used in scenarios with large amounts of message sending and receiving in the room, and where message reliability does not need to be guaranteed, such as live streaming bullet screens. | Please refer to Restrictions. |
| Custom Signaling | Send messages to one or more users in the same room. | Generally used for remote control signaling or simple text message sending. For example, in an online doll machine scenario, remotely control the movement of the doll machine's gripper. | Please refer to Restrictions. |
In addition, ZEGO also provides a complete Instant Messaging ZIM SDK, providing developers with full-platform interaction, massive concurrency, ultra-low latency, and guaranteed message delivery communication services. For details, please refer to In-app Chat.
Download Example Source Code
Please refer to Download Example Source Code to get the source code.
For related source code, please check files in the "lib\topics\CommonFunctions\room_message" directory.
Prerequisites
Before sending real-time messages, please ensure:
- ZEGO Express SDK has been integrated into the project to implement basic real-time audio and video functions. For details, please refer to Quick Start - Integration and Quick Start - Implementation Process.
- A project has been created in the ZEGOCLOUD Console, and a valid AppID and AppSign have been applied for. For details, please refer to Console - Project Information.
Usage Steps
Send and Receive Broadcast Messages
- Send Broadcast message
Call the sendBroadcastMessage interface to send broadcast messages to other users in the same room. The length cannot exceed 1024 bytes.
Get the message sending result through the function return ZegoIMSendBroadcastMessageResult.
// Send broadcast message
ZegoExpressEngine.instance.sendBroadcastMessage(roomID, msg).then((ZegoIMSendBroadcastMessageResult result) {
// Handle success or failure of message sending
});- Receive Broadcast message
Implement the ZegoExpressEngine.onIMRecvBroadcastMessage callback. After the sender successfully sends a broadcast message, other users in the same room receive relevant information through this callback, including message content, message ID, sending time, and sender information.
/**
* Receive room broadcast message notification
*
* @param roomID Room ID
* @param messageList List of received messages
*/
ZegoExpressEngine.onIMRecvBroadcastMessage = (String roomID, List<ZegoBroadcastMessageInfo> messageList) {
// Handle receiving messages from other users
};Send and Receive Barrage Messages
- Send Barrage message
Call the sendBarrageMessage interface to send barrage messages to other users in the same room. The length cannot exceed 1024 bytes.
Get the message sending result through the function return ZegoIMSendBarrageMessageResult.
// Send barrage message
ZegoExpressEngine.instance.sendBarrageMessage("ChatRoom-1", "This is a barrage message").then((ZegoIMSendBarrageMessageResult result){
// Handle success or failure of message sending
});- Receive Barrage message
Implement the ZegoExpressEngine.onIMRecvBarrageMessage callback. After the sender successfully sends a barrage message, other users in the same room receive relevant information through this callback, including message content, message ID, sending time, and sender information.
/**
* Receive room barrage message notification
*
* @param roomID Room ID
* @param messageList List of received messages
*/
ZegoExpressEngine.onIMRecvBarrageMessage = (String roomID, List<ZegoBarrageMessageInfo> messageList) {
// Handle receiving messages from other users
};Send and Receive Custom Signaling
- Send Custom signaling
Call the sendCustomCommand interface to send custom signaling to users specified by "toUserList" in the same room. The length cannot exceed 1024 bytes.
Get the message sending result through the function return ZegoIMSendCustomCommandResult.
// Send custom signaling, only users specified in `toUserList` can receive this signaling through onIMSendCustomCommandResult
// If `toUserList` parameter is `[]`, the SDK will send this signaling to all users in the room
ZegoExpressEngine.instance.sendCustomCommand(roomID, command, toUserList).then((ZegoIMSendCustomCommandResult result) {
// Handle success or failure of message sending
});- Receive Custom signaling
Implement the ZegoExpressEngine.onIMRecvCustomCommand callback. After the sender successfully sends a custom message, specified users in the same room receive relevant information through this callback, including message content and message sender information.
/**
* Receive custom signaling notification
*
* @param roomID Room ID
* @param fromUser Sender of the signaling
* @param command Signaling content
*/
ZegoExpressEngine.onIMRecvCustomCommand = (String roomID, ZegoUser fromUser, String command) {
// Handle receiving messages from other users
};