logo
On this page

Real-time Messaging and Signaling

2024-01-02

Feature Introduction

ZEGO provides various text message sending and receiving functions to implement sending in-room broadcast messages, barrage messages, simple IM text chat, likes, gifts, quizzes, 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 MessageViewers can express their comments during the live streaming process, which are displayed as sliding subtitles, increasing interactivity between viewers.Generally used for scenarios with large amounts of message sending and receiving in the room, and 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 claw machine scenario, remotely control the movement of the claw machine gripper.Please refer to Restrictions.

In addition, ZEGO also provides a complete Instant Messaging ZIM SDK, providing developers with cross-platform interaction, massive concurrency, ultra-low latency, and guaranteed message delivery communication services. For details, please refer to Instant Messaging.

Sample Source Code Download

Please refer to Download Sample Source Code to get the source code.

For related source code, please check the files in the "/ZegoExpressExample/Examples/CommonFeatures/RoomMessage" directory.

Prerequisites

Before sending real-time messages, please ensure:

Usage Steps

Send and Receive Broadcast Messages

  1. Send broadcast message

    Call the 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 ZegoIMSendBroadcastMessageCallback callback.

    // Send broadcast message
    [self.engine sendBroadcastMessage:@"This is a broadcast message" roomID:@"ChatRoom-1" callback:^(int errorCode, unsigned long long messageID) {
        NSLog(@"send broadcast message result block");
    }];
  2. Receive broadcast message

    Implement the onIMRecvBroadcastMessage callback in the ZegoEventHandler delegate. After the sender successfully sends the broadcast message, other users in the same room receive relevant information through this callback, including message content, message ID, sending time, and sender information.

    // Received message sent by other users
    - (void)onIMRecvBroadcastMessage:(NSArray<ZegoBroadcastMessageInfo *> *)messageList roomID:(NSString *)roomID {
        NSLog(@"received broadcast message");
    }

Send and Receive Barrage Messages

  1. Send barrage message

    Call the sendBarrageMessage interface to send barrage messages to other users in the same room. The length cannot exceed 1024 bytes.

    Get the message sending result through the ZegoIMSendBarrageMessageCallback callback.

    // Send room barrage message
    [self.engine sendBarrageMessage:@"This is a barrage message" roomID:@"ChatRoom-1" callback:^(int errorCode, NSString *messageID) {
        NSLog(@"send barrage message result block");
    }];
  2. Receive barrage message

    Implement the onIMRecvBarrageMessage callback in the ZegoEventHandler delegate. After the sender successfully sends the barrage message, other users in the same room receive relevant information through this callback, including message content, message ID, sending time, and sender information.

    // Received message sent by other users
    - (void)onIMRecvBarrageMessage:(NSArray<ZegoBarrageMessageInfo *> *)messageList roomID:(NSString *)roomID {
        NSLog(@"received barrage message");
    }

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. The length cannot exceed 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:fromUser:roomID:
    // If the `toUserList` parameter is `nil`, the SDK will send this signaling 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(@"send custom command result block");
    }];
  2. Receive custom signaling

    Implement the onIMRecvCustomCommand callback in the ZegoEventHandler 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.

    // Received message sent by other users
    - (void)onIMRecvCustomCommand:(NSString *)command fromUser:(ZegoUser *)fromUser roomID:(NSString *)roomID {
        NSLog(@"received custom command");
    }

Previous

Room Connection Status

Next

Login to Multiple Rooms

On this page

Back to top