logo
On this page

Real-Time Messaging and Signaling

2024-01-02

Feature Overview

ZEGO provides various text message sending and receiving functions, implementing features such as sending in-Room broadcast messages, barrage messages, simple IM text chat, likes, gift giving, quiz answering, and more.

Message TypeFunction DescriptionApplication ScenariosSend Frequency Limit
Broadcast MessagesSend text messages to all users in the same Room.Can be used for simple in-Room chat room scenarios.Please refer to Restrictions.
Barrage MessagesAudiences can express their comments during live streaming, which are displayed as sliding subtitles, increasing interaction between audiences.Generally used for 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 barrage.Please refer to Restrictions.
Custom CommandsSend messages to single or multiple users in the same Room.Generally used for remote control signaling or simple text message sending. For example, in online claw machine scenarios, remotely control the movement of the claw machine 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 Instant Messaging.

Example Source Code Download

Please refer to Download Example Source Code to get the source code.

For related source code, please check the files in the "/ZegoExpressExample/CommonFeatures/src/main/java/im/zego/roommessage" directory.

Prerequisites

Before sending real-time messages, please ensure:

Usage Steps

Send and Receive Broadcast Messages

  1. Send Broadcast Messages

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 onIMSendBroadcastMessageResult callback.

// Send broadcast message
engine.sendBroadcastMessage(roomID, msg, new IZegoIMSendBroadcastMessageCallback() {
    // Callback handling for broadcast message send result
    @Override
    public void onIMSendBroadcastMessageResult(int errorCode, long messageID) {
        // Handling for message send success or failure
    }
});
  1. Receive Broadcast Messages

Implement the onIMRecvBroadcastMessage callback in the IZegoEventHandler delegate. After the sender successfully sends a broadcast message, other users in the same Room receive related information through this callback, including message content, message ID, send time, and sender information.

public abstract class IZegoEventHandler {

    /**
     * Receive Room broadcast message notification
     *
     * @param roomID Room ID
     * @param messageList List of received messages
     */
    @Override
    public void onIMRecvBroadcastMessage(String roomID, ArrayList<ZegoBroadcastMessageInfo> messageList){
        // Handling for receiving messages sent by other users
    }

}

Send and Receive Barrage Messages

  1. Send Barrage Messages

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 onIMSendBarrageMessageResult callback.

// Send Room barrage message
engine.sendBarrageMessage("ChatRoom-1", "This is a barrage message", new IZegoIMSendBarrageMessageCallback(){
    // Callback handling for barrage message send result
    @Override
    public void onIMSendBarrageMessageResult(int errorCode, String messageID) {
         // Handling for message send success or failure
    }
});
  1. Receive Barrage Messages

Implement the onIMRecvBarrageMessage callback in the IZegoEventHandler delegate. After the sender successfully sends a barrage message, other users in the same Room receive related information through this callback, including message content, message ID, send time, and sender information.

public abstract class IZegoEventHandler {

    /**
     * Receive Room barrage message notification
     *
     * @param roomID Room ID
     * @param messageList List of received messages
     */
    @Override
    public void onIMRecvBarrageMessage(String roomID, ArrayList<ZegoBarrageMessageInfo> messageList){
         // Handling for receiving messages sent by other users
    }

}

Send and Receive Custom Commands

  1. Send Custom Commands

Call the sendCustomCommand interface to send custom commands to users specified through "toUserList" in the same Room. The length cannot exceed 1024 bytes.

Get the message sending result through the onIMSendCustomCommandResult callback.

// Send custom command, only users specified in `toUserList` can receive this command through onIMSendCustomCommandResult
// If the `toUserList` parameter is passed as `null`, the SDK will send this command to all users in the Room
engine.sendCustomCommand(roomID, command, toUserList, new IZegoIMSendCustomCommandCallback() {
    // Callback handling for user-defined message send result
    @Override
    public void onIMSendCustomCommandResult(int errorCode) {
        // Handling for message send success or failure
    }
});
  1. Receive Custom Commands

Implement the onIMRecvCustomCommand callback in the IZegoEventHandler delegate. After the sender successfully sends a custom message, specified users in the same Room receive related information through this callback, including message content and message sender information.

public abstract class IZegoEventHandler {

    /**
     * Receive custom command notification
     *
     * @param roomID Room ID
     * @param fromUser Sender of the command
     * @param command Command content
     */
    @Override
    public void onIMRecvCustomCommand(String roomID, ZegoUser fromUser, String command){
        // Handling for receiving messages sent by other users
    }

}

Previous

Room Connection Status

Next

Login to Multiple Rooms

On this page

Back to top