Creates a callback to subscribe to channel or user events.
Operation description
You can call this operation to create a callback to subscribe to channel or user events. When you create a callback, you can configure parameters such as the callback URL and event type.
QPS limit
You can call this operation up to 100 times per second per account. Requests that exceed this limit are dropped and you will experience service interruptions. We recommend that you take note of this limit when you call this operation.
Debugging
Authorization information
The following table shows the authorization information corresponding to the API. The authorization information can be used in the Action
policy element to grant a RAM user or RAM role the permissions to call this API operation. Description:
- Operation: the value that you can use in the Action element to specify the operation on a resource.
- Access level: the access level of each operation. The levels are read, write, and list.
- Resource type: the type of the resource on which you can authorize the RAM user or the RAM role to perform the operation. Take note of the following items:
- The required resource types are displayed in bold characters.
- If the permissions cannot be granted at the resource level,
All Resources
is used in the Resource type column of the operation.
- Condition Key: the condition key that is defined by the cloud service.
- Associated operation: other operations that the RAM user or the RAM role must have permissions to perform to complete the operation. To complete the operation, the RAM user or the RAM role must have the permissions to perform the associated operations.
Operation | Access level | Resource type | Condition key | Associated operation |
---|---|---|---|---|
live:CreateEventSub | *Rtc acs:live::{#accountId}:rtc/{#AppId} |
| none |
Request parameters
Parameter | Type | Required | Description | Example |
---|---|---|---|---|
AppId | string | Yes | The application ID. | 9qb1**** |
ChannelId | string | No | The channel ID. You can call the ListEventSub operation to query the channel ID. Note
| 123333 |
Users | array | No | The user whose events you want to subscribe to. If you leave this parameter empty, the events of all users in the channel are subscribed to, including the events of the streamer and viewers. Specify this parameter in the following format:
| |
string | No | The user ID. | user1 | |
Events | array | Yes | Subscribe to events. | |
string | Yes | The type of the event. Valid values:
| ChannelEvent | |
CallbackUrl | string | Yes | The callback URL. For more information about the content of the messages that are sent to the callback URL, see the Callback section in this topic. | http://****.com/callback |
CallBack
Callback messages are sent to the callback URL that is specified by the CallbackUrl parameter. The following sample code provides an example:
Request:
POST /callbackURL
Body
application/json
{
"MsgId": "Message ID",
"MsgTimestamp": 12312324, // The UNIX timestamp when the message was sent.
"SubscribeID": "Subscription ID",
"AppId":"", // The ID of the application in which the message was generated.
"ChannelID":"", // The ID of the channel in which the message was generated.
"Contents": [
{
"Event": "UserEvent",// The type of the event that is subscribed to. In this case, the type is user event.
"UserEvent": {
"UserId": "80331631628*****", // The user ID.
"EventTag": "Publish", // The event. Valid values: Join, Leave, Publish, Unpublish, and Roleupdate.
"SessionId": "0dr15rrnhkz0jnvz6o8sxo0*****", // The ID of the session in which the event was generated.
"Timestamp": 1609854786, // The UNIX timestamp when the event occurred.
"Reason": 1, // The reason for joining or leaving the channel. This parameter is available only if the event is a Join event.
"Role": 1, // The role of the user. The roles include streamer and viewer.
"CurrentMedias":"1,2,3"// The types of the streams that were ingested by the user.
}
},
{
"Event": "ChannelEvent",// The type of the event that is subscribed to. In this case, the type is channel event.
"ChannelEvent": {
"ChannelId": "88888****",
"EventTag": "Open", // The event. Valid values: Open and Close.
"Timestamp": 1609854530 // The UNIX timestamp when the event occurred.
}
}
]
}
Response
HTTP STATUS 200
Parameters of a user event
Parameter | Type | Required | Description |
---|---|---|---|
UserId | string | Yes | The user ID. |
SessionId | string | Yes | The session ID. |
EventTag | string | Yes | The event. Valid values:Join: The user joined the channel.Leave: The user left the channel.PublishVideo: The user ingested a video stream.PublishAudio: The user ingested an audio stream.PublishScreen: The user ingested a stream for screen sharing.UnpublishVideo: The user stopped ingesting a video stream.UnpublishAudio: The user stopped ingesting an audio stream.UnpublishScreen: The user stopped ingesting a stream for screen sharing.Roleupdate: The role of the user was changed. |
Timestamp | number | Yes | The timestamp when the event occurred. |
Reason | integer | Yes | The reason for joining or leaving the channel. This parameter is available only if the event is a Join event. Valid values:1: The user joined or left the channel as needed.2: The user reconnected to join the channel again when the user was already in the channel.3: The user performed stream relay across channels.4: The user left the channel due to a timeout.5: The user started a new session and was forced to go offline in the current session.6: The user was kicked out of the channel.7: The channel was closed. |
Role | integer | Yes | The role of the user. Valid values:1: streamer2: viewer |
CurrentMedias | integer | Yes | The type of the stream. Valid values:1: audio2: video3: screen sharing |
Parameters of a channel event
Parameter | Type | Required | Description |
---|---|---|---|
EventTag | string | Yes | The event. Valid values:Open: The channel was opened.Close: The channel was closed. |
Timestamp | number | Yes | The timestamp when the event occurred. |
Callback authentication
By default, callback authentication is enabled. The following authentication logic applies:
- When ApsaraVideo Live initiates a callback request, it includes the Ali-Rtc-Timestamp and Ali-Rtc-Signature headers in the HTTP or HTTPS request so that the callback message receiving server can authenticate the signature. The value of Ali-Rtc-Timestamp is calculated based on the following formula: Ali-Rtc-Signature=MD5SUM(MD5CONTENT). In the formula, MD5CONTENT is a string in the following format: Callback domain name|Ali-Rtc-Timestamp value|Authentication key. The callback domain name is the domain name in the callback URL. The authentication key is the AppKey of the application.
- After receiving a callback message, the callback message receiving server concatenates the callback domain name, value of the Ali-Rtc-Timestamp header, and authentication key in the preceding format. The server calculates the MD5 value of the string to obtain an encrypted string. Then, the server compares the encrypted string with the value of the Ali-Rtc-Signature header in the HTTP or HTTPS request initiated by Real-Time Communication (RTC). If the two values are different, the request is invalid.
Callback retry
After Alibaba Cloud initiates a callback request, the callback is considered successful only if the HTTP status code 200 is returned by your business server. If a callback fails, ApsaraVideo Live resends the callback request 7 times at intervals of 1 second, 2 seconds, 5 seconds, 10 seconds, 60 seconds, 120 seconds, and 300 seconds. A callback record is generated each time a callback request is resent.
Exception handling
A keep-alive mechanism is implemented between clients that have joined a channel or started stream ingest and the Alibaba Cloud server. If the server does not receive heartbeat information from a client for 90 seconds, the server considers that the client timed out and the user left the channel and then fires callbacks. Heartbeats fail to be detected if a network disconnection occurs or the application accidentally exits.
Response parameters
Examples
Sample success responses
JSON
format
{
"RequestId": "760bad53276431c499e30dc36f6b****",
"SubscribeId": "ad53276431c****"
}
Error codes
HTTP status code | Error code | Error message | Description |
---|---|---|---|
400 | InputInvalid | %s. | Illegal input parameters |
400 | QuotaLimitError | %s. | For each AppId, a maximum of 20 subscriptions are allowed at the same time, and only one full channel subscription is allowed. |
400 | ErrorInvalidCallBackUrl | %s. | The CallBackURL is invalid, please check and try again. |
403 | NoAuth | %s. | No permission |
404 | ResourceNotExist | %s. | The requested resource does not exist, please check and try again |
500 | ServerError | %s. | Unknown error, please try again later or submit a ticket for consultation. |
For a list of error codes, visit the Service error codes.
Change history
Change time | Summary of changes | Operation |
---|---|---|
2023-11-21 | The Error code has changed | View Change Details |
2023-11-21 | The Error code has changed. The request parameters of the API has changed | View Change Details |
2023-08-31 | The Error code has changed | View Change Details |
2023-08-04 | The request parameters of the API has changed | View Change Details |