logo
On this page

Real-Time Messaging and Signaling

2024-01-02

Feature Overview

ZEGO provides various text message sending and receiving functions to implement sending in-room broadcast messages, barrage messages, simple IM text chat, likes, gift sending, quiz answering, and other functions.

Message TypeFunction DescriptionApplication ScenarioSending 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 post their comments, which are displayed as scrolling subtitles, increasing interactivity between viewers.Generally used for scenarios with large amounts of message sending and receiving in a room, and where message reliability does not need to be guaranteed, such as live streaming barrage.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 online claw machine scenarios, remotely control the movement of the claw machine's claw.Please refer to Restrictions.

Download Example Source Code

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

For related source code, please check the files in the "/ZegoExpressExample/Examples/CommonFeatures/RoomMessage" directory.

Prerequisites

Before sending real-time messages, ensure that:

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

    // Send broadcast message
    engine->sendBroadcastMessage("ChatRoom-1", "This is a broadcast message", [=](int errorCode, unsigned long long messageID){
        printf("send broadcast message: errorCode=%d", errorCode);
    })
  2. Receive Broadcast Message

    Implement the onIMRecvBroadcastMessage callback in the IZegoEventHandler delegate. When 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.

    class MyEventHandler: public IZegoEventHandler{
        /**
        * Receive room broadcast message notification
        *
        * @param roomID Room ID
        * @param messageList List of received messages
        */
        void onIMRecvBroadcastMessage(const std::string& roomID, std::vector<ZegoBroadcastMessageInfo> messageList) override {
            printf("received broadcast message");
            // Process for handling messages received 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 ZegoIMSendBarrageMessageCallback callback.

    // Send barrage message
    engine->sendBarrageMassage("ChatRoom-1", "This is a barrage message", [=](int errorCode, std::string messageID){
        printf("send barrage message: errorCode=%d", errorCode);
    })
  2. Receive Barrage Message

    Implement the onIMRecvBarrageMessage callback in the IZegoEventHandler delegate. When 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.

    class MyEventHandler: public IZegoEventHandler{
        /**
        * Receive room barrage message notification
        *
        * @param roomID Room ID
        * @param messageList List of received messages
        */
        void onIMRecvBarrageMessage(const std::string& roomID, std::vector<ZegoBarrageMessageInfo> messageList) override {
            printf("received barrage message");
            // Process for handling messages received 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 ZegoIMSendCustomCommandCallback callback.

    // Send custom signaling, only users specified in toUserList can receive this signaling through onIMRecvCustomCommand
    auto user1 = ZegoUser("userID1", "userName1");
    auto user2 = ZegoUser("userID2", "userName2");
    std::vector<ZegoUser> toUserList = {user1, user2};
    engine->sendCustomCommand("ChatRoom-1", "This is a custom command", toUserList, [=](int errorCode){
        printf("send custom command: errorCode=%d", errorCode);
    })
  2. Receive Custom Signaling

    Implement the onIMRecvCustomCommand callback in the IZegoEventHandler delegate. When the sender successfully sends custom signaling, specified users in the same room receive relevant information through this callback, including message content and message sender information.

    class MyEventHandler: public IZegoEventHandler{
        /**
        * Receive custom signaling notification
        *
        * @param roomID Room ID
        * @param fromUser Sender of the signaling
        * @param command Signaling content
        */
        void onIMRecvCustomCommand(const std::string& roomID, ZegoUser fromUser, const std::string& command) override {
            printf("received custom command");
            // Process for handling messages received from other users
        }
    }

Previous

Geofencing

Next

Login to Multiple Rooms

On this page

Back to top