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 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://');

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

ws.on(‘open’, function open() {
ws.send(‘CAP REQ’);
ws.send('PASS ’ + auth);
ws.send('NICK ’ + nick);
ws.send(‘JOIN #’ + channel);

//show raw data
ws.on(‘message’, function(data){

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

1 Like

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:// 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


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="//"></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 ... */

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

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