Node.js Socket.io on Heroku drops H13

Deploying my app to Heroku breaks the websockets protocol.
It runs on localhost, not Heroku. In the browser I get:

WebSocket connection with 'wss: // node -omi.herokuapp.com/socket.io/?EIO=2&transport=websocket&sid=*' failed: WebSocket handshake error: unexpected response code: 503

On the server side, I get the logs:

2014-08-12T15: 05: 24.761611 + 00: 00 heroku [router]: at = error code = H13 desc = "Connection closed without response" method = GET path = "/socket.io/?EIO=2&, transport = websocket & sid = **** "host = node -omi.herokuapp.com request_id = * fwd =" * "dyno = web.1 connect = 3ms service = 3ms status = 503 bytes = 864

This is part of my server script:

var express = require('express'),
    http = require('http'),
    socket_io = require('socket.io');
var app = express(),
    server = http.Server(app),
    io = new socket_io(server).of('/test');
io.on('connection', this.connection.bind(this));
app.get('/client.html', function(req, res, next) { ... });
app.use('/', express.static(__dirname + '/public', {'index': ['index.html']}));
server.listen(process.env.PORT || 5000);

      

What's wrong with my code? Has anyone succeeded in making a socket.io server with Express 4.8.3 and Socket.io 1.0.6? The grid works with polling, but I really need the websockets to work. Thanks for answers.

+3


source to share


1 answer


Websocket support on Heroku has since gone from beta to official, so the fix might have been easy at the time of posting.But labs:enable websockets

you don't need to do this anymore, you get websites out of the box.

Attention!

But as a warning to others reading this question, using socket.io on Heroku can be problematic due to the controversy around sticky sessions

Heroku clearly stands out against them , but ( aside from the rather cheesy workaround described below) socket.io requires them:

https://github.com/Automattic/engine.io/issues/261

^ tl; dr, if this very long thread - before 1.0, socket.io was working with non-sticky services like Heroku, you just had to use a redis adapter to manage the split state through your dynos. This was removed in 1.0 and the socket.io team is wary of bringing it back due to the cost of maintaining the code. A github issue requiring it to be returned was closed with a message that they would gladly consider it as soon as someone wants to do it badly enough to make a PR.

Workaround!



There is a workaround which is to restrict socket.io usage to websocket porting only, which does not require sticky sessions. For example:

Server:

io.set('transports', ['websocket']);

      

Client

var socket = io.connect(host, {
    transports: ['websocket']
});

      

However, this removes the great benefit that socket.io 1.0 brings using engine.io.

There are other websocket frameworks like Faye that work very well on Heroku.

+2


source







All Articles