How do I send an AJAX request to Twitch?

Not favoritedFavorited Favorited 0 favourites
  • 3 posts
From the Asset Store
The soundstage is set for an immersive listening experience sending your audience adrift.
  • Hi,

    I’m trying to build an app that subscribes to Twitch’s eventsub API:

    dev.twitch.tv/docs/eventsub/handling-websocket-events

    I’ve used the Websocket plugin to connect to:

    wss://eventsub.wss.twitch.tv/ws

    When I get a session_welcome message, I extract the session ID.

    What I then want to do is to use the AJAX plugin to send a request to subscribe to various events.

    (I know how to set headers for the AJAX request.)

    I have a template for this request saved as a JSON project file:

    { "type": "channel.chat.message", "version": "1", "condition": { "broadcaster_user_id": "xxxxxx" }, "transport": { "method": "websocket", "session_id": "YOUR_SESSION_ID" } }

    I can load this file using the AJAX plugin, but I’m struggling to:

    1. Once this file is loaded, how can I replace “YOUR_SESSION_ID” with the session ID returned when I connect to the WSS?
    2. Then how do I format this so that I can use it in the data parameter of the AJAX request?
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I have a template for this request saved as a JSON project file:

    It doesn't have to be a file, you can simply store this JSON string in a text variable. Then to replace session ID use:

    Set jsonString to replace(jsonString, "YOUR_SESSION_ID", extractedSessionID)

    Another option is to load (parse) it into JSON object and use this action:

    JSON Set value at "transport.session_id" to extractedSessionID

    Then how do I format this so that I can use it in the data parameter of the AJAX request?

    This should be in Twitch documentation I suppose. If you are using JSON object, you can convert it back to string with JSON.ToCompactString expression.

  • Thanks - I need to have play around with the JSON manipulation stuff.

    But the second part of the query was around how to get something like this example curl request working in the AJAX plugin.

    curl -X POST 'https://api.twitch.tv/helix/eventsub/subscriptions' \

    -H 'Authorization: Bearer 2gbdx6oar67tqtcmt49t3wpcgycthx' \

    -H 'Client-Id: wbmytr93xzw8zbg0p1izqyzzc5mbiz' \

    -H 'Content-Type: application/json' \

    -d '{

    "type": "channel.chat.message",

    "version": "1",

    "condition": {

    "broadcaster_user_id": "12826",

    "user_id": "141981764"

    },

    "transport": {

    "method": "webhook",

    "callback": "https://your-webhook-handling-server.com",

    "secret":"s3cre7"

    }

    }'

    Specifically the -d data part

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)