Exact format for WebSocket request header?

I wish it were that easy! Hehe, the irc server at irc.twitch.tv port 6667 is a “straight socket”, which would be a generic tcp socket connection and that’s fairly easy to get the data from:

var net = require('net')

var client = net.connect({
  host: 'irc.twitch.tv',
  port: '6667'
}).on('ready', () => {
    console.log('ready');
}).on('connect', () => {
    console.log('connect');
    client.write('PASS oauth:nopass\r\n');
        client.write('NICK justinfan123\r\n');

        client.write('CAP REQ :twitch.tv/commands\r\n');
        client.write('CAP REQ :twitch.tv/tags\r\n');
    client.write('JOIN #twitch\r\n');
}).on('data', buf => {
    console.log(buf.toString('utf8'));
});

No WebSocket needed, this is using node’s net module which is just a mostly a TCP connection, actually the http module is built on top of the net module as well.
Now a WebSocket is a completely different beast and in order to see that this isn’t the same thing all you need to do is replace it with irc-ws.chat.twitch.tv port 80(specifically this is a ws connection, NOT wss, wss is secure and will require the tls module instead of the net module along with generating security certificates). Notice that I’m leaving out the ws:// in front. In the anatomy of a url the “wss://” or “http://” dictate the protocol used, but if only using the underlying transport layer which is tcp in this case, it’s up to the programmer to deal with the incoming protocol per its specs.
In order for something to actually be a WebSocket it requires an http handshake which itself means that request headers MUST be sent and then receive a response of 101 (the Switching Protocols code). From the specs:

1.2.  Protocol Overview

   _This section is non-normative._

   The protocol has two parts: a handshake and the data transfer.

   The handshake from the client looks as follows:

        GET /chat HTTP/1.1
        Host: server.example.com
        Upgrade: websocket
        Connection: Upgrade
        Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
        Origin: http://example.com
        Sec-WebSocket-Protocol: chat, superchat
        Sec-WebSocket-Version: 13

   The handshake from the server looks as follows:

        HTTP/1.1 101 Switching Protocols
        Upgrade: websocket
        Connection: Upgrade
        Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
        Sec-WebSocket-Protocol: chat

also better at describing than me:

### [1.7](https://tools.ietf.org/html/rfc6455#section-1.7). Relationship to TCP and HTTP

_This section is non-normative._ The WebSocket Protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request.

Basically, it uses http requests for the upgrade and then converts to that upgrade.

I’ll have to browse the package a bit more to see if I can get the request headers, but the response headers are fairly readily available for your connection in your examples. Adding a new listener of

.on('upgrade', response => {
        console.log(response.rawHeaders);
});

to the socket Object in your github examples above will show the response headers that the http connection is sending to confirm the upgrade from http→WebSocket.

Slight misunderstanding there, not looking to upgrade from a WebSocket(wss) to anything, I’m looking for the header format of the request in order to upgrade from HTTP to a WebSocket protocol, though it appears I may have the answer available to me now and I can start testing that. The ws library contains a “.protocol” entry for a client connection, and since as your example shows it’s pretty easy to connect using the library:

const WebSocket = require('ws');
socket = new WebSocket('wss://irc-ws.chat.twitch.tv');

socket.on('close', () => {
        console.log('Closed');
}).on('open', () => {
        console.log('Opened');
        console.log('Send Conn stuff');

        socket.send('PASS oauth:nopass');
        socket.send('NICK justinfan123');

        socket.send('CAP REQ :twitch.tv/commands');
        socket.send('CAP REQ :twitch.tv/tags');
    socket.send('JOIN #twitch');
}).on('message', (raw_data) => {
        console.log(raw_data);
}).on('upgrade', response => {
        console.log(response.rawHeaders);
});

all else fails I’ll use the ws library to create a WebSocket server and check what the the connection looks like on that end =)

I’ll see how far I can get from here, thanks for your time and patience :slight_smile: