How to connect to chat and send message

I connect to Twitch Chat via a socket, using an IRC style interface.
My code:

const WebSocketClient = require('websocket').client;
const client = new WebSocketClient();

client.on('connectFailed', function(error) {
    console.log('Connect Error: ' + error.toString());
});

client.on('connect', function(connection) {
    console.log('WebSocket Client Connected');
    
    // Send CAP (optional), PASS, and NICK messages
});

client.connect('ws://irc-ws.chat.twitch.tv:80');

But how to send a message immediately after connecting? And how do you choose which channel to connect to?
I just don’t know how to connect to the chat

Joining a chat room to recieve messages

client.send(‘JOIN #somechannel’);

sending a message

client.send(‘PRIVMSG #somechannel :words to send’);

Should I just write like this?

const WebSocketClient = require('websocket').client;
const client = new WebSocketClient();

client.on('connectFailed', function(error) {
    console.log('Connect Error: ' + error.toString());
});

client.on('connect', function(connection) {
    console.log('WebSocket Client Connected');
    
    // Send CAP (optional), PASS, and NICK messages
});

client.connect('ws://irc-ws.chat.twitch.tv:80');

client.send(‘JOIN S4tont’);
client.send(‘PRIVMSG S4tont :hello!’);

image

Channel names are all lowercase and preceeded by #

You also seem to lack the PASS and NICK commands to login to the server as documented

I fix this error. instead of such quotes `` put such "

like here:

connection.sendUTF('PASS oauth:yfvzjqb705z12hrhy1zkwa9xt7v662');
connection.sendUTF('NICK myusername');

?

Sure but with the correct username an an oAuth token for it

const WebSocketClient = require('websocket').client;
const client = new WebSocketClient();

client.on('connectFailed', function(error) {
    console.log('Connect Error: ' + error.toString());
});

client.on('connect', function(connection) {
    console.log('WebSocket Client Connected');
    connection.sendUTF('PASS oauth:vz9fcq1xv0qxxr7kcr2g9btubgdofayyzq2r142p7kygtqx12zde4ugpdx83tt1qr35u6jqlcbbn4ix526rgsfxiypq130z2kbqflvxyxf5jnl4j7ued2nyt17b9iayzcg959kxw2cp69q4e3mzkfmfwhtmy46601oy46z6uf6qus2y5qq9egoo1c8f7qvpwreen0gfdnb57o6xscqpdddfyfe62oykiniiu7zatq44imae3rjxr2un04r1ut2jd6j3tnt2oao19l7ro43j12nn1h1rmc71tvtxtm7qm324zbvpwn6g3i1rxyfnw4f0pero884hedm3ilxe6qa47lcb7ou01m47g4oknw1gizt9vwh7m8cowi5jdalf5t87vpb5e9v5kpw170iimnykjf8x4ao0icrf5y4dv');
    connection.sendUTF('NICK #s4tont');
});

connection.on('message', function(ircMessage) {
  client.send('JOIN #s4tont');
  client.send('PRIVMSG #s4tont :hello!');
});

client.connect('ws://irc-ws.chat.twitch.tv:80');

should everything look like this?
But I have error…

You cast the websocket to client then tried to use it under connection

Nick are not preceeded with #



I remove # from connection.sendUTF('NICK #s4tont');

Hello! I wrote the code, but I’m not sure if I wrote it correctly. I have to connect to the chat and immediately send a message.
My code:

const WebSocketClient = require('websocket').client;
const client = new WebSocketClient();

client.on('connectFailed', function(error) {
    console.log('Connect Error: ' + error.toString());
});

client.on('connect', function(connection) {
    console.log('WebSocket Client Connected');
    connection.sendUTF('PASS oauth:vz9fcq1xv0qxxr7kcr2g9btubgdofayyzq2r142p7kygtqx12zde4ugpdx83tt1qr35u6jqlcbbn4ix526rgsfxiypq130z2kbqflvxyxf5jnl4j7ued2nyt17b9iayzcg959kxw2cp69q4e3mzkfmfwhtmy46601oy46z6uf6qus2y5qq9egoo1c8f7qvpwreen0gfdnb57o6xscqpdddfyfe62oykiniiu7zatq44imae3rjxr2un04r1ut2jd6j3tnt2oao19l7ro43j12nn1h1rmc71tvtxtm7qm324zbvpwn6g3i1rxyfnw4f0pero884hedm3ilxe6qa47lcb7ou01m47g4oknw1gizt9vwh7m8cowi5jdalf5t87vpb5e9v5kpw170iimnykjf8x4ao0icrf5y4dv');
    connection.sendUTF('NICK S4tont');
});

connection.on('message', function(ircMessage) {
  client.send('JOIN #s4tont');
  client.send('PRIVMSG #s4tont :hello!');
});



client.connect('ws://irc-ws.chat.twitch.tv:80');

Error:

/home/user/Documents/typescript/bots/twitch/twitch.js:42
connection.on('message', function(ircMessage) {
^

ReferenceError: connection is not defined
    at Object.<anonymous> (/home/user/Documents/typescript/bots/twitch/twitch.js:42:1)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

conneciton doesn’t exist, based on the presented code it appears it should be client.

You may wish to refer to a javascript primer before continuing trying to write javascript.

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