Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Socket.io connection slow on Safari and Chrome

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

I set up a very basic dummy on Heroku to test out Socket.io. All you can do is click a button and all connected browser see a message accordingly. This worked very well when the server was running locally. Now with it being on Heroku I see connection problems on Safari and Chrome. The initial connection is delayed for about 10 seconds, and I get a error 503 on the first call of io.connect('http://myapp.heroku.com/').

Things work well with a pretty much immediate connection in Firefox and Internet Explorer.

You can see the app in action here: http://sprain.ch/socketio/

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

like image 293
sprain Avatar asked Dec 12 '11 20:12

sprain


3 Answers

You can reduce the timeout used for the first websocket connection using the "connect timeout" parameter (which is 10 seconds by default).

You can try reducing the timeout to 1 second using:

io.connect('http://myapp.heroku.com/',{'connect timeout': 1000});
like image 138
leszek.hanusz Avatar answered Nov 14 '22 21:11

leszek.hanusz


It's because they don't support websockets. Therefore socket.io times out trying to use websockets before resorting to using XHR polling, which isn't the same, but might be OK for your purposes (?). As the link in Aashay's post suggests, add this code to your 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 thing as actually using websockets..! For example on OpenShift.com you can now connect over port 8000 to your app and then it actually uses websockets, while the default port annoyingly does not support it due to problems with Apache.

like image 27
CommaToast Avatar answered Nov 14 '22 21:11

CommaToast


A few things:

  • I'd suggest you use these settings: http://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku

  • Try simply "io.connect()" instead of attempting to specify the URL manually. SocketIO has some pretty solid built-in discovery mechanisms for talking to localhost regardless of what that host is.

  • There's nothing inherently slow about Heroku + Socket.IO (speaking from experience), so it leads me to believe there's something else going on in your code. If you could share a little more it might be easier to diagnose.

Also FWIW that page you linked simply has some text that says "klick me!" and no button.

like image 30
Aashay Desai Avatar answered Nov 14 '22 21:11

Aashay Desai