Solution for Maintaining Room Stream List via Callbacks
Operation scenario
When developers frequently publish streams and stop publishing streams within a short period of time, the Stream Created Callback and Stream Destroyed Callback provided by ZEGO do not guarantee that the order of results is consistent with the order of execution by the developer.
When developers need to rely on the Stream Created Callback and Stream Destroyed Callback to maintain a local room stream list, the inconsistency between the callback order and the execution order may cause some business errors.
For example, the user's behavior order is:
- User u1 logs in to room r1;
- User u1 publishes stream s1, triggering stream created callback sc1;
- User u1 stops publishing stream s1, triggering stream destroyed callback sc2;
- User u1 publishes stream s1 again, triggering stream created callback sc3.
After passing through the callback service, the order of reaching the developer's server may become:
- Stream created callback sc1 when user u1 publishes stream s1;
- Stream created callback sc3 when user u1 publishes stream s1 again;
- Stream destroyed callback sc2 when user u1 stops publishing stream s1.
The developer updates the local room stream list information based on the order of callbacks. Stream s1 will be deleted, but in fact stream s1 is still in room r1.
Operation steps
To solve the problems caused by the above callback out-of-order, ZEGO recommends that developers combine Solution for Maintaining Room List via Callbacks and refer to the following solution to maintain the local room user list.
This solution solves the out-of-order problem based on the situation where callbacks are not lost. If callbacks are lost, it cannot be solved.
Pay attention to key callback parameters
Pay attention to the key parameters of Stream Created Callback and Stream Destroyed Callback.
| Parameter | Description |
|---|---|
| event | Event name:
|
| room_id | Room ID. |
| room_session_id | Room session ID, globally unique, and unchanged during the complete lifecycle of a room: This parameter is consistent with the parameter room_session_id in Room Created Callback and Room Destroyed Callback. |
| stream_id | Stream ID, corresponds to the client's StreamID. |
| stream_seq | The seq of the server stream list change, which increases by 1 each time the stream changes. |
| create_time_ms | Stream creation timestamp, unit: milliseconds. |
| destroy_timemillis | Stream destruction time, unit: milliseconds. This parameter exists in Stream Destroyed Callback. |
Maintain local room stream list
Maintain the local room stream list based on stream_id, stream_sid, create_time_ms, stream_seq, and the corresponding room's room_id and room_session_id.
The data structure of the local room stream list is as follows:
{
RoomInfo: {//Refer to "Solution for Maintaining Room List via Callbacks"
RoomID: room_id,
RoomSessionId: room_session_id,
RoomCreateTime:room_create_time,
...//Other data that needs to be saved for business
},
StreamList:[
{
StreamId:stream_id_1,
StreamSid:session_sid_1,
StreamCreateTime:create_time_ms_1,
StreamSeq:stream_seq_1
...//Other data that needs to be saved for business
},{
StreamId:stream_id_2,
StreamSid:session_sid_2,
StreamCreateTime:create_time_ms_2,
StreamSeq:stream_seq_2
...//Other data that needs to be saved for business
}
]
}Processing judgment logic
After receiving the Stream Created Callback and Stream Destroyed Callback, update the local room list.
The field descriptions in the images below are as follows:
| Field prefix | Meaning |
|---|---|
| zego.* | Parameters in ZEGO callbacks. |
| local.* | Parameters maintained locally by the developer. |
- Stream created callback processing flow:
- Stream destroyed callback processing flow:
