logo
On this page

Real-time Messaging and Signaling

2024-01-02

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 TypeFeature DescriptionApplication ScenariosSending Frequency Limit
Broadcast MessageSend text messages to all users in the same room.Can be used for simple in-room chat room scenarios.Please refer to Restrictions.
Barrage MessageDuring 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 SignalingSend 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:

Usage Steps

Send and Receive Broadcast Messages

  1. 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
});
  1. 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

  1. 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
});
  1. 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

  1. 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
});
  1. 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
  };

Previous

Room Connection Status

Next

Login to Multiple Rooms

On this page

Back to top