# How to connect to chat and send message

**URL:** <https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543>\
**Category:** Chat and IRC\
**Created:** [September 11, 2022, 5:16pm UTC](https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543 "2022-09-11T17:16:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![cxzivxizeoqc](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@cxzivxizeoqc](https://discuss.dev.twitch.com/u/cxzivxizeoqc)\
**Post date:** [September 11, 2022, 5:16pm UTC](https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543/1 "2022-09-11T17:16:05Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [September 11, 2022, 5:19pm UTC](https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543/2 "2022-09-11T17:19:00Z")

</div>

Joining a chat room to recieve messages

- [Joining a Twitch Chat Room | Twitch Developers](https://dev.twitch.tv/docs/irc/join-chat-room)

> client.send(‘JOIN #somechannel’);

sending a message

- [Sending and Receiving Chat Messages | Twitch Developers](https://dev.twitch.tv/docs/irc/send-receive-messages)

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

---

<div class="post-metadata">

**Author:** ![cxzivxizeoqc](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@cxzivxizeoqc](https://discuss.dev.twitch.com/u/cxzivxizeoqc)\
**Post date:** [September 11, 2022, 7:27pm UTC](https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543/3 "2022-09-11T19:27:56Z")

</div>

Should I just write like this?

```auto
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](https://d2opxh93rbxzdn.cloudfront.net/original/3X/f/f/ff261a89a0fc13f38242bc72d0116daf85b5746d.png)

 ![image](https://d2opxh93rbxzdn.cloudfront.net/original/3X/2/9/2926f19041eeb177fdf864daac64f04203ac1e00.png)

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [September 11, 2022, 7:38pm UTC](https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543/4 "2022-09-11T19:38:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cxzivxizeoqc](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@cxzivxizeoqc](https://discuss.dev.twitch.com/u/cxzivxizeoqc)\
**Post date:** [September 11, 2022, 7:46pm UTC](https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543/6 "2022-09-11T19:46:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cxzivxizeoqc](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@cxzivxizeoqc](https://discuss.dev.twitch.com/u/cxzivxizeoqc)\
**Post date:** [September 11, 2022, 7:51pm UTC](https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543/7 "2022-09-11T19:51:00Z")

</div>

like here:

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

```

?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [September 11, 2022, 8:10pm UTC](https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543/8 "2022-09-11T20:10:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cxzivxizeoqc](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@cxzivxizeoqc](https://discuss.dev.twitch.com/u/cxzivxizeoqc)\
**Post date:** [September 11, 2022, 8:18pm UTC](https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543/9 "2022-09-11T20:18:38Z")

</div>

```auto
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…

 ![image](https://d2opxh93rbxzdn.cloudfront.net/original/3X/c/f/cff4dea89a1b93f309ee7c4930a824c771daf004.jpeg)

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [September 11, 2022, 8:19pm UTC](https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543/10 "2022-09-11T20:19:37Z")

</div>

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

Nick are not preceeded with `#`

---

<div class="post-metadata">

**Author:** ![cxzivxizeoqc](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@cxzivxizeoqc](https://discuss.dev.twitch.com/u/cxzivxizeoqc)\
**Post date:** [September 11, 2022, 8:55pm UTC](https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543/11 "2022-09-11T20:55:57Z")

</div>

![image](https://d2opxh93rbxzdn.cloudfront.net/original/3X/5/f/5ff521d3d54f94563fc7d12fa9dfc027f20eaf58.png)  
 ![image](https://d2opxh93rbxzdn.cloudfront.net/original/3X/d/4/d4ffe93ba8b43c15453dec09d13a5284b00a2325.jpeg)  
I remove # from `connection.sendUTF('NICK #s4tont');`

---

<div class="post-metadata">

**Author:** ![cxzivxizeoqc](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@cxzivxizeoqc](https://discuss.dev.twitch.com/u/cxzivxizeoqc)\
**Post date:** [September 12, 2022, 9:18am UTC](https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543/12 "2022-09-12T09:18:00Z")

</div>

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:

```auto
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:

```auto
/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

```

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [September 12, 2022, 9:33am UTC](https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543/13 "2022-09-12T09:33:29Z")

</div>

> [@BarryCarlyon](#):
>
> You cast the websocket to `client` then tried to use it under `connection`

`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.

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [October 12, 2022, 9:33am UTC](https://discuss.dev.twitch.com/t/how-to-connect-to-chat-and-send-message/40543/14 "2022-10-12T09:33:45Z")

</div>

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