Why Socket.io connection is slow in Safari and Chrome

I am a real newbie with Node.js and Socket.io - so please bear with me if this is a stupid question.

I installed a very simple dummy on Heroku to check out Socket.io. All you can do is click on the button and all connected browsers will see the message accordingly. This worked very well when the server was running locally. Now that I am on Heroku, I see connectivity issues in Safari and Chrome. The initial connection is delayed for 10 seconds, and I get an error 503 when I first call io.connect('http://myapp.heroku.com/') .

Everything works well with fairly direct communication in Firefox and Internet Explorer.

The application can be seen here: http://sprain.ch/socketio/

Any ideas on what might cause this problem and how to fix it?

+5
source share
3 answers

You can reduce the timeout used for the first connection to the web socket using the "connection timeout" parameter (by default it is 10 seconds).

You can reduce the wait time to 1 second using:

 io.connect('http://myapp.heroku.com/',{'connect timeout': 1000}); 
+7
source

This is because they do not support websockets. So socket.io occasionally tries to use websockets before resorting to using the XHR poll, which is not the same, but might be ok for your purposes (?). As the link in the Aashay post suggests, add this code to the server.js or app.js file:

 io.configure(function () { io.set("transports", ["xhr-polling"]); io.set("polling duration", 10); }); 

But this is NOT the same as actually using websockets ..! For example, on OpenShift.com, you can connect through port 8000 to your application, and then actually use web ports, and the default port does not support it due to problems with Apache.

+4
source

A few things:

  • I would suggest you use the following settings: http://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku

  • Try simply "io.connect ()" instead of manually specifying the URL. SocketIO has some fairly robust built-in discovery mechanisms to communicate with the local host no matter what the host is.

  • Nothing happens slowly in Heroku + Socket.IO (speaking from experience), so it makes me believe that something else is happening in your code. If you could share a little more, it would be easier to diagnose.

Also FWIW that the page you linked just contains text that says β€œklick me!”. and there is no button.

+3
source