Skip to content

Commit

Permalink
docs: clarify some eventsub details
Browse files Browse the repository at this point in the history
  • Loading branch information
iProdigy committed Jan 25, 2024
1 parent 6b71f7c commit 4ebeaf2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
22 changes: 22 additions & 0 deletions docs/versioned_docs/version-1.x/eventsub/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,25 @@ The library handles websockets for you, so this transport tends to be easier to
Webhooks, on the other hand, require more code on your end to establish an HTTPS webserver and keep the EventSub subscription healthy.

:::

## Subscription Basics

To receive EventSub events, you must create an EventSub subscription.
Each subscription has a `type`, which identifies the type of event you wish to receive.
Twitch list all of the public subscription types in their [documentation](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/).

Each subscription has a `condition`, which identifies the parameters under which the event fires.
For example, [most](https://twitch4j.github.io/javadoc/com/github/twitch4j/eventsub/condition/ChannelEventSubCondition.html) subscriptions are conditioned by the ID of the channel in which the event occurred.

Each subscription has a `cost`, which is determined by the type of authorization you have.
By default, the `cost` of a subscription is 1, but is reduced to 0 if you have a user access token from the channel related to the subscription.
Each transport mechanism imposes different limits on the total cost (i.e., `max_total_cost`) across your enabled subscriptions for that transport type.

To establish an EventSub subscription, you must obtain an instance of `EventSubSubscription`, which is passed to [`TwitchHelix#createEventSubSubscription`](https://twitch4j.github.io/javadoc/com/github/twitch4j/helix/TwitchHelix.html#createEventSubSubscription(java.lang.String,com.github.twitch4j.eventsub.EventSubSubscription)).
It is easiest to create `EventSubSubscription` instances through our [`SubscriptionTypes`](https://twitch4j.github.io/javadoc/com/github/twitch4j/eventsub/subscriptions/SubscriptionTypes.html) utility class: [`SubscriptionType#prepareSubscription`](https://twitch4j.github.io/javadoc/com/github/twitch4j/eventsub/subscriptions/SubscriptionType.html#prepareSubscription(java.util.function.Function,com.github.twitch4j.eventsub.EventSubTransport)) creates a subscription for the specified condition and transport, which should be passed to [`IEventSubSocket#register`](https://twitch4j.github.io/javadoc/com/github/twitch4j/eventsub/socket/IEventSubSocket.html#register(com.github.twitch4j.eventsub.EventSubSubscription)) (when using websocket) or [`TwitchHelix#createEventSubSubscription`](https://twitch4j.github.io/javadoc/com/github/twitch4j/helix/TwitchHelix.html#createEventSubSubscription(java.lang.String,com.github.twitch4j.eventsub.EventSubSubscription)) (when using webhook).

You can query your oustanding EventSub subscriptions via [`TwitchHelix#getEventSubSubscriptions`](https://twitch4j.github.io/javadoc/com/github/twitch4j/helix/TwitchHelix.html#getEventSubSubscriptions(java.lang.String,com.github.twitch4j.eventsub.EventSubSubscriptionStatus,com.github.twitch4j.eventsub.subscriptions.SubscriptionType,java.lang.String,java.lang.String,java.lang.Integer)). This endpoint supports filtering by the subscription type, subscription status (e.g., enabled or disabled), and user id (for subscriptions related to a specific channel).

To delete an EventSub subscription, simply call [`IEventSubSocket#unregister`](https://twitch4j.github.io/javadoc/com/github/twitch4j/eventsub/socket/IEventSubSocket.html#unregister(com.github.twitch4j.eventsub.EventSubSubscription)) (when using websocket) or [`TwitchHelix#deleteEventSubSubscription`](https://twitch4j.github.io/javadoc/com/github/twitch4j/helix/TwitchHelix.html#getEventSubSubscriptions(java.lang.String,com.github.twitch4j.eventsub.EventSubSubscriptionStatus,com.github.twitch4j.eventsub.subscriptions.SubscriptionType,java.lang.String,java.lang.String,java.lang.Integer)) (when using webhook).

For further clarification, please read through Twitch's official [documentation](https://dev.twitch.tv/docs/eventsub/manage-subscriptions/) on managing EventSub subscriptions.
14 changes: 12 additions & 2 deletions docs/versioned_docs/version-1.x/eventsub/webhook.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ With webhooks, you utilize an app access token to create and delete EventSub sub
Since certain subscription types require user authorization, these users must authenticate with your Client ID
to generate a user access token (but you do not need to maintain this user token to utilize webhooks).

If the subscription does not have a scope requirement, you do not need user authorization, but the subscription will have a `cost` of 1.
When your Client ID has user authorization, the subscription has a `cost` of 0.
By default, every Client ID has a `max_total_cost` of 10,000 for webhooks.

When your webhooks server start, you should call [`TwitchHelix#getEventSubSubscriptions`](https://twitch4j.github.io/javadoc/com/github/twitch4j/helix/TwitchHelix.html#getEventSubSubscriptions(java.lang.String,com.github.twitch4j.eventsub.EventSubSubscriptionStatus,com.github.twitch4j.eventsub.subscriptions.SubscriptionType,java.lang.String,java.lang.String,java.lang.Integer))
to check whether your EventSub subscriptions are already healthy (so you don't create duplicate subscriptions).
Twitch imposes a limit of three (3) subscriptions with the same `type` and `condition` values.

To create EventSub subscriptions, you would call [`TwitchHelix#createEventSubSubscription`](https://twitch4j.github.io/javadoc/com/github/twitch4j/helix/TwitchHelix.html#createEventSubSubscription(java.lang.String,com.github.twitch4j.eventsub.EventSubSubscription)).
The first argument must be an app access token (or `null` if `withDefaultAuthToken` is an app access token OR `withClientId`/`withClientSecret` is specified while `withDefaultAuthToken` is not specified).
Expand Down Expand Up @@ -88,6 +93,9 @@ It is acceptable to utilize the same secret across multiple EventSub subscriptio

:::

While Twitch does not officially document their specific retry policy, it has been observed that they employ exponential backoff to retry failed notifications up to 5 times over a couple minutes. If all retries are exhausted without the webserver responding with a 2xx code, the EventSub subscription would be disabled.


## Web Server Logic

You can use any framework you desire to implement the webhook server.
Expand All @@ -97,11 +105,13 @@ For example, [this gist](https://gist.github.com/iProdigy/53f61e439b81d1d926748d
This example highlights a few important notes:

* To deserialize incoming notification bodies, you can utilize our [`TypeConvert`](https://twitch4j.github.io/javadoc/com/github/twitch4j/common/util/TypeConvert.html) helper. <sup>Under the hood, [`NotificationDeserializer`](https://github.com/twitch4j/twitch4j/blob/master/eventsub-common/src/main/java/com/github/twitch4j/eventsub/util/NotificationDeserializer.java) is used to create [`EventSubNotification`](https://twitch4j.github.io/javadoc/com/github/twitch4j/eventsub/EventSubNotification.html) instances.</sup>
* You should validate the request headers to ensure authenticity and avoid replay attacks. The example above shows how to utilize [`EventSubVerifier`](https://github.com/twitch4j/twitch4j/blob/master/eventsub-common/src/main/java/com/github/twitch4j/eventsub/util/EventSubVerifier.java) to easily perform these verification checks.
* Upon receiving a valid [notification challenge](https://twitch4j.github.io/javadoc/com/github/twitch4j/eventsub/EventSubNotification.html#getChallenge()), you should set the response body to the same challenge string (and yield a 200 status).
* You should [validate](https://dev.twitch.tv/docs/eventsub/handling-webhook-events/#verifying-the-event-message) the request headers to ensure authenticity and avoid replay attacks. The example above shows how to utilize [`EventSubVerifier`](https://github.com/twitch4j/twitch4j/blob/master/eventsub-common/src/main/java/com/github/twitch4j/eventsub/util/EventSubVerifier.java) to easily perform these verification checks.
* Upon receiving a valid [notification challenge](https://twitch4j.github.io/javadoc/com/github/twitch4j/eventsub/EventSubNotification.html#getChallenge()), you should set the response body to the same [challenge](https://dev.twitch.tv/docs/eventsub/handling-webhook-events/#responding-to-a-challenge-request) string (and yield a 200 status).

:::tip

For debugging, you can utilize the [Twitch CLI](https://github.com/twitchdev/twitch-cli#download) to [verify](https://github.com/twitchdev/twitch-cli/blob/main/docs/event.md#verify-subscription) subscriptions and [trigger](https://github.com/twitchdev/twitch-cli/blob/main/docs/event.md#trigger) notifications.

:::

For any further clarification, please refer to Twitch's official [documentation](https://dev.twitch.tv/docs/eventsub/handling-webhook-events/) on handling webhook events.
4 changes: 2 additions & 2 deletions docs/versioned_docs/version-1.x/eventsub/websocket.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ EventSub notifications can also be received over websockets, aka EventSockets.

When using websockets, you create eventsub subscriptions with a **user** access token.

For each user ID, you can create up to 900 enabled subscriptions (but `max_total_cost` is only 10 across all subscriptions).
For each user ID, you can create up to 900 enabled subscriptions (but `max_total_cost` is only 10 across all subscriptions for that `client_id`-`user_id` tuple).

Twitch4J automatically creates additional websockets to comply with Twitch's limits (each websocket can have up to 300 enabled subscriptions, and each user ID can create up to 3 websocket connections for a given client ID).

Expand Down Expand Up @@ -175,7 +175,7 @@ In addition to firing the corresponding events for each subscription type, the w
* [`EventSocketClosedByTwitchEvent`](https://twitch4j.github.io/javadoc/com/github/twitch4j/eventsub/socket/events/EventSocketClosedByTwitchEvent.html): Called when Twitch decides to close our EventSocket.


## Example
## Code Example


<Tabs groupId="code">
Expand Down

0 comments on commit 4ebeaf2

Please sign in to comment.