React to user follow with the Twitch API?

Hi there,

I know that this question was probably set a lot of times, but I can’t figure it out with search on Google.

Is there a way to listen to follow events with the twitch API ? I mean, when a user is following the channel, then get a json response or something like that ?

If not, how to do that ?

Thank you very much for your help ! :slight_smile:

Yes, you can use EventSub, which at time of writing only supports a HTTP POST/Webhook notification transport.

This will broacast a HTTP POST JSON payload when a follow occurs.

Okay so I have checked the documentation, but in the JSON response there is no information about the user that follow ? Or maybe I didn’t see it…

Thank you :slight_smile:

EventSub will send a noticitaion like this:

See Channel Follow Webhook Notification Example on the link I linked:

{
    "subscription": {
        "id": "f1c2a387-161a-49f9-a165-0f21d7a4e1c4",
        "type": "channel.follow",
        "version": "1",
        "status": "enabled",
        "cost": 0,
        "condition": {
           "broadcaster_user_id": "1337"
        },
         "transport": {
            "method": "webhook",
            "callback": "https://example.com/webhooks/callback"
        },
        "created_at": "2019-11-16T10:11:12.123Z"
    },
    "event": {
        "user_id": "1234",
        "user_login": "cool_user",
        "user_name": "Cool_User",
        "broadcaster_user_id": "1337",
        "broadcaster_user_login": "cooler_user",
        "broadcaster_user_name": "Cooler_User",
        "followed_at": "2020-07-15T18:16:11.17106713Z"
    }
}

The user that followed is this section of that JSON blob.

        "user_id": "1234",
        "user_login": "cool_user",
        "user_name": "Cool_User",

Okay thank you for this precisions.
I don’t want to abuse but, how does it work ? I use JavaScript and so I have to make a POST HTTP request and wait for a user to follow ? And then when a user has follow, do I have to make an other request to wait for the next follower ?

Again, thank you so much for your time :slight_smile:

That would be if you are long polling the Follows Endpoint.

For EventSub you need to create a Server.
That server is secured behind SSL
Then when a Follower occurs Twitch will send a HTTP Post to you. to that server

You do not have to fetch anything As Twitch will notify you when a follower occurs.

EventSub is currently a Webhooks only platform.

So you tell Twitch what topics you want via Create EventSub Subscription which is the only/single HTTP POST you will make

Then when a follow occurs, Twitch will make a HTTP Post to your Server. So you don’t have to do any fetching at all.

Since you are using JavaScript here is a NodeJS Example of an EventSub Server for recieving notifications from Twitch

Thank you so much I will check your example and comeback if I have any other questions ! :slight_smile:

Okay I see that you told me to have a server secured by SSL, but I’m working on localhost. Will it work ?

You cann’t receive WebHooks to localhost.

As Localhost to Twitch, is Twitch, not you.

If you can only work on localhost then you can either long poll the API, or determine a way to receive webhooks.

That could be via NGROK to handle the firewall bypass or you need to host your solution on a server/cloud solution.

Mmmh, and I forgot to say that my application is running with Laragon, which is a Wamp equivalent. So it will not work with those solutions ?

If you can make this server accessable to the internet then sure.

NGROK can do that for you.
Or you can get a domain name and open a hole in your firewall, which is generally not advised.

Okay, I will check NGROK then and see if I can do something with it. Well, thank you very much for your reactivity and your help, I will be back, maybe :slight_smile:

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