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?


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});

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.


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.

    链接地址: http://www.djcxy.com/p/63172.html

    上一篇: Socket.io脚本大小减小

    下一篇: 为什么Safari和Chrome上的Socket.io连接速度较慢