The browser refuses a ws:// connection opened from your HTTPS React app
Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.Short answer
Your page is on HTTPS but the socket URL starts with ws://, and browsers refuse that mix. Use a wss:// URL, or a relative URL that follows the page's scheme, and make sure something in front of the socket server speaks TLS. Local development hosts are the one exception, and only some of them.
Fix it fast
1Find out which cause you have
Find where the socket URL is built. A hard-coded ws:// string or an environment variable holding one is the usual source.
grep -rn -E "ws://|new WebSocket" src/ .env* 2>/dev/null2Match what you see, then jump to its fix
- 1
The error appears only on the deployed HTTPS site, not on http://localhostThe URL is hard-coded with the ws:// schemeFix - 2
Switching the URL to wss:// makes the connection fail or hang insteadThe socket server does not speak TLSFix - 3
After wss:// works, the socket closes by itself when nothing is sentConnections idle for a minute drop behind the proxyFix - 4
ws:// works for localhost or a private IP but fails for a custom hostnameLocal development against a device on your networkFix
- 1
What does the error look like?
Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.
Cause 1 of 4
The URL is hard-coded with the ws:// scheme
1Check
Search the source and the build-time environment files for the ws:// string. Browsers do not allow a non-secure socket from a page loaded over HTTPS.
grep -rn "ws://" src/ .env* 2>/dev/null2Fix
Use wss:// for the socket. The WebSocket constructor also accepts a relative URL and maps https to wss, so a path alone follows whatever scheme the page uses.
// explicit secure URL
const socket = new WebSocket("wss://example.com/ws");
// or relative, resolved against the page (https page -> wss)
const socket2 = new WebSocket("/ws");3Verify
Reload the deployed page with the console open; the constructor no longer throws.
# in the browser console after load
new WebSocket('wss://example.com/ws').readyStateConfirmed by developer.mozilla.org
Cause 2 of 4
The socket server does not speak TLS
1Check
wss is WebSocket over TLS, on port 443 by default. A plain Node.js or Java server on a high port answers wss:// with garbage. Test it from the shell.
curl -vkI https://example.com/ws2Fix
Terminate TLS in a reverse proxy and pass the upgrade headers to the plain socket server. nginx needs Upgrade and Connection set explicitly, because they are hop-by-hop headers.
location /ws {
proxy_pass http://127.0.0.1:3001;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}3Verify
Test the config, reload, and open the socket from the HTTPS page.
sudo nginx -t && sudo systemctl reload nginxConfirmed by nginx.org
Cause 3 of 4
Connections idle for a minute drop behind the proxy
1Check
nginx closes a proxied WebSocket that sends no data for 60 seconds by default.
grep -rn "proxy_read_timeout" /etc/nginx/2Fix
Raise proxy_read_timeout, or send WebSocket ping frames from the server at a shorter interval.
location /ws {
proxy_read_timeout 300s;
}3Verify
Leave the page idle past a minute and check the socket stays open.
sudo nginx -t && sudo systemctl reload nginxConfirmed by nginx.org
Cause 4 of 4
Local development against a device on your network
1Check
Chrome exempts ws:// from mixed content blocking only when the host is recognisably local: a loopback or private IP literal, localhost or a .local name. A custom hostname is not exempt.
grep -rn "ws://" src/2Fix
Use wss:// for a local endpoint reached through a custom hostname.
const socket = new WebSocket("wss://devbox.example.test:8443/ws");3Verify
Open the page and watch the console for the error.
# browser console: no mixed content errorConfirmed by developer.chrome.com
How do I stop it coming back?
- Never write a scheme into socket URLs; build them from the page or from one config value.
- Run the staging site on HTTPS too, so this fails before release.
- Send ping frames from the server so proxies do not close idle sockets.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix