logo
Live Streaming
On this page

Real-time Messages and Signaling

2024-01-02

Introduction

ZEGO provides multiple text message sending and receiving functions, implementing sending in-room broadcast messages, barrage messages, simple IM text chat, likes, gift-giving, answering questions, and other functions.

Message TypeFunction 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 comments, which are displayed as sliding subtitles, increasing the interactivity between viewers.Generally used for scenarios with a large amount of message sending and receiving in the room, 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 the online doll machine scenario, remotely control the movement of the doll machine clamp.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 Sample Source Code

Please refer to Download Sample Source Code to obtain 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 messages

    Call the sendBroadcastMessage interface to send broadcast messages to other users in the same room, with a maximum length of 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 messages

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

    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");
            // Handle processing for 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, with a maximum length of 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 messages

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

    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");
            // Handle processing for messages sent by 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, with a maximum length of 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. After 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");
            // Handle processing for messages sent by other users
        }
    }

Previous

Room Connection Status

Next

Multi-Room Login

On this page

Back to top