-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting "proxy" in package.json Fails for WebSockets #5280
Comments
Did it work in 1.x? Or is this a regression? |
Current workaround:Create
|
In 1.x, it was possible to specify more advanced options, including Though I don't know what the handling by the simple |
OK, sounds like this isn't a regression but a proposal to auto-detect and proxy websockets in the simple proxy mode. |
At minimum, reality is out of sync with the documentation. Though I would recommend making WebSocket proxying work by default. |
It was supposed to work in 1.x too. |
I reproduce with issue.
|
I can confirm that the following setup worked in 1.x:
but this no longer works in 2.1.1's I get the following error message upon That said, I will be happy to test any fix you throw at my direction. |
Hey, |
@mxschmitt, thank you for your email. So I've hacked the following fix and it works for my setup: |
@ivosh You don't have to use |
@mxschmitt, you are right, thank you for pointing this out. So in my case, even when all communication happens over websockets, specifying |
+1'ing the change @mxschmitt is proposing, this does fix the issue for me. For background, the problem locally is that firefox is sending 'text/html' in 'Accept' when opening the websocket connection, which causes the default proxy heuristic to -not- proxy the request:
I agree the presence of the 'upgrade' header should force proxying just like a non-GET request, unless there's a case I'm missing. |
Yey, the stale bot has closed my PR: #5841.... |
I've been wrestling with this issue for a while now, and the workarounds are very annoying, especially when you have cookies involved. Is there any reason why #5841 isn't being merged? It looks like one of the builds failed, but it seems to be an issue completely unrelated to the code change in that PR. @mxschmitt maybe if you add a comment to the code in your PR, the CI will rerun and hopefully there won't be any build issues this time? |
@hamidnoei This workaround has already been discussed. |
@avdeev Alexey, have you found a solution for the issue you described in this comment: #5280 (comment) ? I have got the same. |
Workaround: Use Firefox, for some reason this feature is not broken on Firefox. I typically do my local development on Firefox, and websocket proxying works there out of the box, and I was surprised to see that this was broken on Chrome, wasted alot of hours on this bug |
The docs for proxy linked to in the original description have moved here. |
Verified this is broken in Chrome 77.0.3865.75 (Official Build) (64-bit) Agree with @kent-h - preferred behavior would be "work out of the box" Burned hours thinking it was my code preventing the socket connection. I'd give big nerd creds to anyone involved in fixing it ! |
can u tell me how can i use setupProxy.js and i removed proxy from packege.json so how can i redireect my api to proxy |
I can also confirm this is broken in chrome but working in firefox. This is probably unrelated, but I have At the very least, the docs should be updated to indicate that this is currently broken for websockets in chrome. It's a little misleading to say that it works websockets when that's only true for certain browsers. |
I also have the same issue, proxy not working for Chrome but is for Firefox. |
+1 |
any news on the issue? |
+1 |
The issue for me is that the websocket request never reaches the proxy, e.g. module.exports = app => {
app.use((req, res, next) => {
console.log(req.url)
next()
})
} I see HTTP requests there but none on the localhost:{port} for The only solution I can think of atm is to start another local server in setupProxy.js just to proxy websocket requests on another port since they aren't passed through here? |
According to the doc, this works for me: app.use(
createProxyMiddleware(
"/subscriptions",
{
target: 'http://localhost:4000',
ws: true
})
); Note that, the |
In |
My solution was to use both techniques:
const createProxyMiddleware = require("http-proxy-middleware");
module.exports = function (app) {
app.use(
createProxyMiddleware("/socket.io/", {
target: "http://localhost:8000/",
ws: true,
})
);
}; Hope it helps someone. |
When I did the above steps my websocket to my dotnet app worked a treat - however, now my hot reloading isn't working with this error
Anyone with an idea on what is happening? my setup const proxy = require("http-proxy-middleware");
module.exports = function (app) {
app.use(
"/api",
proxy({
target: "http://localhost:5000",
changeOrigin: true,
})
);
app.use(
"/ws",
proxy({ target: "http://localhost:5000", ws: true, changeOrigin: true })
);
}; |
@no1melman I also see the same issue as you. Adding a Socket implementation: socket.io v4 |
@no1melman not sure if this helps but for some reason the following no longer affects sockjs and forwards as expected.. const proxy = require("http-proxy-middleware");
module.exports = function (app) {
app.use(
proxy("/api", { // <-- notice the pattern is not in use but in the proxy method
target: "http://localhost:5000",
changeOrigin: true,
})
);
app.use(
proxy("/ws", { // <-- notice the pattern is not in use but in the proxy method
target: "http://localhost:5000",
ws: true,
changeOrigin: true
})
);
}; |
My question may be silly, but I have multiple websockets in my backend : How to deal with all these ?????? |
+1 |
+1 |
with :-
I am getting the following :-
|
With Chrome and ApolloServer and plain
Similar on FireFox :-
|
Okay I tried #5280 (comment) and it worked with latest subscriptions implementation on Apollo Server on FireFox and Chrome, but not on MS Edge. I will have to look into that later its not throwing up any errors, but is not working on MS Edge, it might be my code seeing as how near Chrome and Edge are. |
My Finally workable version for ASP.NET Core 6: const {createProxyMiddleware} = require('http-proxy-middleware');
const {env} = require('process');
const target = env.ASPNETCORE_HTTPS_PORT
? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}`
: env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'http://localhost:40239';
const context = [
"/test"
];
module.exports = function (app) {
app.use(createProxyMiddleware(context, {
target: target,
secure: false,
headers: {
Connection: 'Keep-Alive'
}
}));
app.use(createProxyMiddleware("/hubs/", {
target: target,
ws: true,
secure: false
}))
}; |
i also saw that the problem is still active and is on the context function on react-dev-utils/WebpackDevServerUtils should create a PR for it? |
+1 |
Expected Behavior
Setting "proxy" in package.json should proxy WebSockets to specified server.
(As documented here: "The proxy option supports HTTP, HTTPS and WebSocket connections.")
Actual Behavior
Proxy does not work for WebSocket connections.
Reproducible Demo
No time for this atm. ¯\(ツ)/¯
The text was updated successfully, but these errors were encountered: