user318750
user318750

Reputation: 386

Browser closes WebSocket connection with error 1006 one second after it is established when I'm behind OpenVPN

I have small HTTP + WebSocket server hosted on Amazon VPS. Index.html has JS code to connect WebSocket server and exchange data with it. When I connect my server directly using public IP or domain name - everything works fine.

However I don't want this server to be public, so I configured OpenVPN to connect to this server privately.

Sometimes everything works as expected over OpenVPN and when I enter local (inside VPN) servers IP address in my browser (Chrome or Opera) it succesfully loads index.html, connects my WebSocket server and succesfully exchanges data via WebSocket connection. But sometimes (or some days) 1 second after Websocket connection is established it is closed by browser with error code 1006 and without any description. My script tries to reconnect WebSocket 1 second after this, but result is the same all the time.

I can't figure out why sometimes everything is working and sometimes I can't use WebSocket over OpenVPN for several hours.

Can somebody describe why error 1006 occures when using WebSocket over OpenVPN and how to eliminate it by coding or reconfiguring Chrome, Opera or OpenVPN?

Upvotes: 1

Views: 8669

Answers (1)

user318750
user318750

Reputation: 386

I discovered that problem only occurs when any side of WS connection sends large message.

I guess that if there is some middleware like VPN, firewall or proxy between browser and WebSocket server, then large WS message can exceed some internal packet size or limit of that middleware and it will interrupt the connection between browser and server during message transfer. This unexpected disconnect results to error 1006 in your browser.

If your clients experience unexpected disconnects with error 1006, try to minimize WebSocket message sizes of your API. If you need to send large data amounts then don't send it in one chunk. You better slice it and send multiple short messages.

Upvotes: 2

Related Questions