Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io failed: WebSocket is closed before the connection is established

The Issue

My Twitter streaming app using Socket.io works okay when visiting the site with the IP address and port, but live streaming throws an error, when using the domain name to access the site. 🤔

Try the links below, and open up the developer tools, to see the error

Visiting site using the domain name (http://sentiment-sweep.com)

Hundreds of socket errors following this format:

WebSocket connection to 
'ws://sentiment-sweep.com/socket.io/?EIO=3&transport=websocket&sid=guBmeCqsOr22CTsWAAC0' 
failed: WebSocket is closed before the connection is established.

Visiting the site using IP and Port (http://XXX.XX.XXX.XX:3003)

Initially a few of the above socket errors, but then affter 10-15 seconds the app starts to work fine

Background

I made this Node app for a University project few years back. I keep it updated. It uses Socket.io and the Twitter API to stream live Tweets, calculate the sentiment and display results visually using D3. I use NGINX for port forwarding.

For reference, here is my git repo: https://github.com/Lissy93/twitter-sentiment-visualisation

Recently I made a small HTML/ CSS change, and updated a couple of dependencies, and that's when this strange socket.io issue started

What I've tried so far

  • Changing the port - no difference
  • I use io.connect(); with no params to connect
  • Including socket.io locally, rather than from CDN
  • Could it have something to do with my site being non-SSL
  • Locally it works totally fine, and faster and less of a delay than the API version

Has anyone seen an issue similar to this before? Any suggestions, comments or answers would be greatly appreciated - thanks very much in advance.

like image 414
Alicia Avatar asked Jan 03 '18 10:01

Alicia


People also ask

How do I fix WebSocket connection failed?

Solution 1Check that all the Bot Insight services are running. Check that your firewall settings are configured to accept incoming websocket data. Try to use a different web browser. Restart the Bot Insight Visualization and Bot Insight Scheduler services.

What causes a WebSocket to fail?

The most common cause of Websocket error is when you connect to DSS through a proxy. Websockets is a fairly recent protocol and many enterprise proxies do not support it. The websocket connection will not establish and you will see this message.

Can I reopen a closed WebSocket?

Once the original connection has been closed, you need to create a new websocket object with new event listeners: var SERVER = 'ws://localhost:8080' var ws = new WebSocket(SERVER) ws.


1 Answers

Found the solution, and it was in fact simple! Posting the answer here, to help others facing a similar issue, because I was totally stuck on this for 2 days!

My issue was caused by a dependency update.

Socket.io V 2.0.0 is not backward compatible.

According to the release notes:

The new (V 2.0.0) major release brings several performance improvements, but also snuck into the bottom of the release notes was this:

This release is not backward-compatible, due to:

  • A breaking change related to utf-8 encoding in engine.io-parser (socketio/engine.io-parser#81)
  • an update to make the socket id on the client match the id on the server-side (socketio/socket.io-client#1058)

Please note that if you are using a self-signed certificate, rejectUnauthorized now defaults to true (socketio/engine.io-client#558).

Here's the full V2 release notes: https://github.com/socketio/socket.io/releases/tag/2.0.0

The solution is to use HTTPS

Make your app https, or sign your own requests, as per documentation. As a quick fix though, I just downgraded back to socket.io V 1.7.0 and it works perfectly 😏

like image 135
Alicia Avatar answered Sep 24 '22 00:09

Alicia