Build a WebSocket channel
Follow the WebSocket upgrade
Trace the HTTP handshake into a persistent ws or wss connection and understand what infrastructure must support.
A WebSocket does not start as a socket. It starts as a normal HTTP request that asks to switch protocols. Only after the server answers 101 Switching Protocols do you get a framed two-way channel.
When the operator console fails to connect, the bug is often in the handshake path, not in your onmessage handler.
What the client sends
Production should use wss: (WebSocket over TLS), the same way you use HTTPS:
const socket = new WebSocket('wss://status.example.com/operator')
The first HTTP request includes upgrade headers:
GET /operator HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
The server replies with 101 and a Sec-WebSocket-Accept value derived from the key. After that, bytes on the wire are WebSocket frames, not HTTP.
Minimal Node server
The ws library handles the handshake for you. See Using WebSockets with Node.js for a longer walkthrough:
const WebSocket = require('ws')
const wss = new WebSocket.Server({ port: 8080 })
wss.on('connection', (ws) => {
ws.send(JSON.stringify({ type: 'ready', v: 1 }))
})
Connect from a browser or wscat -c ws://localhost:8080. You should receive the ready frame immediately. If you get 404 or 502, capture the HTTP response before the upgrade and read the status line.
Where upgrades break
Reverse proxies must forward Upgrade and Connection headers and allow idle connections. A proxy that buffers the request body or strips those headers leaves the client hanging on CONNECTING.
TLS termination adds another hop. The browser speaks wss to the edge; the origin may speak plain ws behind the firewall. Both sides must agree the upgrade succeeded.
Capture one successful and one failed handshake in DevTools or with curl’s verbose mode. Note the exact status code and which layer changed it. That trace beats guessing in JavaScript.
Try this on your own project: connect through the same path production uses, including TLS and the reverse proxy, before you write application messages.
Lesson completed