Connecting to Twitch API with NodeJS and socket io

I am using NodeJS and I would like to create a connection to the Twitch API, so I can catch all the different things happening on my channel. So far I got my client ID, client secret and token. But the Twitch Dev Docs are quite overwhelming and the examples are in Curl, which I have no knowledge about. Because I am already able to connect to Streamlabs with socket.io, I thought I could do the same thing with the Twitch API. With Streamlabs, it looks like this:

const io = require('socket.io-client');
const streamlabs = io(`https://sockets.streamlabs.com?token=XXX`, {transports: ['websocket']});
streamlabs.on('connect', () => console.log('Connected with Streamlabs.'));

So with Twitch I tried it like this:

const io = require('socket.io-client');
const twitch = io(`https://sockets.twitch.com?token=XXX`, {transports: ['websocket']});
twitch.on('connect', () => console.log('Connected with Twitch.'));

Because this doesn’t work, I am not sure how to solve this. Did I write it wrong? Is this even possible with socket.io?

Thank you.

Twitch doesn’t provide a socket.io compatible endpoint. Nor is there any hint of socket.io in the docs.

Socket wise Twitch offers the following:

  • Twitch does provide a PubSub (regular sockets) endpoint.
  • Twitch will provide EventSub over sockets at some point
  • Twitch Chat is available over WebSocket as well.

All three of these will be “regular” sockets rather than socket.io, socket.io is sockets but uses a messaging encapsulation method.

Here are some node examples that do PubSub and Chat in nodeJS

So it depends on what data you are intending to obtain which then detemines what thing you will need to use.

The API is only available via HTTP interface, So cURL is used as cURL will translate to any language. Teach a developer to translate cURL into their language of choice and then you can use any API and they only need to write one version of docs!

Hey,

thanks for the answer. So I was able to connect with the chat. Now I would like to know, what information I am actually able to get? So for example:

twitch.on('message', (message) => {
    console.log(message);
});

’message’ seems to be the filter for “Just react, when something was written in the chat” and (message) is for pointing at this key to get its value, in this case the actuall written message.
But beside ‘message’, what other filters can I use? Or is there even a word for “Tell me always, no matter what”?
Same with (message): Is there a word for giving me the complete content and not just certain keys?

Depends what is twitch?

Are you using my chat exmaple, or a library?

It’s unclear what you are doing or using from this snippet.

Oh yeah, sry, here is the full thing:

const tmi = require('tmi.js');
const twitch = new tmi.client({
    identity: {
        username: 'xxx',
        password: 'oauth:xxx'
    },
    channels: ['xxx']
});

twitch.connect(console.log('Connected with Twitch.'));

twitch.on('message', (message) => {
    console.log(message);
});

You will need to refer to the tmi.js documentation

Which I can’t help with as I don’t use tmi.js I use the chat example I linked

Hey, so the last days I looked into the tmi.js dev docs and found answers. Unfortunately tmi.js can’t handle things like Channel Points. Because of that I am kinda back at point 1 and now I am trying to connect with the Pubsub system. You and the dev docs are so nice and link to JS examples. Problem is:

My knowledge is really low. As I mentioned in the start post: I get really fast overwhelmed. Imaging I am from China, Russia or any counrty, that doesn’t use the latin alphabet. I am trying to learn french and would like to start by saying “Hello, my name is…”. Your linked github examples have the translation in it, but for me they are like a 10 pager example-story with hundreds of french sentences that I have to read through now, just to see where in that story a character says “Hello, my name is…”. And the twitch dev docs are like a GIANT book with thousands of pages. I just can’t handle this. I am an idiot. When I mention things like “Unfortunately the examples in the dev docs are in cURL” and you say “cURL will translate to ANY language”, that is like me saying “The examples are in english, not french” and you answering “Well english is a global used language. So english can be translated to ANY language.” You are absoltuly right when you say that, but that doesn’t help me.
So yeah, thank you very much for trying to help me, but unfortunately you have to do it way easier for me. I am really sorry.

Looking through your example code, all I can do is guess. To make the first step, It seems like instead of writing something like…
const io = require('socket.io-client');
…I have to write…
const ws = new WebSocket('wss://pubsub-edge.twitch.tv');
…,right?

Chat libraries generally won’t.
As they handle chat rather than API or EventSub operations, which is where CP’s reside.

cURL is more like the alphabet than a different langauge but sure.
Since use seem to be using node/JS

You’ll need to pick a library to make HTTP requests with and then something like

Where the token is a user token

curl -X GET 'https://api.twitch.tv/helix/users' \
-H 'Authorization: Bearer cfabdegwdoklmawdzdo98xt2fo512y' \
-H 'Client-Id: uo6dggojyb8d6soh92zknwmi5ej1q2'

becomes, when using the HTTP Lib got

got({
    url: 'https://api.twitch.tv/helix/users',
    headers: {
        'Client-ID': 'uo6dggojyb8d6soh92zknwmi5ej1q2',
        'Authorization': 'Bearer cfabdegwdoklmawdzdo98xt2fo512y'
    },
    responseType: 'json'
})
.then(resp => {
    // process resp.body
})
.catch(err => {
    // do error processing
})

Once you know how to make HTTP calls in your language of choice then you can read any docs in cURL.

Not really no.

SocketIO is WS but encapsulated.

So socketIO you do io.on('eventDefinedByTheProvider

Where is WebSocket it’s all .on('message

It doesn’t split them into events for you it’s all one event.

So a “full” pubsub example is linked

This

  • sets up the pubsub socket connection.
  • The Ping/Pong loop functions
  • Asking for topics (fake/bad topics in the runAuth)
  • Message console.log-ing (no processing)

So

That line is the actual messge recieverm, the process msg (line 93) to see what topic it is and action/do your own stuff as needed when this triggers.