Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

https://appname.herokuapp.com/socket.io/?api_key=xxx&EIO=3&transport=polling&t=1424782938420-1&sid=hcTvjdxYePd4kNS5AAAe 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="https://appname.herokuapp.com/socket.io/socket.io.js"></script>
<script>
var socket = io.connect("https://appname.herokuapp.com", { 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?

like image 255
Horse Avatar asked Feb 24 '15 13:02

Horse


1 Answers

I had the same issue. Solution was to change hosting. I didn't want to move from heroku as it's really convenient. But after I wasted lots of time of searching a fix, I tried to deploy application to OpenShift. Also I added modified starting of server with providing explicit port and host(doesn't work without it!)

var port = process.env.OPENSHIFT_NODEJS_PORT || 8080,
    ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
...
sockets.setupListeners(server);
server.listen(port, ip_address);

More detailed info you can find here enter link description here under clause 4.

I believe you'll resolve your headache.

like image 179
Mihail Zheludev Avatar answered Nov 15 '22 00:11

Mihail Zheludev