The ZEGO Call implements the backend business logic with the real-time database and cloud functions of Firebase, while you can also build your own business server instead of using the Firebase. This document describes the ZEGO Call's business server logic for you to refer to and build your own business server.
We encapsulate the method calls into different commands. Different commands receive different parameters and then execute the request through the unified execute
method. The following describes the execution process:
command
object and sets corresponding parameters.execute
method of the command
object.execute
method of the class CommandManager
.request
method of FirebaseManager
.All method calls are ultimately executed through FirebaseManager's request
method. The request
takes three parameters:
Different methods have different path
and parameter
, and different logic is executed within the request
based on different parameters.
There are two ways for clients to know about data changes on the server. One is the client actively sends a request to the server for the latest data, which is the method call described above. The other is that the server keeps a long connection with the client, and the server actively sends data to the client.
The following describes the process:
The SDK calls ZegoListener
's addListener
method to set up and listen for an event callback.
ZegoListenerManager
saves the callback that has been set up and listened for.
FirebaseManager receives data changes from the business server.
FirebaseManager calls the receiveUpdate
method of ZegoListenerUpdater
.
ZegoListenerManager
gets the callback based on path
.
The SDK receives the callback and processes the business logic.
Through the above-mentioned description, you can know that all active requests are implemented by the FirebaseManager
, and all passive data modifications are also been listened for by the FirebaseManager
. So you only need to replace the FirebaseManager
with your own business logic if you want to use the ZEGO Call with your own business server.
The request protocol refers to the protocol by which the client calls the method of the server. It can be implemented using HTTP or by establishing a long-lived connection.
path
: /call/start_call
Sample request:
{
"call_id" : "123",
"caller": {
"id": "xxx",
"name": "name1"
}
"callees" : [
{
"id": "123",
"name": "name"
},
{
"id": "123",
"name": "name"
}
],
"type" : 1
}
Request parameters:
Parameter | Type | Description |
---|---|---|
call_id | string | The UID of a call. |
caller | json | The caller information, including the user ID and username. |
callees | json | The callee information array, including the user ID and username. |
type | int | The type of the call. 1: voice call. 2: video call. |
Sample response:
{
"code" : 0,
"message" : ""
}
Response parameters:
Parameter | Type | Description |
---|---|---|
code | int | Return code. 0: successful. Other values indicate failures. |
message | int | Return message. You can customize it as needed. |
Client logic:
Users need to call this method when making a call.
Server logic:
call_id
and save user information about both parties.callees
. For more details about the callback, see Callback notification: receive an incoming call.The method protocol supports multi-party calls, but the client can only make a call to a single user yet, that is, the callees
only carry the information of one callee.
path
: /call/cancel_call
Sample request:
{
"id" : "123",
"call_id" : "123",
"callee_id": "123"
}
Request parameters:
Parameter | Type | Description |
---|---|---|
id | string | The ID of the current user. |
call_id | string | The ID of the current call. |
callee_id | string | The ID of the callee. |
Sample response:
{
"code" : 0
"message" : ""
}
Response parameters:
Parameter | Type | Description |
---|---|---|
code | int | Return code. 0: successful. Other values indicate failures. |
message | int | Return message. You can customize it as needed. |
Client logic:
The client can call this method to cancel a call after making an outbound call.
Server logic:
The server sends a callback notification to the callee based on the callee_id
indicating the call has been canceled. For more details about the callback, see Callback notification: the call has been canceled .
path
: /call/accept_call
Sample request:
{
"id" : "123",
"call_id" : "123"
}
Request parameters:
Parameter | Type | Description |
---|---|---|
id | string | The ID of the current user. |
call_id | string | The ID of the current call. |
Sample response:
{
"code" : 0
"message" : ""
}
Response parameters:
Parameter | Type | Description |
---|---|---|
code | int | Return code. 0: successful. Other values indicate failures. |
message | int | Return message. You can customize it as needed. |
Client logic:
The client can call this method to accept a call when receiving an incoming call.
Server logic:
The server sends a callback notification to the caller based on the call_id
indicating the call has been accepted. For more details about the callback, see Callback notification: the call has been accepted.
path
: /call/decline_call
Sample request:
{
"id" : "123",
"caller_id" : "xxx",
"call_id" : "123",
"type" : 1
}
Request parameters:
Parameter | Type | Description |
---|---|---|
id | string | The ID of the current user. |
caller_id | string | The ID of the caller. |
call_id | string | The ID of the current call. |
type | int | Decline types: 1: declined actively. 2: the callee is busy. |
Sample response:
{
"code" : 0
"message" : ""
}
Response parameters:
Parameter | Type | Description |
---|---|---|
code | int | Return code. 0: successful. Other values indicate failures. |
message | int | Return message. You can customize it as needed. |
Client logic:
The client can call this method to decline a call when receiving an incoming call.
Server logic:
The server sends a callback notification to the caller based on the call_id
indicating the call has been declined. For more details about the callback, see Callback notification: the call has been declined.
path
: /call/end_call
Sample request:
{
"id" : "123",
"call_id" : "123"
}
Request parameters:
Parameter | Type | Description |
---|---|---|
id | string | The ID of the current user. |
call_id | string | The ID of the current call. |
Sample response:
{
"code" : 0
"message" : ""
}
Response parameters:
Parameter | Type | Description |
---|---|---|
code | int | Return code. 0: successful. Other values indicate failures. |
message | int | Return message. You can customize it as needed. |
Client logic:
Both the callee and caller can call this method to end a call.
Server logic:
The server sends a callback notification to the peer end based on the call_id
indicating the call has ended. For more details about the callback, see Callback notification: the call has ended.
path
: /call/heartbeat
Sample request:
{
"id" : "123",
"call_id" : "123"
}
Request parameters:
Parameter | Type | Description |
---|---|---|
id | string | The ID of the current user. |
call_id | string | The ID of the current call. |
Sample response:
{
"code" : 0
"message" : ""
}
Response parameters:
Parameter | Type | Description |
---|---|---|
code | int | Return code. 0: successful. Other values indicate failures. |
message | int | Return message. You can customize it as needed. |
Client logic:
After a call starts, both parties need to send a heartbeat message to the server at intervals. The default heartbeat interval is 20 seconds, and the default heartbeat timeout duration is 60 seconds.
Server logic:
Notification protocol refers to the protocol by which the server proactively sends notifications to the callback method of the client. The notification protocol requires a long-live connection between the client and the server.
Only the callee can receive this callback notification, which indicates the callee receives an incoming call.
path
: /call/notify_call_invited
body:
{
"call_id" : "123",
"caller" : {
"id": "123",
"name": "name"
},
"callees" : [
{
"id": "123",
"name": "name"
},
{
"id": "123",
"name": "name"
}
],
"type" : 1
}
Parameters:
Parameter | Type | Description |
---|---|---|
call_id | string | The UID of a call. |
caller | json | The caller information, including the user ID and username. |
callees | json | The callee information array, including the user ID and username. |
type | int | The type of the call. 1: voice call. 2: video call. |
Only the callee can receive this callback notification, which indicates the call has been canceled by the caller.
path
: /call/notify_call_canceled
body:
{
"call_id" : "123",
"caller_id" : "123"
}
Parameters:
Parameter | Type | Description |
---|---|---|
call_id | string | The ID of the current call. |
callee_id | string | The ID of the callee. |
Only the caller can receive this callback notification, which indicates the call has been accepted by the callee.
path
: /call/notify_call_accept
body:
{
"callee_id" : "123",
"call_id" : "123",
}
Parameters:
Parameter | Type | Description |
---|---|---|
call_id | string | The ID of the current call. |
callee_id | string | The ID of the callee. |
Only the caller can receive this callback notification, which indicates the call has been declined by the callee.
path
: /call/notify_call_decline
body:
{
"callee_id" : "123",
"call_id" : "123",
"type" : 1
}
Parameters:
Parameter | Type | Description |
---|---|---|
call_id | string | The ID of the current call. |
callee_id | string | The ID of the callee. |
type | int | Declined type: 1: declined. 2: the callee is busy. |
Both the callee and caller can receive this callback notification, which indicates the call has ended.
path
: /call/notify_call_end
body:
{
"id" : "123",
"call_id" : "123"
}
Parameters:
Parameter | Type | Description |
---|---|---|
call_id | string | The ID of the current call. |
id | string | The ID of the user who ends the call. |
Receiving this callback notification means that the heartbeat of the peer side has timed out. In this case, you need to end the current call.
path
:/call/notify_timeout
body:
{
"call_id" : "123",
"user_id" : "123"
}
Parameters:
Parameter | Type | Description |
---|---|---|
call_id | string | The ID of the current call. |
id | string | The ID of the user whose heartbeat is timed out. |
The following describes how to modify the client logic to match the server methods.
RequestManager
that conforms to the ZegoRequestProtocol
protocol and implement the request
method.public class RequestManager implements ZegoRequestProtocol {
@Override
public void request(String path, Map<String, Object> parameter, ZegoRequestCallback callback) {
}
}
ZegoCommandManager
, replace the service
property with the RequestManager
.public class ZegoCommandManager {
private static volatile ZegoCommandManager singleton = null;
private ZegoCommandManager() {
}
public static ZegoCommandManager getInstance() {
if (singleton == null) {
synchronized (ZegoCommandManager.class) {
if (singleton == null) {
singleton = new ZegoCommandManager();
}
}
}
return singleton;
}
private ZegoRequestProtocol service = new RequestManager();
public void execute(ZegoCommand command, ZegoRequestCallback callback) {
service.request(command.getPath(), command.getParameter(), callback);
}
}
RequestManager
requires an instance object that conforms to the ZegoRequestProtocol
protocol for notifying the upper layer of data changes from the backend.public class RequestManager implements ZegoRequestProtocol {
private ZegoListenerUpdater updater = ZegoListenerManager.getInstance();
}
//RequestManager.java
@Override
public void request(String path, Map<String, Object> parameter, ZegoRequestCallback callback) {
if ("/call/heartbeat".equals(path)) {
sendHeartBeat(parameter, callback);
}else if(){
...
}
}
private void sendHeartBeat(Map<String, Object> parameter, ZegoRequestCallback callback) {
String callID = (String) parameter.get("callID");
String userID = (String) parameter.get("userID");
...
//do some http request
...
}
//RequestManager.java
private void onReceiveCallInvite(String receiveData) {
HashMap<String, Object> data = new HashMap<>();
data.put("call_id",receiveData.parseCallID());
//...
updater.receiveUpdate(ZegoListenerManager.RECEIVE_CALL, data);
}