I’m trying to make a programm to display any message that is written in a Twitch chat, with the Twitch websocket, like Chatterino, Chatsen, etc.
I’m using this package: https://www.npmjs.com/package/ws
I’m very new to websockets and never worked with them before. I was able to connect with the websocket, but it disconnects immediately after connecting, with the Close Code: Code: 1006
. I don’t understand what this code means and an solution for the error.
const WebSocket = require('ws');
const twitchUrl = 'wss://irc-ws.chat.twitch.tv/';
const twitchChannel = ''; // my twitch username
const twitchToken = 'auth: [REDACTED]'
const ws = new WebSocket(twitchUrl);
ws.on('open', () => {
console.log('Connected to Twitch Chat');
ws.send(`JOIN ${twitchChannel}`);
ws.send(`PASS ${twitchToken}`);
ws.send(`NICK ${twitchChannel}`);
});
ws.on('message', (data) => {
let message = data.toString()
console.log(`Message: ${message}`);
});
ws.on('error', (error, code, reason) => {
console.log(`Error: ${error} Code: ${code}. Reason: ${reason}`);
});
ws.on('close', (code, reason) => {
console.log(`Connection closed! Code: ${code}. Reason: ${reason}`);
});