Is it possible to proxy websocket connections within the webpack dev server? I know how to proxy regular HTTP requests to another backend but it's not working for websockets, presumably because the target in the proxy configuration starts with http://...
-
OP, could you provide some context on why you need to do this? Does `devServer.public` not suffice (seems like it would send *all* your WDS requests to that public domain/host)? – mecampbellsoup Mar 02 '21 at 15:17
3 Answers
Version 1.15.0 of the webpack-dev-server supports proxying websocket connections. Add the following to your configuration:
devServer: {
proxy: {
'/api': {
target: 'ws://[address]:[port]',
ws: true
},
},
}

- 1,552
- 1
- 15
- 15
-
6They should put this bolded into the documentation. I wasted a few hours right now to figure out why socket.io is using polling instead of web sockets. – p7adams Jun 18 '19 at 11:24
-
got it working. https://stackoverflow.com/questions/42611926/error-using-socket-io-along-with-webpack-dev-server/64725627#64725627 – MartianMartian Nov 07 '20 at 07:58
Webpack dev server does not support proxying ws connections yet.
Until then, you can implement proxying manually, by adding additional http-proxy
to webpack server:
Add new dependency to
package.json
:"http-proxy": "^1.11.2"
Proxy websocket connections manually by listening to
upgrade
events// existing webpack-dev-server setup // ... var server = new WebpackDevServer(...); proxy = require('http-proxy').createProxyServer(); server.listeningApp.on('upgrade', function(req, socket) { if (req.url.match('/socket_url_to_match')) { console.log('proxying ws', req.url); proxy.ws(req, socket, {'target': 'ws://localhost:4000/'}); } }); //start listening server.listen(...)
NOTE (after using this for some time)
There is an issue with proxying websockets as socket.io
is used by WebpackDevServer to notify browser of code changes. socket.io
may conflict with proxying websockets; in my case, connections were being dropped before handshake was returned from my server unless it responded very quickly.
At that point, I just ditched WebpackDevServer and used custom implementation based on react-hot-boilerplate

- 1,273
- 12
- 18
@Mr. Spice's answer is correct. But it can be further simplified, check http-proxy-middleware, it can be set as following, i.e. just add ws: true
and keep other settings as usual.
// proxy middleware options
var options = {
target: 'http://www.example.org', // target host
changeOrigin: true, // needed for virtual hosted sites
ws: true, // proxy websockets
...

- 10,295
- 11
- 80
- 129
-
1Sorry @Quilang, but I find your example insufficient to answer the question. The OP was looking for something in the context webpack-dev-server. I can't understand from your answer how that relates to a node.js middleware for proxying. What do the devServer: options look like? – Greg Veres Apr 18 '19 at 11:41
-
webpack-dev-server uses http-proxy-middleware, so basically proxy settings of the webpack-dev-server is actually http-proxy-middleware options. They're same. I didn't get what he means by 'further simplified' though – rosencreuz May 29 '19 at 14:05
-
1@rosencreuz - I guess the simplification is that you don't have to specify both target: 'ws://' and ws: true - one of those is enough, according to the docs. This is useful when you want to proxy both http and ws, so you can just leave target: 'http://' – glexey Sep 03 '19 at 23:16