2
votes

I have a sails.js / socket.io chat application with websocket and polling transports enabled. In my organisation, we have a proxy/firewall which blocks connection to ws:// protocol.

When I run the application in this environment, I can see the sails application trying to reconnect to websocket with output like this:

WebSocket connection to 'ws://.../socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript&EIO=3&transport=websocket' failed: WebSocket is closed before the connection is established. sails.io.js:143
Socket is trying to reconnect to Sails... -|>- (attempt #11)

How do I make the application fallback to polling transport after 3rd attempt?

UPDATE: I've just found this on the socket.io blog:

Socket.IO never assumes that WebSocket will just work, because in practice there’s a good chance that it won’t. Instead, it establishes a connection with XHR or JSONP right away, and then attempts to upgrade the connection to WebSocket.

Also, the described conditions only happen once in a time, most of the times it works ok with polling. Looks like the sails app sometime skips the XHR connection and tries websockets right away.

2

2 Answers

1
votes

I have the same problem when i make deploy on Heroku, apparently he don't work well with polling.

I solved this by forcing transports for websocket.

In config/sockets:

transports: ["websocket"]

Add this line immediately after the sails.io.js. Because this file is in the pipeline i created a new file to put the script:

io.sails.transports=['websocket'];

More details in this link:

http://sailsjs.org/documentation/concepts/deployment/scaling

0
votes

Turns out I need to have sessions enabled to make polling work correctly, so it may by the cause of my problems.