Explore Bato Web Agency Webflow Certified Partner Profile Explore Bato Web Agency Webflow Certified Partner Profile

WebSocket closes before it connects: trace the failed handshake

Illustration without a description Illustration without a description

Record whether open fired. If the socket closes first, inspect the handshake in Network. A hostname or TLS failure can stop it before an HTTP response. A 404 can point to the wrong route. For an HTTP/1.1 upgrade, a 200 can mean a regular handler answered instead of switching protocols.

 

On an HTTPS page, use the intended wss URL. If the request reaches the server, check the exact route, authentication, origin and proxy upgrade handling. If it never reaches the server, check DNS and TLS from the affected network.

 

The snippet timestamps open, error and close. If open never fires, the logs help you align the attempt with the Network trace. If open does fire, inspect what the app sent or whether it called close() after navigation. Send application data only once the connection is open.

 

const socket = new WebSocket('wss://example.com/live'); const start = performance.now(); for (const type of ['open', 'error', 'close']) { socket.addEventListener(type, event => { const elapsed = Math.round(performance.now() – start); console.log(elapsed, type, socket.readyState, event.code ?? ''); }); }

 

Repeat one failed and one working connection and identify the first event or Network entry where their timelines differ.