title: Heroku and Socket.io, 400 Bad requests and many disconnects
body: <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>https://appname.herokuapp.com/socket.io/?api_key=xxx&amp;EIO=3&amp;transport=polling&amp;t=1424782938420-1&amp;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&amp;transport=websocket&amp;sid=hcTvjdxYePd4kNS5AAAe' failed: WebSocket is closed before the connection is established.
  </code></pre>
  
  <p><strong>So in my server.js</strong></p>
  
  <pre><code>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 &lt; 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);
    });
  }
  </code></pre>
  
  <p><strong>In my client.html</strong></p>
  
  <pre><code>&lt;script src="https://appname.herokuapp.com/socket.io/socket.io.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  var socket = io.connect("https://appname.herokuapp.com", { query: "api_key=xxx" });
  &lt;/script&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>I am not trying to run this on multiple instances, this is just running with <code>heroku ps:scale web=1</code></p>
  
  <p>Not even sure websockets are working, it seems to be using polling and then failing on that too.</p>
  
  <p><strong>So my questions:</strong> Why am I getting all these disconnects and 400 bad requests when its working fine locally? How can I fix?</p>