Hello, I don't quite understand the webhook flow

Hello, I don’t quite understand the webhook flow.

So far I’m thinking: A user makes a request to my server which when sends the webhook subscribe post request to Twitch, then Twitch sends confirm request to my callback (on my server). Then when someone goes online Twitch sends a webhook notification to my server and my server responds with an HTTP success (2xx) response code and also sends the webhook notification to my client side?

Webhook Subscribe: User request -> My server (makes the wehbhook subscribe req) -> Twitch -> My server (callback) confirm -> Twitch

Notification: Twitch -> My server (callback) -> Twitch Confirm & notification sent to the client

Also, Webhooks/WebSub is new thing and PubSub is the old right?

  1. Your server POSTS a subscribe request to Twitch
  2. Twitch sends a GET request to your callback URL to verify it’s accessible (at this point you should verify it’s something you actually requested, if you blindly accept all requests it’d allow malicious devs to flood you with subscriptions to high traffic topics) and your server should respond with the hub.challenge that’s in the querystring.
  3. You can check to ensure the subscription has been created by using the Get Webhook Subscriptions endpoint Reference | Twitch Developers
  4. Twitch will send a POST request to your callback URL with notifications for the topic you subscribed to. When you get a notification you should verify the authenticity of it using the secret you provided when subscribing to that topic.

What triggers that whole process is entirely up to you, if you want to wait for a user to request and then subscribe to topics for that user then you can do that, or you can just subscribe to whatever topics you like.

Webhooks are newer, and PubSub is older, but they are completely different systems for different use cases. Neither is a replacement for the other.

Thanks for the help!
I managed to receive the notifications.

How do I identify the channel from an Offline notification?
The only identification I could find is the “user_id” in the property Link in headers.

      '<https://api.twitch.tv/helix/webhooks/hub>; rel="hub", <https://api.twitch.tv/helix/streams?user_id=527735084>; rel="self"',
     'Twitch-Notification-Id': 'da2cabdb-daa4-44c1-afe8-51f8b8a412ae',

You shouldn’t use the link header to determine which topic the notification is for, as that goes against the WebSub spec.

There are 2 ways usually used to identify the topic and that is either by callback URL path, or by callback URL querystring param.

So for example you could have a callback url of https://my.domain/webhooks/streamChanged/1234, and then if you’re using something like Express you could have a single route handler such as:

app.post('/webhooks/:topic/:userID', (req, res) => {
    // You can identify the topic/userID with req.params.topic, req.params.userID

Or whatever the equivalent is in the language/hosting service you’re using.

Alternatively another way would be to use a querystring, such as https://my.domain/webhooks?topic=streamChanged&userID=1234 which can be accessed in much the same way as path parameters.

This would mean each topic you subscribe to would have its own unique callback URL, but they would all be going to the same handler, just with path/querystring params to uniquely identify the topic and user associated with notifications coming in.

Ahh, didn’t think about that.

Thanks a lot for the help!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.