{"action":"created","issue":{"url":"https://api.github.com/repos/BrowserSync/browser-sync/issues/780","labels_url":"https://api.github.com/repos/BrowserSync/browser-sync/issues/780/labels{/name}","comments_url":"https://api.github.com/repos/BrowserSync/browser-sync/issues/780/comments","events_url":"https://api.github.com/repos/BrowserSync/browser-sync/issues/780/events","html_url":"https://github.com/BrowserSync/browser-sync/issues/780","id":102354887,"number":780,"title":"websocket problems 502","user":{"login":"jmls","id":929523,"avatar_url":"https://avatars.githubusercontent.com/u/929523?v=3","gravatar_id":"","url":"https://api.github.com/users/jmls","html_url":"https://github.com/jmls","followers_url":"https://api.github.com/users/jmls/followers","following_url":"https://api.github.com/users/jmls/following{/other_user}","gists_url":"https://api.github.com/users/jmls/gists{/gist_id}","starred_url":"https://api.github.com/users/jmls/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmls/subscriptions","organizations_url":"https://api.github.com/users/jmls/orgs","repos_url":"https://api.github.com/users/jmls/repos","events_url":"https://api.github.com/users/jmls/events{/privacy}","received_events_url":"https://api.github.com/users/jmls/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":14,"created_at":"2015-08-21T11:16:45Z","updated_at":"2015-11-11T20:15:58Z","closed_at":null,"body":"I've logged an issue here (https://github.com/chimurai/http-proxy-middleware/issues/15) where I am having a problem with browser-sync, http-proxy-middleware and websockets\r\n\r\nIn a nutshell, I have this code\r\n\r\n```javascript\r\nvar server = {\r\n    baseDir: baseDir,\r\n    routes: routes\r\n  };\r\n\r\n  var proxies = [];\r\n\r\n  proxies.push(proxyMiddleware('/socket.io', { \r\n        target: 'http://localhost:5000/' , ws: true\r\n  }));\r\n\r\n  server.middleware = proxies;\r\n\r\n  browserSync.instance = browserSync.init({\r\n    startPath: '/',\r\n    server: server,\r\n    browser: browser\r\n  });\r\n```\r\n\r\nbut get these errors : \r\n\r\n```html\r\nwoot! connection\r\n[HPM] Upgrading to WebSocket\r\n[HPM] Client disconnected\r\n\r\nand the client console has\r\n\r\nWebSocket connection to 'ws://mysite.io/socket.io/?EIO=3&transport=websocket&sid=5_BeDfck0LFYcYxPAAAA' failed: Error during WebSocket handshake: Unexpected response code: 502\r\n```\r\n\r\nand obviously, I can't get my websocket connected.\r\n\r\nAm I missing something or is it a bug ?\r\n\r\nthanks\r\n"},"comment":{"url":"https://api.github.com/repos/BrowserSync/browser-sync/issues/comments/155898360","html_url":"https://github.com/BrowserSync/browser-sync/issues/780#issuecomment-155898360","issue_url":"https://api.github.com/repos/BrowserSync/browser-sync/issues/780","id":155898360,"user":{"login":"shakyShane","id":1643522,"avatar_url":"https://avatars.githubusercontent.com/u/1643522?v=3","gravatar_id":"","url":"https://api.github.com/users/shakyShane","html_url":"https://github.com/shakyShane","followers_url":"https://api.github.com/users/shakyShane/followers","following_url":"https://api.github.com/users/shakyShane/following{/other_user}","gists_url":"https://api.github.com/users/shakyShane/gists{/gist_id}","starred_url":"https://api.github.com/users/shakyShane/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shakyShane/subscriptions","organizations_url":"https://api.github.com/users/shakyShane/orgs","repos_url":"https://api.github.com/users/shakyShane/repos","events_url":"https://api.github.com/users/shakyShane/events{/privacy}","received_events_url":"https://api.github.com/users/shakyShane/received_events","type":"User","site_admin":false},"created_at":"2015-11-11T20:15:58Z","updated_at":"2015-11-11T20:15:58Z","body":"@redi-madhava the problem is that Browsersync is intercepting the upgrade server event.\r\n\r\nso this works\r\n\r\n```js\r\nvar WebSocketServer = require('ws').Server;\r\nvar wss = new WebSocketServer({ port: 8000 });\r\n\r\nwss.on('connection', function connection(ws) {\r\n    ws.on('message', function incoming(message) {\r\n        console.log('received: %s', message);\r\n    });\r\n    ws.send('something');\r\n});\r\n\r\n/**\r\n * Setup proxy\r\n * @type {*|exports|module.exports}\r\n */\r\nvar httpProxy = require('http-proxy');\r\nvar http = require('http');\r\nvar proxy = new httpProxy.createProxyServer({\r\n    target: {\r\n        host: 'localhost',\r\n        port: 8000\r\n    }\r\n});\r\n\r\n/**\r\n * Setup static file server + proxy websockets\r\n */\r\nvar connect = require('connect')();\r\nconnect.use(require('serve-static')('app'));\r\nvar http = require('http');\r\nvar server = http.createServer(connect);\r\nserver.on('upgrade', function (req, socket, head) {\r\n    proxy.ws(req, socket, head);\r\n});\r\nserver.listen(3000);\r\n```\r\n\r\nBut swap the last chunk for \r\n\r\n```js\r\nvar browserSync = require('browser-sync');\r\nbrowserSync({\r\n    server: \"app\",\r\n    files: [\"app/*.html\", \"app/css/*.css\"]\r\n}, function (err, bs) {\r\n    bs.server.on('upgrade', function (req, socket, head) {\r\n        proxy.ws(req, socket, head);\r\n    });\r\n});\r\n```\r\n\r\nand it all just breaks!\r\n\r\n"}}