Documentation
ExpressVideoSDK Video Call
Documentation
Demo APP
SDK Center
API Center
FAQ
Code Market
Console
Sign Up
Log In
中文站 English
  • Documentation
  • Video Call
  • Upgrade using advanced features
  • Advanced features
  • Message signaling
  • Broadcast real-time messages to a room

Broadcast real-time messages to a room

Last updated:2023-05-17 19:00

1 Introduction

The real-time messaging of ZEGOCLOUD’s SDKs mainly provides the function of sending and receiving plain text messages. Developers can call related APIs to send broadcast messages and barrage messages to other users in the same room, or send custom messages to certain designated users. And you can implement interactive functions such as likes, gifts, and answers as needed.

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

Message Type Function description Application scenario Sending frequency limit
Broadcast Message Send a text message to all users in the same room. By default, the first 500 users who join the room can receive the message. Generally, this type of message is sent when the number of people in the live room is less than 500. Please refer to Restrictions.
Barrage News Viewers can post their own comments during the live broadcast and display them with sliding subtitles, which increases the interactivity between the viewers. Generally used in scenarios where there is a large amount of message sending and receiving in the room, and the message does not need to be reliable, such as live barrage. Please refer to Restrictions.
Custom signaling Send signaling messages to a single or multiple users specified in the same room. Generally used to remotely control signaling or send messages to specific users, for example, in an online crane machine scenario, remotely control the movement of the crane machine clip. Please refer to Restrictions.

2 Prerequisites

Before sending a real-time message, please ensure that the basic real-time audio and video functions have been implemented in the project. For details, please refer to Quick start.

3 Implementation Steps

3.1 Send Messages

  • Send broadcast message

Call 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 ZegoIMSendBroadcastMessageResult callback.

// Send a broadcast message, every user who logs 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 sendBarrageMessage interface to send barrage to other users in the same room The length of the message cannot exceed 1024 bytes.

Get the message sending result through the ZegoIMSendBarragetMessageResult callback.

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

Call sendCustomCommand interface to the same room specified by toUserList User-defined signaling can not exceed 1024 bytes in length.

Get the message sending result through the ZegoIMSendCustomCommandResult callback.

// Send custom signaling, the user 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", toUserList, "This is a custom command", [=](int errorCode){
    printf("send custom command: errorCode=%d", errorCode);
})

3.2 Receiving Messages

  • Receive broadcast messages

Implement the onIMRecvBroadcastMessage callback in the IZegoEventHandler agent, when After the sender successfully sends the broadcast message, other users in the same room receive related 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 received message list
    */
    void onIMRecvBroadcastMessage(const std::string& roomID, std::vector<ZegoBroadcastMessageInfo> messageList) override {
        printf("received broadcast message");
    }
}
  • Receive barrage messages

Implement the onIMRecvBarrageMessage callback in the IZegoEventHandler agent, when After the sender successfully sends the barrage message, other users in the same room receive related information through this callback, including the message content, message ID, sending time, and sender information.

class MyEventHandler: public IZegoEventHandler{
    /**
    * Receive notifications of room barrage messages
    *
    * @param roomID room ID
    * @param messageList received message list
    */
    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 agent, when After the sender successfully sends the custom signaling, the designated user in the same room receives related information through this callback, including the message content and the message sender information.

class MyEventHandler: public IZegoEventHandler{
    /**
    * Receive custom signaling notification
    *
    * @param roomID room ID
    * @param fromUser The sender of signaling
    * @param command signaling content
    */
    void onIMRecvCustomCommand(const std::string& roomID, ZegoUser fromUser, const std::string& command) override {
        printf("received custom command");
    }
}

4 API Reference

Method Description
sendBroadcastMessage Send room broadcast message.
onIMRecvBroadcastMessage Receive room broadcast message notifications.
sendCustomCommand Send room barrage message.
onIMRecvBarrageMessage Receive room barrage message notification.
sendCustomCommand Send custom signaling.
onIMRecvCustomCommand Receive custom signaling notification.
Page Directory
  • Free trial
  • 提交工单
    咨询集成、功能及报价等问题
    电话咨询
    400 1006 604
    Get Consulting
    Scan Wechat QR code