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, 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 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 onIMSendBroadcastMessageResult
callback.
// Send a Broadcast Message to all users in the same room. Every user in the room can receive this message through the onIMRecvBroadcastMessage callback [the message sender will not receive the onIMRecvBroadcastMessage callback].
engine.sendBroadcastMessage(roomID, msg, new IZegoIMSendBroadcastMessageCallback() {
/** The callback to report the delivery result of the Broadcast Message */
@Override
public void onIMSendBroadcastMessageResult(int errorCode, long messageID) {
//Handle the delivery result of the Broadcast Message.
}
});
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 onIMSendBarrageMessageResult
callback.
// Send a Barrage Message to all users in the same room. Every user in the room can receive this message through the onIMRecvBarrageMessage callback [the message sender will not receive the onIMRecvBarrageMessage callback].
engine.sendBarrageMessage("ChatRoom-1", "This is a barrage message", new IZegoIMSendBarrageMessageCallback(){
/** The callback to report the delivery result of the Barrage Message */
@Override
public void onIMSendBarrageMessageResult(int errorCode, String messageID) {
// Handle the delivery result of the Barrage Message.
}
});
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 onIMSendCustomCommandResult
callback.
// Send a Custom Signaling Message to the users specified in the `toUserList` parameter. These message recipients can receive this message through the onIMSendCustomCommandResult callback.
// If the `toUserList` parameter passes `null`, the SDK sends the message to all users in the room.
engine.sendCustomCommand(roomID, command, userList, new IZegoIMSendCustomCommandCallback() {
/** The callback to report the delivery result of the Custom Signaling Message */
@Override
public void onIMSendCustomCommandResult(int errorCode) {
//Handle the delivery result of the Custom Signaling Message.
}
});
Receive Broadcast Messages
Implement the onIMRecvBroadcastMessage
callback method (defined in the IZegoEventHandler
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.
public abstract class IZegoEventHandler {
/**
* The callback triggered when Broadcast Messages are received.
*
* @param roomID: The room ID.
* @param messageList: The list of messages received.
*/
@Override
public void onIMRecvBroadcastMessage(String roomID, ArrayList<ZegoBroadcastMessageInfo> messageList){
// Process the Broadcast Messages received.
}
}
Receive Barrage Messages
Implement the onIMRecvBarrageMessage
callback method (defined in the IZegoEventHandler
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.
public abstract class IZegoEventHandler {
/**
* The callback triggered when Barrage Messages are received.
*
* @param roomID: The room ID.
* @param messageList: The list of messages received.
*/
@Override
public void onIMRecvBarrageMessage(String roomID, ArrayList<ZegoBarrageMessageInfo> messageList){
// Process the Barrage Messages received.
}
}
Receive Custom Signaling Messages
Implement the onIMRecvCustomCommand
callback method (defined in the IZegoEventHandler
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.
public abstract class IZegoEventHandler {
/**
* The callback triggered when a Custom Signaling Message is received.
*
* @param roomID: The room ID.
* @param fromUser: The sender of the message.
* @param command: The content of the message.
*/
@Override
public void onIMRecvCustomCommand(String roomID, ZegoUser fromUser, String command){
// Process the Custom Signaling Message received.
}
}