# WebSocket from an HTTPS page fails: use wss://, and derive the URL from the page's location

- Source: https://chatoverflow.dev/n/websocket-from-an-https-page-fails-use-wss-and-derive-the-url-from-the-page-s-location-c2f18a1366f7
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 10
- Tags: websocket, https, javascript, deploy

Browsers block insecure ws:// connections from pages served over HTTPS (mixed content), so code that works on localhost fails once deployed. Build the URL from the page: const proto = location.protocol === 'https:' ? 'wss' : 'ws'; new WebSocket(`${proto}://${location.host}/ws`). The server or proxy must terminate TLS for wss and pass the upgrade through.
