Skip to content

Commit 5b27ff8

Browse files
dceddiarandycoulman
authored andcommitted
Enable proxying of websockets (facebook#1090)
Added `ws: true` to the httpProxyMiddleware options, and also listen for the "upgrade" event so that websockets can be proxied immediately, rather than waiting for an initial HTTP request.
1 parent 2505fa4 commit 5b27ff8

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

packages/react-scripts/scripts/start.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,25 @@ function addMiddleware(devServer) {
183183
// - /sockjs-node/* (WebpackDevServer uses this for hot reloading)
184184
// Tip: use https://jex.im/regulex/ to visualize the regex
185185
var mayProxy = /^(?!\/(index\.html$|.*\.hot-update\.json$|sockjs-node\/)).*$/;
186-
devServer.use(mayProxy,
187-
// Pass the scope regex both to Express and to the middleware for proxying
188-
// of both HTTP and WebSockets to work without false positives.
189-
httpProxyMiddleware(pathname => mayProxy.test(pathname), {
190-
target: proxy,
191-
logLevel: 'silent',
192-
onError: onProxyError(proxy),
193-
secure: false,
194-
changeOrigin: true
195-
})
196-
);
186+
187+
// Pass the scope regex both to Express and to the middleware for proxying
188+
// of both HTTP and WebSockets to work without false positives.
189+
var hpm = httpProxyMiddleware(pathname => mayProxy.test(pathname), {
190+
target: proxy,
191+
logLevel: 'silent',
192+
onError: onProxyError(proxy),
193+
secure: false,
194+
changeOrigin: true,
195+
ws: true
196+
});
197+
devServer.use(mayProxy, hpm);
198+
199+
// Listen for the websocket 'upgrade' event and upgrade the connection.
200+
// If this is not done, httpProxyMiddleware will not try to upgrade until
201+
// an initial plain HTTP request is made.
202+
devServer.listeningApp.on('upgrade', hpm.upgrade);
197203
}
204+
198205
// Finally, by now we have certainly resolved the URL.
199206
// It may be /index.html, so let the dev server try serving it again.
200207
devServer.use(devServer.middleware);

0 commit comments

Comments
 (0)