logo
On this page

Real-time Messaging and Signaling


Function Introduction

ZEGO real-time messaging mainly provides pure text message sending and receiving functionality. Developers can call relevant APIs to send broadcast messages and barrage comments to other users in the same room, or send custom messages to specified users, and can implement interactive functions such as likes, sending gifts, quizzes, etc. according to their needs.

The differences between the three types of messages are as follows:

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 scenarios.Please refer to Limit Description.
Barrage MessageAudiences can post their own comments during live streaming, which are displayed as sliding subtitles, increasing interactivity among audiences.Generally used for scenarios with large message sending and receiving in a room, and where message reliability is not required, such as live barrage comments.Please refer to Limit Description.
Custom SignalingSend signaling messages to single or multiple specified users in the same room.Generally used for remote control signaling or sending messages to specific users, such as remotely controlling the movement of a toy car clamp in online doll machine scenarios.Please refer to Limit Description.

Example Source Code Download

Please refer to Download Example Source Code to obtain source code.

For related source code, please check the "/ZegoExpressExample/src/RoomMessage/ZegoRoomMessageDemo.cpp" file.

Prerequisites

Before sending real-time messages, please ensure:

Usage Steps

Sending Messages

  • Send Broadcast Message

Call the sendBroadcastMessage interface to send broadcast messages to other users in the same room, with a maximum length of 1024 bytes.

// Send broadcast message, every user logged in to the room will receive this message through onIMRecvBroadcastMessage
engine->sendBroadcastMessage("ChatRoom-1", "This is a broadcast message", [=](int errorCode, unsigned long long messageID){
    printf("send broadcast message: errorCode=%d", errorCode);
    })
  • Send Barrage Message

Call the sendBarrageMessage interface to send barrage messages to other users in the same room, with a maximum length of 1024 bytes.

// Send barrage message, every user logged in to the room will receive this message through onIMRecvBarrageMessage
engine->sendBarrageMessage("ChatRoom-1", "This is a barrage message", [=](int errorCode, std::string messageID){
    printf("send barrage message: errorCode=%d", errorCode);
    })
  • Send Custom Signaling

Call the sendCustomCommand interface to send custom signaling messages to users specified by "toUserList" in the same room, with a maximum length of 1024 bytes.

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

Receiving Messages

  • Receive Broadcast Message

Implement the onIMRecvBroadcastMessage callback in the IZegoEventHandler proxy. When the sender successfully sends a broadcast message, other users in the same room will 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");
        }
    };
  • Receive Barrage Message

Implement the onIMRecvBarrageMessage callback in the IZegoEventHandler proxy. When the sender successfully sends a barrage message, other users in the same room will 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");
        }
    };
  • Receive Custom Signaling

Implement the onIMRecvCustomCommand callback in the IZegoEventHandler proxy. When the sender successfully sends a custom signaling message, the specified user in the same room will receive relevant information through this callback, including message content and 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");
        }
    };

API Reference List

MethodDescription
sendBroadcastMessageSend room broadcast message
onIMRecvBroadcastMessageReceive room broadcast message notification
sendBarrageMessageSend room barrage message
onIMRecvBarrageMessageReceive room barrage message notification
sendCustomCommandSend custom signaling
onIMRecvCustomCommandReceive custom signaling notification

Previous

Room Connection Status

Next

Login to Multiple Rooms

On this page

Back to top