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. |
Before you begin to implement real-time messaging in your project, make sure you complete the following steps:
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.");
}];
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.");
}