title: Unexpected response code: 502 error when using socket.io with AppFog
body: <h1>The error</h1>
  
  <p>When the user opens <a href="http://sync.aws.af.cm/" rel="nofollow noreferrer">http://sync.aws.af.cm/</a>, the Google Chrome JavaScript Console shows (within a few seconds):</p>
  
  <pre class="lang-none prettyprint-override"><code>Unexpected response code: 502 error
  </code></pre>
  
  <p>The app should remember the contents of the text area even after the user refreshes the page. The app works locally, but not when I upload it to AppFog, and I suspect the problem is the 502 error.</p>
  
  <p>Do you know what might cause this error in this case?</p>
  
  <h1>What I've tried</h1>
  
  <p>I tried changing the <code>script src=&lt;path_to_socket_io&gt;</code> path in my client from <code>/socket.io/socket.io.js</code> to <code>node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js</code> as described <a href="https://stackoverflow.com/questions/9446708/getting-the-basic-socket-io-sample-to-work">here</a>, but it did not help.</p>
  
  <p>I also tried serving the app at Heroku instead of AppFog, but in that case I kept running into the issue described <a href="https://stackoverflow.com/questions/11674550/socket-io-node-js-on-heroku">here</a> even though I specified socket.io version 0.9.6.</p>
  
  <p>I've also looked at the following questions, but the comments so far don't seem to help me.</p>
  
  <p><a href="https://stackoverflow.com/questions/9908297/socket-io-creating-502-bad-gateway-on-connect-websocket-invalid">socket.io creating 502 Bad Gateway on connect / websocket invalid</a></p>
  
  <p><a href="https://stackoverflow.com/questions/12039690/socket-io-node-js-websocket-connection-invalid-port-8081">Socket.IO node.js websocket connection invalid port 8081</a></p>
  
  <p><a href="https://stackoverflow.com/questions/12016775/socket-io-with-node-js-not-working-as-expected">socket.io with node.js not working as expected</a></p>
  
  <h1>Relevant server code</h1>
  
  <p>Here is the relevant server code:</p>
  
  <pre class="lang-js prettyprint-override"><code>var server = http.createServer(function (req, res) {
    ...
  });
  
  var io = require('socket.io').listen(server);
  
  // Define events that can be triggered by the client
  io.sockets.on('connection', function (socket) {
    socket.on('setServerVariable', function (value) {
      ...
    });
    ...
  });
  
  server.listen(process.env.VCAPP_APP_PORT || 3000);
  </code></pre>
  
  <h1>Relevant client code</h1>
  
  <pre class="lang-html prettyprint-override"><code>&lt;script src="/socket.io/socket.io.js"&gt;&lt;/script&gt;
  
  &lt;script&gt;
    window.onload = function () {
      ...
      var socket = io.connect(window.location.hostname);
      socket.emit('getServerVariable');
      ...
    };
  ...
  </code></pre>
  
  <h1>Server log</h1>
  
  <pre><code>$ af logs sync
  
  ====&gt; /logs/stdout.log &lt;====
  
  info: socket.io started
  debug: served static content /socket.io.js
  debug: client authorized
  info: handshake authorized veNnUVtoDTIz-oudG7Pa
  debug: setting request GET /socket.io/1/websocket/veNnUVtoDTIz-oudG7Pa
  debug: set heartbeat interval for client veNnUVtoDTIz-oudG7Pa
  warn: websocket connection invalid
  info: transport end (undefined)
  debug: set close timeout for client veNnUVtoDTIz-oudG7Pa
  debug: cleared close timeout for client veNnUVtoDTIz-oudG7Pa
  debug: cleared heartbeat interval for client veNnUVtoDTIz-oudG7Pa
  debug: served static content /socket.io.js
  debug: client authorized
  info: handshake authorized 6AKyhROUNjqujk_uG7Pb
  debug: setting request GET /socket.io/1/websocket/6AKyhROUNjqujk_uG7Pb
  debug: set heartbeat interval for client 6AKyhROUNjqujk_uG7Pb
  warn: websocket connection invalid
  info: transport end (undefined)
  debug: set close timeout for client 6AKyhROUNjqujk_uG7Pb
  debug: cleared close timeout for client 6AKyhROUNjqujk_uG7Pb
  debug: cleared heartbeat interval for client 6AKyhROUNjqujk_uG7Pb
  debug: setting request GET /socket.io/1/xhr-polling/6AKyhROUNjqujk_uG7Pb?t=1348504130465
  debug: setting poll timeout
  debug: client authorized for 
  debug: clearing poll timeout
  debug: xhr-polling writing 1::
  debug: set close timeout for client 6AKyhROUNjqujk_uG7Pb
  debug: setting request GET /socket.io/1/xhr-polling/6AKyhROUNjqujk_uG7Pb?t=1348504130724
  debug: setting poll timeout
  debug: discarding transport
  debug: cleared close timeout for client 6AKyhROUNjqujk_uG7Pb
  debug: xhr-polling received data packet 5:::{"name":"getServerVariable"}
  debug: clearing poll timeout
  debug: xhr-polling writing 5:::{"name":"printLog"}
  debug: set close timeout for client 6AKyhROUNjqujk_uG7Pb
  server.js: getServerVariable: 
  debug: setting request GET /socket.io/1/xhr-polling/6AKyhROUNjqujk_uG7Pb?t=1348504130980
  debug: setting poll timeout
  debug: clearing poll timeout
  debug: xhr-polling writing ���23���5:::{"name":"printLog"}���39���5:::{"name":"setItemValue","args":[""]}���23���5:::{"name":"printLog"}
  debug: set close timeout for client 6AKyhROUNjqujk_uG7Pb
  debug: discarding transport
  debug: cleared close timeout for client 6AKyhROUNjqujk_uG7Pb
  debug: setting request GET /socket.io/1/xhr-polling/6AKyhROUNjqujk_uG7Pb?t=1348504131261
  debug: setting poll timeout
  debug: discarding transport
  debug: cleared close timeout for client 6AKyhROUNjqujk_uG7Pb
  debug: clearing poll timeout
  debug: xhr-polling writing 8::
  debug: set close timeout for client 6AKyhROUNjqujk_uG7Pb
  debug: xhr-polling closed due to exceeded duration
  debug: setting request GET /socket.io/1/xhr-polling/6AKyhROUNjqujk_uG7Pb?t=1348504151659
  debug: setting poll timeout
  debug: discarding transport
  debug: cleared close timeout for client 6AKyhROUNjqujk_uG7Pb
  </code></pre>