title: Using node http-proxy to proxy websocket connections
body: <p>I have an application that uses websockets via socket.io. For my application I would like to use a separate HTTP server for serving the static content and JavaScript for my application. Therefore, I need to put a proxy in place.</p>
  
  <p>I am using <a href="https://github.com/nodejitsu/node-http-proxy" rel="noreferrer">node-http-proxy</a>. As a starting point I have my websockets app running on port 8081. I am using the following code to re-direct socket.io communications to this standalone server, while using express to serve the static content:</p>
  
  <pre><code>var http = require('http'),
      httpProxy = require('http-proxy'),
      express = require('express');
  
  // create a server
  var app = express();
  var proxy = httpProxy.createProxyServer({ ws: true });
  
  // proxy HTTP GET / POST
  app.get('/socket.io/*', function(req, res) {
    console.log("proxying GET request", req.url);
    proxy.web(req, res, { target: 'http://localhost:8081'});
  });
  app.post('/socket.io/*', function(req, res) {
    console.log("proxying POST request", req.url);
    proxy.web(req, res, { target: 'http://localhost:8081'});
  });
  
  // Proxy websockets
  app.on('upgrade', function (req, socket, head) {
    console.log("proxying upgrade request", req.url);
    proxy.ws(req, socket, head);
  });
  
  // serve static content
  app.use('/', express.static(__dirname + "/public"));
  
  app.listen(8080);
  </code></pre>
  
  <p>The above application works just fine, however, I can see that socket.io is no longer using websockets, it is instead falling back to XHR polling.</p>
  
  <p>I can confirm that by looking at the logs from the proxy code:</p>
  
  <pre><code>proxying GET request /socket.io/1/?t=1391781619101
  proxying GET request /socket.io/1/xhr-polling/f-VVzPcV-7_IKJJtl6VN?t=13917816294
  proxying POST request /socket.io/1/xhr-polling/f-VVzPcV-7_IKJJtl6VN?t=1391781629
  proxying GET request /socket.io/1/xhr-polling/f-VVzPcV-7_IKJJtl6VN?t=13917816294
  proxying GET request /socket.io/1/xhr-polling/f-VVzPcV-7_IKJJtl6VN?t=13917816294
  </code></pre>
  
  <p>Does anyone know how to proxy the web sockets communication? All the examples from <code>node-http-proxy</code> assume that you want to proxy all traffic, rather than proxying some and serving others.</p>