How do I send an AJAX request to Twitch?

Not favoritedFavorited Favorited 0 favourites
  • 6 posts
From the Asset Store
Easily create "twitch-plays" minigames using audience participation and twitch API
  • 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?
  • 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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You need to use the Websocket plugin. Have you tried sending the data with "Websocket send text" action?

  • I need to send a POST request to Twitch's /subscriptions endpoint (using Construct's AJAX plugin) because:

    IMPORTANT The EventSub WebSocket server supports only outgoing messages. If you send a message to the server, except for Pong messages, the server closes the connection.

    I just need to figure how how to format the data - I think I should be able to do that - especially now I know how to manipulate the templated JSON.

    EDIT - I think the flow is:

    1. Load the templated JSON from a project file
    2. Insert the session ID
    3. JSON.ToCompactString
    4. URLEncode
  • Got this working - didn't need URLEncode.

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