title: Haproxy node socket.io - WebSocket handshake error - Unexpected response code: 502
body: <p>I have configured my socket.io server behind haproxy using following configuration - </p>
  
  <pre><code>frontend http-in
    mode http
    bind *:9090
  
    acl is_websocket path_beg /prodSocket
    use_backend socket if is_websocket
  
  backend socket
    timeout server 180s
    server 127.0.0.1 127.0.0.1:9091
    server 148.251.76.84 148.251.76.84:9000
  </code></pre>
  
  <p>My Node socket.io server code - </p>
  
  <pre><code>var Server = require('socket io');
  var io = new Server({'path': '/prodSocket'});
  
  io.listen(9091);
  console.log('started on  ' + 9091);
  
  io.on('connection', function(socket) {
    console.log(socket.id);
    socket.emit('news', { hello: 'connected' });
    socket.on('client', function (data) {
      socket.emit('news', { hello: 'world' });
      console.log(data);
    });
    socket.on('disconnected', function(socket) {
       console.log(socket.id + " disconnected");
    });
  });
  </code></pre>
  
  <p>My client code - </p>
  
  <pre><code>var socket = io('http://127.0.0.1:9090', {
    'path': '/prodSocket',
    'force new connection': false,
    'reconnection delay': 500,
    'max reconnection attempts': 10,
  });
  socket.emit('client', { my: 'data' });
  socket.on('news', function (data) {
   console.log(data);
  });
  </code></pre>
  
  <p>Errors - </p>
  
  <pre><code>WebSocket connection to 'ws://127.0.0.1:9090/prodSocket/?EIO=3&amp;transport=websocket&amp;sid=cf1I2nsJrsksPpzfAAAA' failed: Error during WebSocket handshake: Unexpected response code: 502
  </code></pre>
  
  <p>I am not really understanding whats wrong here. Although this error, My server and clients are able to exchange the messages. But have to resolve this errror.</p>
  
  <p>Any help would really be appreciated.</p>
  
  <p>Haproxy Logs - </p>
  
  <pre><code>Jul 27 17:17:28 localhost haproxy[1332]: 127.0.0.1:42436 [27/Jul/2016:17:17:28.455] http-in socket/127.0.0.1 0/0/0/-1/15 502 205 - - SH-- 1/1/1/0/0 0/0 "GET /prodSocket/?EIO=3&amp;transport=websocket&amp;sid=kZXRU_7vBf09aoE7AAAc HTTP/1.1"
  Jul 27 17:17:28 localhost haproxy[1332]: 127.0.0.1:42436 [27/Jul/2016:17:17:28.455] http-in socket/127.0.0.1 0/0/0/-1/15 502 205 - - SH-- 1/1/1/0/0 0/0 "GET /prodSocket/?EIO=3&amp;transport=websocket&amp;sid=kZXRU_7vBf09aoE7AAAc HTTP/1.1"
  </code></pre>