Live Streaming
  • iOS
  • Android
  • Web
  • Flutter
  • React Native : JavaScript
  • Electron
  • Unity3D
  • Windows
  • macOS
  • Linux
  • Overview
  • Live Streaming vs. Interactive Live Streaming
  • Develop your app
    • Live Streaming
      • Quick start
      • Enhance basic livestream
      • CDN
    • Interactive Live Streaming
  • Upgrade the livestream
    • Advanced features
      • Media push
        • Mix the live streams
      • Message signaling
        • Broadcast real-time messages to a room
        • Quotas and limits
      • Play media files
        • Play media files
  • Resources & Reference
    • SDK
    • Sample code
    • API reference
      • Client APIs
      • Server APIs
    • 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, make sure you complete the following:

  • Create a project in ZEGOCLOUD Admin Console and get the AppID and AppSign of your project.

  • Refer to the Quick Start doc to complete the SDK integration and basic function implementation.

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

    // Send a Broadcast Message to all users in the same room. Every user in the room can receive this message through the `IMRecvBroadcastMessage` callback.
    ZegoExpressEngine.instance().sendBroadcastMessage("ChatRoom-1", "This is a broadcast message").then((result) => {
        // Handle the 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 ZegoIMSendBarrageMessageResult callback.

    // Send a Barrage Message to all users in the same room. Every user in the room can receive this message through the `IMRecvBarrageMessage` callback.
    
    ZegoExpressEngine.instance(). sendBarrageMessage("ChatRoom-1", "This is a barrage message").then((result) => {
        // Handle the 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 ZegoIMSendCustomCommandResult callback.

    // Send a Custom Signaling Message to the users specified in the `toUserList` parameter. These message recipients can receive this message through the `IMRecvCustomCommand` callback.
    // If the `toUserList` parameter is not specified, the SDK sends the message to all users in the room.
    ZegoExpressEngine.instance().sendCustomCommand("ChatRoom-1", "This is a custom command", toUserList)

Receive messages

  • Receive Broadcast Messages

    To receive and handle Broadcast Messages, call the on method to subscribe to the IMRecvBroadcastMessage callback. 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.

    ZegoExpressEngine.instance().on('IMRecvBroadcastMessage', (roomID, messageList) => {
          console.log("Broadcast message received.") 
        })
  • Receive Barrage Messages

    To receive and handle Barrage Messages, call the on method to subscribe to the IMRecvBarrageMessage callback. When a user successfully sends out a Barrage Messages, other users in the same room receive the message through this callback, including the message content, message ID, time message sent, and sender information.

    ZegoExpressEngine.instance().on('IMRecvBarrageMessage', (roomID, messageList) => {
          console.log("Barrage message received.") 
        })
  • Receive Custom Signaling Messages

    To receive and handle Custom Signaling Messages, call the on method to subscribe to the IMRecvCustomCommand callback. 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.

    ZegoExpressEngine.instance().on('IMRecvCustomCommand', (roomID, fromUser, command) => {
          console.log("Custom command received.") 
        })
Page Directory