Creates a callback for subscribing to channel messages.
Operation description
Creates a callback for subscribing to channel messages. For example, when creating a callback, you can configure parameters such as the callback URL and event types.
QPS limit
The single-user QPS limit for this operation is 100 calls per second. If the limit is exceeded, API calls are throttled, which may affect your business. Call this operation appropriately.
Try it now
Test
RAM authorization
|
Action |
Access level |
Resource type |
Condition key |
Dependent action |
|
live:CreateEventSub |
none |
*Rtc
|
None | None |
Request parameters
|
Parameter |
Type |
Required |
Description |
Example |
| AppId |
string |
Yes |
The ID of the application to subscribe to. You can view your application IDs by navigating to ApsaraVideo Live > Live+ > ApsaraVideo Real-time Communication > Application Management. If no application exists, create one by clicking [Create Application]. |
9qb1**** |
| ChannelId |
string |
No |
The ID of the channel to subscribe to. You can call the ListEventSub operation to query the subscribed channel IDs. Note
|
123333 |
| Users |
array |
No |
The users whose messages you want to subscribe to. If this parameter is empty, all users in the channel (including streamers and viewers) are subscribed. Format: |
|
|
string |
No |
The user ID. |
user1 |
|
| Events |
array |
Yes |
The subscription events. |
|
|
string |
Yes |
The subscription event. Valid values:
|
ChannelEvent |
|
| CallbackUrl |
string |
Yes |
The callback URL. For the callback content, see the callback content examples below. |
http://****.com/callback |
Callback
The following example shows the content that is called back to the user through the specified CallbackUrl:
Request:
POST /callbackURL
Body
application/json
{
"MsgId": "Message ID",
"MsgTimestamp": 12312324, // Unix timestamp when the message is sent
"SubscribeID": "Subscription ID",
"AppId":"", // The AppId that generated this message
"ChannelID":"", // The channel that generated this message
"Contents": [
{
"Event": "UserEvent",// Subscription event: user event within a channel
"UserEvent": {
"UserId": "80331631628*****", // User ID
"EventTag": "Publish", // Event, including Join, Leave, Publish, Unpublish, Roleupdate
"SessionId": "0dr15rrnhkz0jnvz6o8sxo0*****", // The SessionID that generated this event
"Timestamp": 1609854786, // Unix timestamp when the event occurred
"Reason": 1, // Reason for joining or leaving. Only available for Join events.
"Role": 1, // Role type: streamer or viewer
"CurrentMedias":"1,2,3"// Stream type: the streams published by the user
}
},
{
"Event": "ChannelEvent",// Subscription event: channel event
"ChannelEvent": {
"ChannelId": "88888****",
"EventTag": "Open", // Channel event, including Open and Close
"Timestamp": 1609854530 // Unix timestamp when the event occurred
}
}
]
}
Response
HTTP STATUS 200
UserEvent
| Parameter | Type | Required | Description |
| UserId | string | Yes | The user ID. |
| SessionId | string | Yes | The user session ID. |
| EventTag | string | Yes | The event type. Valid values: Join: joins the channel. Leave: leaves the channel. PublishVideo: starts publishing a video stream. PublishAudio: starts publishing an audio stream. PublishScreen: starts screen sharing. UnpublishVideo: stops publishing a video stream. UnpublishAudio: stops publishing an audio stream. UnpublishScreen: stops screen sharing. Roleupdate: switches the role. |
| Timestamp | number | Yes | The timestamp when the event occurred. |
| Reason | integer | Yes | The reason for joining or leaving (only available for Join events). Valid values: 1: normal join or leave. 2: reconnection join (the user already exists in the channel and joins again). 3: cross-channel relay. 4: timeout leave. 5: the user starts a new session and the current session is forced offline. 6: kicked out. 7: channel dismissed. |
| Role | integer | Yes | The role type. Valid values: 1: streamer. 2: viewer. |
| CurrentMedias | integer | Yes | The stream type. Valid values: 1: audio. 2: video. 3: screen sharing. |
ChannelEvent
| Parameter | Type | Required | Description |
| EventTag | string | Yes | The event type. Valid values: Open: the meeting starts. Close: the meeting ends. |
| Timestamp | number | Yes | The timestamp when the event occurred. |
Callback authentication
The event callback authentication feature is enabled by default. The authentication logic is as follows:
When ApsaraVideo Live initiates a callback request, the HTTP(S) request header contains the Ali-Rtc-Timestamp and Ali-Rtc-Signature fields for the callback message receiving server to perform signature authentication. The Ali-Rtc-Timestamp value is calculated as follows: Ali-Rtc-Signature=MD5SUM(MD5CONTENT), where MD5CONTENT=Callback domain name|Ali-Rtc-Timestamp value|Authentication Key. The callback domain name is the domain name configured in the callback URL, and the authentication Key is the AppKey generated when the AppId was created.
When the callback message receiving server receives a callback message, it concatenates the callback domain name, Ali-Rtc-Timestamp value, and authentication Key, calculates the MD5 value to obtain an encrypted string, and then compares the calculated encrypted string with the Ali-Rtc-Signature field value in the HTTP(S) request header initiated by ApsaraVideo Real-time Communication. If they do not match, the request is invalid.
Callback exception retry
When Alibaba Cloud initiates a callback request, the callback is considered successful only when your business server responds with HTTP status code 200. If the callback fails, Alibaba Cloud retries 7 times at intervals of 1 second, 2 seconds, 5 seconds, 10 seconds, 1 minute, 2 minutes, and 5 minutes. Each retry generates a corresponding callback record.
Exception handling
Clients that have joined a channel or are publishing streams maintain a heartbeat keep-alive mechanism with the Alibaba Cloud server. When the heartbeat keep-alive fails because the client loses network connectivity or the app is closed abnormally (failure is determined if no heartbeat is received from the client for 90 seconds), the server determines that the client has timed out and left abnormally, and generates event callbacks for the user stopping stream publishing and leaving the channel.
Response elements
|
Element |
Type |
Description |
Example |
|
object |
Schema of Response |
||
| RequestId |
string |
The request ID. |
760bad53276431c499e30dc36f6b**** |
| SubscribeId |
string |
The ID of the created subscription. |
ad53276431c**** |
Examples
Success response
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. |
| 500 | ServerError | %s. | Unknown error, please try again later or submit a ticket for consultation. |
| 403 | NoAuth | %s. | No permission |
| 404 | ResourceNotExist | %s. | The requested resource does not exist, please check and try again |
See Error Codes for a complete list.
Release notes
See Release Notes for a complete list.