Live Streaming
  • iOS : Objective-C
  • Android
  • Web
  • Flutter
  • React Native
  • Electron
  • Unity3D
  • Windows
  • macOS
  • Linux
  • Overview
  • Live Streaming vs. Interactive Live Streaming
  • Develop your app
    • Live Streaming
      • Integrate the SDK
      • Implement a basic live streaming
      • Enhance basic livestream
      • CDN
      • Play live streams
    • Interactive Live Streaming
  • Upgrade the livestream
    • Advanced features
      • Enhance the livestream
        • Share the screen
        • Improve your appearance in the livestream
        • Beautify & Change the voice
        • Output the livestream in H.265
        • Watermark the live/Take snapshots
        • Config video codec
        • Visualize the sound level
      • Message signaling
        • Convey extra information using SEI
        • Broadcast real-time messages to a room
        • Quotas and limits
      • Ensure livestream quality
        • Test network and devices in advance
        • Check the room connection status
        • Monitor streaming quality
        • Configure bandwidth management
      • Play media files
        • Play media files
        • Play sound effects
      • Record video media data
      • Join multiple rooms
      • Publish multiple live streams
      • Low-latency live streaming
      • Use the bit mask
      • Common audio config
      • Playing streams via URL
      • Mix the live streams
    • Distincitve features
      • Set the voice hearing range
      • Single stream transcoding
      • Low-light enhancement
      • Customize the video and audio
  • Upgrade using Add-on
  • Resources & Reference
    • SDK
    • Sample code
    • API reference
      • Client APIs
      • Server APIs
    • Debugging
      • Error codes
    • FAQs
    • Key concepts
  • Documentation
  • Live Streaming
  • Upgrade the livestream
  • 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

Introduction

The real-time messaging component of ZEGOCLOUD’s SDKs provides the ability to send and receive plain text messages in real time. You can use the SDK's real-time messaging APIs to send and receive the following types of messages to all or specified users in the same room.

Message type Description Use case
Broadcast Message Text messages to be sent to all users in the same room. By default, the maximum number of message recipients is 500. If there are more than 500 users in a room, the first 500 users (in login sequence) can receive the message. Suitable for broadcasting text messages to all users in a room that has less than 500 users.
Barrage Message (Bullet Screen Message) Text messages to be sent to all users in the same room, but the delivery isn't guaranteed. Suitable for broadcasting a large number of messages to all users in a room, when the delivery of messages doesn't need to be guaranteed.

For example, you can use this type of message to implement the "Bullet Screen" feature for live streaming use cases.

"Bullet Screen" refers to the feature that during a live streaming session, viewers can send real-time text comments that fly across the screen like bullets while the video is playing, hence given the name.
Custom Signaling Message Text messages to be sent to the specified users in the same room. Suitable for sending text chat messages or signaling messages to one or more specific users in the same room.

Prerequisites

Before you begin to implement real-time messaging in your project, make sure you complete the following steps:

  • ZEGO Express SDK has been integrated into the project to implement basic real-time audio and video functions. For details, please refer to Quick start .
  • A project has been created in ZEGOCLOUD Console and applied for a valid AppID and AppSign. For details, please refer to Console - Project Information .

Send messages

  • Send a Broadcast Message

    To send a Broadcast Message to all users in the same room, call the sendBroadcastMessage method. The maximum length of the message is 1024 bytes.

    Get the message delivery result through the ZegoIMSendBroadcastMessageCallback callback.

    // Send a Broadcast Message to all users in the same room. Every user in the room can receive this message through the `-onIMRecvBroadcastMessage:roomID:` callback.
    [self.engine sendBroadcastMessage:@"This is a broadcast message" roomID:@"ChatRoom-1" callback:^(int errorCode, unsigned long long messageID) {
        NSLog(@"Handle the Broadcast Message delivery result.");
    }];
  • Send a Barrage Message

    To send a Broadcast Message to all users in the same room, call the sendBarrageMessage method. The maximum length of the message is 1024 bytes.

    Get the message delivery result through the ZegoIMSendBarrageMessageCallback callback.

    // Send a Barrage Message to all users in the same room. Every user in the room can receive this message through the `-onIMRecvBarrageMessage:roomID:` callback.
    [self.engine sendBarrageMessage:@"This is a barrage message" roomID:@"ChatRoom-1" callback:^(int errorCode, NSString *messageID) {
        NSLog(@"Handle the Barrage Message delivery result.");
    }];
  • Send a Custom Signaling Message

    To send a Custom Signaling Message to the specified users, call the sendCustomCommand method with the message recipients passed to the toUserList parameter. The maximum length of the message is 1024 bytes.

    Get the message delivery result through the ZegoIMSendCustomCommandCallback callback.

    // Send a Custom Signaling Message to the users specified in the `toUserList` parameter. These message recipients can receive this message through the `-onIMRecvCustomCommand:fromUser:roomID:` callback.
    // If the `toUserList` parameter is set to `nil`, the SDK sends the message to all users in the room.
    NSArray<ZegoUser *> *toUserList = @[[ZegoUser userWithUserID:@"user1"], [ZegoUser userWithUserID:@"user2"]];
    [self.engine sendCustomCommand:@"This is a custom command" toUserList:toUserList roomID:@"ChatRoom-1" callback:^(int errorCode) {
        NSLog(@"Handle the Custom Signaling Message delivery result here.");
    }];

Receive messages

  • Receive Broadcast Messages

Implement the onIMRecvBroadcastMessage callback method (defined in the ZegoEventHandler delegate) to receive and handle Broadcast Messages. When a user successfully sends out a Broadcast Message, other users in the same room receive the message through this callback, including the message content, message ID, time message sent, and sender information.

- (void)onIMRecvBroadcastMessage:(NSArray<ZegoBroadcastMessageInfo *> *)messageList roomID:(NSString *)roomID {
      NSLog(@"received broadcast message");
}
  • Receive Barrage Messages

    Implement the onIMRecvBarrageMessage callback method (defined in the ZegoEventHandler delegate) to receive and handle Barrage Messages. When a user successfully sends out a Barrage Message, other users in the same room receive the message through this callback, including the message content, message ID, time message sent, and sender information.

    -(void)onIMRecvBarrageMessage:(NSArray<ZegoBarrageMessageInfo *> *)messageList roomID:(NSString *)roomID {
         NSLog(@"Barrage message received.");
    }
  • Receive Custom Signaling Messages

    Implement the onIMRecvCustomCommand callback method (defined in the ZegoEventHandler delegate) to receive and handle Custom Signaling Messages. When a user successfully sends out a Custom Signaling Message, other users in the same room receive the message through this callback, including the message content, message ID, time message sent, and sender information.

    -(void)onIMRecvCustomCommand:(NSString *)command fromUser:(ZegoUser *)fromUser roomID:(NSString *)roomID {
        NSLog(@"Custom command received.");
    }
Page Directory