This guide describes how to implement live voice chat based on ZEGO's GoChat cloud service and Express-Audio SDK.
In this solution, users in a chat room have the following roles:
A user who creates a chat room becomes the host of the room. The host of a room manages who can speak in the room.
A user who joins an existing room created by someone else becomes a listener in the room. A listener can only listen to the conversation between the host and the guest speakers in the room.
A listener becomes a guest speaker when he/she is invited or admitted by the host to join the conversation in the room. A guest speaker becomes a listener when he/she quits the conversation.
The voice chat scenarios described in this guide involves the following main features:
The host of a room can invite any listener in the room to join the conversion, subject to the listener's acceptance. A listener can also request to speak by raising his/her hand, subject to the host's approval.
Guest speakers in a room can voluntarily quit the conversation. The host can also remove any guest speaker from the conversation by setting him/her as a listener.
Guest speakers in a room can mute themselves or be muted by the host. A muted guest speaker remains in the conversation.
The following system components work together to make a live chat happen.
The business server is a backend service responsible for maintaining the chat room list, room members' roles, the status of users' chat requests, users' microphone status, etc.
To save you development effort and accelerate your time to market, ZEGO can provide you with the source code of this backend service, which you can deploy directly or customize according to your specific needs. For more details, please refer to ZEGO GoChat Cloud Service.
The ZEGO Express-Audio SDK (referred to as "the Express SDK" hereafter) is used for publishing/playing audio streams. Please refer to SDK Integration for details on how to download and integrate the Express SDK.
Chat room clients refer to the client apps (in this example, native apps) built with the Express SDK. Chat room clients perform different functions based on their user role in a chat room.
For easy presentation, when we refer to a user role (e.g., host) in the following processing steps, we are referring to the chat room client being used by the user in that role.
API call sequence:
The main steps are as follows:
login
request to the business server. The business server records the user information of the host and sends back a response, which includes the unique userID
of the user.heartbeat
with the business server.create_room
request to the business server. The business server creates a new room and returns the information of the new room, which includes the unique roomID
of the room.createEngineWithAppID
to create and initialize a ZegoExpressEngine
instance and calls setEventHandler
to set an event handler to listen for and handle event callbacks.loginRoom
to log into the ZEGO room with the userID
and roomID
obtained in step 1 and step 3 above.startPublishingStream
to start publishing a stream to the target ZEGO room.API call sequence:
The main steps are as follows:
login
request to the business server. The business server records the information of the listener and sends back a response, which includes the unique userID
of the user.heartbeat
with the business server.get_room_list
request to the business server to obtain the chat room list.login_room
request to the business server. The business server adds the listener to the room and returns the information of the room.onRoomExtraInfoUpdate
event from the Express SDK's callback function, room members extract the user list from the event data and refresh the UI accordingly.createEngineWithAppID
to create and initialize a ZegoExpressEngine
instance and calls Express SDK's API setEventHandler
to set an event handler to listen for and handle event callbacks.get_attendee_list
to the business server to obtain the user list of the room and displays the user list on the client UI. loginRoom
to log into the ZEGO room with the userID
obtained in step 1 and the roomID
identified in step 4 above.API call sequence:
The main steps are as follows:
operate_raise_hand
request to the business server.onRoomExtraInfoUpdate
event from the Express SDK's callback function, room members extract the listener's "raise hand" status change and refresh UI accordingly.invite_onstage
request to the business server to invite the target listener to speak.onIMRecvCustomCommand
event from the Express SDK's callback function, the listener extracts the invitation message from the event data.response_onstage_invite
request to the business server accordingly.onRoomExtraInfoUpdate
event from the Express SDK's callback function, room members extract the user role change from the event data and refresh the UI accordingly.startPublishingStream
to start publishing a stream to the room if the received event data indicates that his/her role has been changed to guest speaker.API call sequence:
The main steps are as follows:
invite_onstage
request to the business server to invite the target listener to speak.onIMRecvCustomCommand
event from the Express SDK's callback function, the listener extracts the invitation message from the event data.response_onstage_invite
request to the business server accordingly.onRoomExtraInfoUpdate
event from the Express SDK's callback function, room members extract the user role change from the event data and refresh the UI accordingly.startPublishingStream
to start publishing a stream to the room if the received event data indicates that his/her role has been changed to guest speaker.API call sequence:
The main steps are as follows:
set_user_info
request to the business server to change the target guest speaker to listener. onRoomExtraInfoUpdate
event from the Express SDK's callback function, room members extract the user role change from the event data and refresh the UI accordingly.stopPublishingStream
to stop the stream publishing to the room if the received event data indicates that his/her role has been changed to listener.API call sequence:
The main steps are as follows:
set_user_info
request to the business server to turn off the target guest speaker's microphone.onRoomExtraInfoUpdate
event from the Express SDK's callback function, room members extract the microphone status change from the event data and refresh the UI accordingly.muteMicrophone
to stop audio capture if the received event data indicates that his/her microphone should be muted.Method | Description |
---|---|
login | Logs in to the business server and obtain user ID. |
logout | Logs out of the business server. |
get_room_list | Gets chat room list from the business server. |
create_room | Creates a chat room. |
login_room | Joins a chat room. |
get_attendee_list | Lists all users in a chat room. |
operate_raise_hand | Raises hand or lowers hand. |
set_user_info | Changes user's role or microphone status. |
invite_onstage | Invites a user to the stage (to join the chat). |
response_onstage_invite | Responds to an on-stage invitation. |
For the complete list of APIs, please refer to GoChat Cloud Service .
Method | Description |
---|---|
createEngineWithAppID | Creates a singleton instance of ZegoExpressEngine. |
setEventHandler | Sets an event handler to listen for and handle event callbacks. |
loginRoom | Logs in to a room. |
startPublishingStream | Starts publishing a stream. |
stopPublishingStream | Stops publishing a stream. |
startPlayingStream | Starts playing a remote stream. |
stopPlayingStream | Stops playing a remote stream. |
muteMicrophone | Mutes or unmutes the microphone. |
onRoomExtraInfoUpdate | The callback triggered when there is an update on the room's extra information. |
onIMRecvCustomCommand | The callback triggered when a Custom Command is received. |
For the complete list of APIs, please refer to Express Audio SDK APIs.