Getting tornadio2 working on heroku
I spent a while the other day figuring out how to get websockets working on heroku, so I thought I’d write it up.
First, Heroku doesn’t actually support websockets, so you must use something like socket.io which can fallback to various long polling mechanisms.
Step 1, disable websocket support in socket.io
Without this, socket.io tries to connect first using websockets and it takes a while to timeout before switching to long polling.
// remove websocket for heroku
var options = {transports:["flashsocket", "htmlfile", "xhr-polling", "jsonp-polling"]};
var socket = io.connect('http://.../", options);
Step 2, configure tornadio to use xheaders
If you don’t tell tornadio to use xheaders it will think heroku is trying to hijack sessions and nothing will work. You will get 401 unauthorized messages back from tornado and the error from this statement in your logs:
# If IP address don't match - refuse connection
if handler.request.remote_ip != self.remote_ip:
logging.error('Attempted to attach to session %s (%s) from different IP (%s)' % (
self.session_id,
self.remote_ip,
handler.request.remote_ip
))
Enabling xheaders is a good idea when deploying to heroku in general and is not tornadio specific.
Add the xheaders option to the main SocketServer initialization, and everything is happy.
SocketServer(application,xheaders=True)