Twitch.TV Web Browser Chat

Hey guys, I’m trying to get something to work to load & display chat similarly to how it does on web browsers on Twitch.tv. But it requires a username and auth token. On the website you don’t need to be logged in. What am i doing wrong?!

Here is my code. It doesn’t let me connect without login credentials however…

var ws = new WebSocket('ws://192.16.64.145:80');

var nick = ‘’; //all lowercase
var auth = ‘’; //include oauth:xxxx
var channel = ‘lirik’;

ws.on(‘open’, function open() {
ws.send(‘CAP REQ :twitch.tv/tags twitch.tv/commands twitch.tv/membership’);
ws.send('PASS ’ + auth);
ws.send('NICK ’ + nick);
ws.send(‘JOIN #’ + channel);
});

//show raw data
ws.on(‘message’, function(data){
console.log(data);
});

// reply to ping
ws.on(‘message’, function(data){
if (data.lastIndexOf(‘PING’, 0) === 0) {
ws.send(‘PONG :tmi.twitch.tv’);
console.log(‘PONG Sent\r\n’);
}
});

Set NICK to “justinfan” followed by any numbers (e.g. justinfan123) and it will be treated as an anonymous read-only login that works with any PASS (you can even leave PASS out).

As a side-note, it’s best to use wss://irc.chat.twitch.tv/ for the web socket.

Thank you so much! I got past that handshake error now. However, now I’m getting a timeout error response. What would be causing this?

That may be caused by not properly responding to pings.

Twitch sends a ping like this:

PING irc.twitch,tv

You have to respond with

PONG irc.twitch.tv

whenever you receive this message.

I recommend using tmi.js as it can also be used in the browser. Here’s the documentation for tmi.js.

    <script src="//cdn.tmijs.org/js/latest/tmi.js"></script>
    <script>
    var nick = '',
        auth = '',
        channel = '',
        
        client = new irc.client({
                connection: {
                        reconnect: true,
                        secure: true
                    },
                identity: {
                        username: nick,
                        password: auth
                    },
                channels: [channel]
            });
    client.on('message', (channel, user, message, fromSelf) {
            console.log(fromSelf ? '[YOU]' : '', channel.slice(1, 5), user['display-name'] + ':', message);
            if(fromSelf) {
                return false;
            }
            /* etc ... */
        });
    client.connect();
    </script>

There are some updates that will be coming to tmi.js soon in the next branch.