mardi 24 février 2015

Heroku and Socket.io, 400 Bad requests and many disconnects

So I have an app that works on socket.io and I am trying to get it working on Heroku but having serious problems getting it to run.


In the console log, and in the chrome console there are a bunch of disconnects every second or so. And in the chrome console, I keep getting 400 Bad Requests.



http://ift.tt/1Erd8mv Failed to load resource: the server responded with a status of 400 (Bad Request)
disconnect
WebSocket connection to 'wss://appname.herokuapp.com/socket.io/?api_key=xxx=3&transport=websocket&sid=hcTvjdxYePd4kNS5AAAe' failed: WebSocket is closed before the connection is established.


So in my server.js



var cluster = require('cluster');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var redis = require('redis');
var redisAdapter = require('socket.io-redis');

var port = process.env.PORT || 3800;

var workers = process.env.WORKERS || require('os').cpus().length;

var redisUrl = process.env.REDISTOGO_URL || 'redis://rediscloud:pw@url:14599';

var redisOptions = require('parse-redis-url')(redis).parse(redisUrl);
var pub = redis.createClient(redisOptions.port, redisOptions.host, {
detect_buffers: true,
auth_pass: redisOptions.password
});
var sub = redis.createClient(redisOptions.port, redisOptions.host, {
detect_buffers: true,
auth_pass: redisOptions.password
});

io.set('origins', '*:*');

io.adapter(redisAdapter({
pubClient: pub,
subClient: sub
}));

console.log('Redis adapter started with url: ' + redisUrl);

/* -------------------------------- */

//APP LOGIC HERE

/* -------------------------------- */

if (cluster.isMaster) {
console.log('start cluster with %s workers', workers - 1);
workers--;
for (var i = 0; i < workers; ++i) {
var worker = cluster.fork();
console.log('worker %s started.', worker.process.pid);
}

cluster.on('death', function(worker) {
console.log('worker %s died. restart...', worker.process.pid);
});
cluster.on('exit', function(worker) {
console.log('worker %s exited. restart...', worker.process.pid);
cluster.fork();
});
} else {
start();
}

function start() {
http.listen(port, function() {
console.log('listening on *:' + port);
});
}


In my client.html



<script src="http://ift.tt/1DjybWn"></script>
<script>
var socket = io.connect("http://ift.tt/1Erd8mx", { query: "api_key=xxx" });
</script>


I had read that sticky sessions can cause problems, but most of the information seems to be outdated, and apparently Heroku supports websockets nicely now.


I am not trying to run this on multiple instances, this is just running with heroku ps:scale web=1


Not even sure websockets are working, it seems to be using polling and then failing on that too.


So my questions: Why am I getting all these disconnects and 400 bad requests when its working fine locally? How can I fix?


Aucun commentaire:

Enregistrer un commentaire