{"id":6255,"date":"2014-04-14T12:25:43","date_gmt":"2014-04-14T12:25:43","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/14\/unexpected-response-code-502-error-when-using-socket-io-with-appfog-collection-of-common-programming-errors-2\/"},"modified":"2014-04-14T12:25:43","modified_gmt":"2014-04-14T12:25:43","slug":"unexpected-response-code-502-error-when-using-socket-io-with-appfog-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/14\/unexpected-response-code-502-error-when-using-socket-io-with-appfog-collection-of-common-programming-errors-2\/","title":{"rendered":"Unexpected response code: 502 error when using socket.io with AppFog-Collection of common programming errors"},"content":{"rendered":"<p>When the user opens http:\/\/sync.aws.af.cm\/, the Google Chrome JavaScript Console shows (within a few seconds):<\/p>\n<pre><code>Unexpected response code: 502 error\n<\/code><\/pre>\n<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>\n<p>Do you know what might cause this error in this case?<\/p>\n<h2>What I&#8217;ve tried<\/h2>\n<p>I tried changing the <code>script src=<\/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 here, but it did not help.<\/p>\n<p>I also tried serving the app at Heroku instead of AppFog, but in that case I kept running into the issue described here even though I specified socket.io version 0.9.6.<\/p>\n<p>I&#8217;ve also looked at the following questions, but the comments so far don&#8217;t seem to help me.<\/p>\n<p>socket.io creating 502 Bad Gateway on connect \/ websocket invalid<\/p>\n<p>Socket.IO node.js websocket connection invalid port 8081<\/p>\n<p>socket.io with node.js not working as expected<\/p>\n<h2>Relevant server code<\/h2>\n<p>Here is the relevant server code:<\/p>\n<pre><code>var server = http.createServer(function (req, res) {\n  ...\n});\n\nvar io = require('socket.io').listen(server);\n\n\/\/ Define events that can be triggered by the client\nio.sockets.on('connection', function (socket) {\n  socket.on('setServerVariable', function (value) {\n    ...\n  });\n  ...\n});\n\nserver.listen(process.env.VCAPP_APP_PORT || 3000);\n<\/code><\/pre>\n<h2>Relevant client code<\/h2>\n<pre><code>\n\n\n  window.onload = function () {\n    ...\n    var socket = io.connect(window.location.hostname);\n    socket.emit('getServerVariable');\n    ...\n  };\n...\n<\/code><\/pre>\n<pre><code>$ af logs sync\n\n====&gt; \/logs\/stdout.log<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When the user opens http:\/\/sync.aws.af.cm\/, the Google Chrome JavaScript Console shows (within a few seconds): Unexpected response code: 502 error 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6255","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6255"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6255\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}