Real-time Messaging and Signaling
Overview
ZEGO provides multiple text message sending and receiving functions, implementing functions such as sending broadcast messages in the Room, bullet screen messages, simple IM text chat, likes, gifts, and quizzes.
| Message type | Feature description | Application scenario | Sending frequency limit |
|---|---|---|---|
| Broadcast message | Send text messages to all users in the same Room. | Can be used for simple in-Room chat room scenarios. | Please refer to Restriction description. |
| Bullet screen message | Viewers can express their comments during live streaming, and they 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 without requiring message reliability, such as live streaming bullet screens. | Please refer to Restriction description. |
| Custom signaling | Send messages to one or more users in the same Room. | Generally used for remote control signaling or simple text message sending. For example, in an online doll machine scenario, remotely control the movement of the doll machine clamp. | Please refer to Restriction description. |
In addition, ZEGO also provides a complete instant messaging ZIM SDK, providing developers with full-platform interaction, massive concurrency, ultra-low latency, and guaranteed message delivery communication services. For details, please refer to Instant Messaging.
Example Source Code Download
Please refer to Download Example Source Code to get the source code.
For relevant source code, please view files in the "src/Examples/CommonFeatures/RoomMessage" directory.
Prerequisites
Before sending real-time messages, please ensure:
- You have created a project in the ZEGOCLOUD Console and applied for a valid AppID and Server address. For details, please refer to Console - Project Information.
- You have integrated ZEGO Express SDK in your project and implemented basic audio/video Stream Publishing and Playing functions. For details, please refer to Quick Start - Integration and Quick Start - Implementation.
Usage Steps
Send and Receive Broadcast Messages
- 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 "errorCode" in ZegoServerResponse. "0" indicates success, non-"0" indicates failure.
try {
const isSent = await zg.sendBroadcastMessage(this.data.roomID, this.data.inputMessage)
console.log('>>> sendMsg success, ', isSent);
} catch (error) {
console.log('>>> sendMsg, error: ', error);
};- Receive broadcast message
Room members register the IMRecvBroadcastMessage callback. After the sender successfully sends a broadcast message, other users in the same Room receive relevant information through this callback, including message content, message ID, sending time, and sender information.
zg.on('IMRecvBroadcastMessage', (roomID, chatData) => {
console.log('IMRecvBroadcastMessage', roomID, chatData);
let message = {
ID: 'zego' + chatData[0].fromUser.userID + chatData[0].sendTime,
name: chatData[0].fromUser.userName,
time: format(chatData[0].sendTime),
content: chatData[0].message + '(Broadcast sent)'
}
});Send and Receive Bullet Screen Messages
- Send bullet screen message
Call the sendBarrageMessage interface to send bullet screen messages to other users in the same Room. The length cannot exceed 1024 bytes.
Get the message sending result through "errorCode" in ZegoServerResponse. "0" indicates success, non-"0" indicates failure.
try {
const isSent = await zg.sendBarrageMessage(this.data.roomID, this.data.inputMessage)
console.log('>>> barrageMessage success, ', isSent);
} catch (error) {
console.log('>>> barrageMessage, error: ', error);
};- Receive bullet screen message
Room members register the IMRecvBarrageMessage callback. After the sender successfully sends a bullet screen message, other users in the same Room receive relevant information through this callback, including message content, message ID, sending time, and sender information.
zg.on('IMRecvBarrageMessage', (roomID, chatData) => {
console.log('IMRecvBroadcastMessage', roomID, chatData);
let message = {
ID: 'zego' + chatData[0].fromUser.userID + chatData[0].sendTime,
name: chatData[0].fromUser.userName,
// @ts-ignore
time: format(chatData[0].sendTime),
content: chatData[0].message + '(Bullet screen sent)'
}
});Send and Receive Custom Signaling
- Send custom signaling
Call the sendCustomCommand interface to send custom signaling to users specified by "toUserIDList" in the same Room. The length cannot exceed 1024 bytes.
Get the message sending result through "errorCode" in ZegoServerResponse. "0" indicates success, non-"0" indicates failure.
try {
const res = await zg.sendCustomCommand(this.data.roomID, this.data.inputMessage, toUserIDList);
console.warn('send custom success ' + res)
} catch(error) {
console.error(JSON.stringify(error))
}- Receive custom signaling
Room members register the IMRecvCustomCommand callback. After the sender successfully sends custom signaling, specified users in the same Room receive relevant information through this callback, including message content and sender information.
zg.on('IMRecvCustomCommand', (roomID, fromUser, command) => {
console.log('IMRecvCustomCommand',roomID, fromUser, command);
let message = {
ID: fromUser.userID,
name: fromUser.userName,
time: format(new Date().getTime()),
content: command + '(Custom sent)'
}
});