1

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?

user318750
  • 326
  • 3
  • 7
  • If the socket closed during first second, I can suppose that the issue with connection establishment. It can be, that something wrong during Upgrade request. This is the very first http requests, that happened before switching protocol to WebSocket. If this request sequence failed, browser may return 1006 error. You should probably examine this first requests in chrome developer tools. – SleepWalker Oct 03 '20 at 14:42
  • Thanks @SleepWalker, but this is not my case, because everything works fine when client connects server directly. The problem occures only when server is behind VPN. I discovered that reducing WebSocket message sizes eliminates the problem. – user318750 Oct 04 '20 at 17:18

1 Answers1

1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user318750
  • 326
  • 3
  • 7