title: node.js: proxy websockets to other port
body: <p>I have written http proxy in node.js running on port 80. All I need is to redirect socket.io traffic to port 9090 and standard http traffic to Apache on 8080. This is my proxy code:</p>
  
  <pre><code>httpProxy = require('http-proxy');
  
  httpProxy.createServer(function (req, res, proxy) {
      if (req.url.match(/socket.io/)) {
          proxy.proxyRequest(req, res, {
              host: 'localhost',
              port: 9090
          });
      } else {
          proxy.proxyRequest(req, res, {
              host: 'localhost',
              port: 8080
          });
      }
  }).listen(80);
  </code></pre>
  
  <p>Everything works, but io.socket falls back to xhr-polling.</p>
  
  <pre><code>http://localhost/client.htm    - falls back to xhr-polling
  
  file:///C:/.../client9090.htm  - still uses websocket
  </code></pre>
  
  <p>socket.io app is running on port 9090, client.htm connects to 80 and client9090.htm connects directly to 9090.</p>
  
  <p>It looks like a node-http-proxy makes socket.io app to work in xhr-polling mode. 
  Client is Chrome v.25</p>
  
  <p>socket.io app code</p>
  
  <pre><code>var io = require('socket.io').listen(9090);
  
  io.on('connection', function (socket) {
  
          socket.on('hi!', function (data) {
              console.log(data);
              socket.emit('news');
          });
  
          socket.on('ahoj', function (data) {
              console.log(data);
          });
      });
  </code></pre>
  
  <p>client.htm code</p>
  
  <pre><code>&lt;script src="http://localhost/socket.io/socket.io.js"&gt;&lt;/script&gt;
  &lt;script&gt;
      var chat = io.connect('http://localhost')
  
      chat.on('connect', function () {
          chat.emit('hi!');
      });
  
      chat.on('news', function () {
          chat.emit('ahoj',{a:1,b:2});
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>client9090.htm is the same but localhost is replaced by localhost:9090</p>
  
  <p>As I said, everythig works well, only problem is, that node-http-proxy makes to fall back from websockets to xhr-polling. 
  Can anyone help?</p>