-
Notifications
You must be signed in to change notification settings - Fork 866
websocket proxy cannot upgrade websocket connection #112
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
Comments
Thanks for reporting. Can you provide info on the websocket client(s) you are using.
http-proxy-middleware/lib/index.js Line 15 in 0cb6839
Can you try to remove the debounce code and see if it solves the issue? From: var wsUpgradeDebounced = _.debounce(handleUpgrade); To: var wsUpgradeDebounced = handleUpgrade; |
We are using the websocket client implementation of Chrome(53.0.2785.116 m (64-bit)) and Firefox (47.0). Frequency: I try to establish only one connection on initialisation. Nothing changed on removing the debounce code. |
Just to confirm if the issue is related to HPM. If is it HPM related; It can be either an issue in HPM configuration or a bug in HPM. Try adding the option: devServer: {
proxy: {
'/hyperguard/websocket/*': {
target: 'ws://localhost:8082',
changeOrigin: true,
ws: true
}
}
} |
Direct connectionI think the issue is related to HPM, because if i request a websocket upgrade directly it always works as expected. request (without HPM)
immediatlely response (without HPM)
WorkaroundI figured out a way to reproduce a workaround with tornado + HPM + webpack-dev-server. response
If i go back to my working url e.g. http://localhost:8079 the websocket connection can be upgraded as expected. Q&R
|
Can you try setting your target with the target: 'http://localhost:8082' |
Changing the protocol of the target option has no effect. I have to debug the issue in-depth. It cannot be excluded that the problem occures on tornados side. |
Think I've exhausted the common configurations. :) FYI: |
It looks like I get this with webpack-dev-server 1.16.2 also. I get this on the server:
Chrome reports:
When using Fiddler it reports:
I'm using a similar config:
One thing I do see which hasn't been reported before is nothing happens if I perform raw websocket connection on a cold start, it just hangs. I only get the above output if I first attempt a basic The closest issue I've found in the |
@dansiviter, could you try to change your |
Thanks, one step closer as that worked. However, I still need to warm up the connection with a For anyone who needs it my config is:
Which ultimately proxies to |
debugged that problem weeks ago rudimentary, it seems that the http proxy middleware which is applied to the express server never be executed. maybe a side effect with sockjs which is used to handle the hot realoading websocket channel. |
@dansiviter this SO answer might be helpful: http://stackoverflow.com/a/32943389/465887 I'm working on enabling websocket proxying in Create React App (facebook/create-react-app#1013) and it looks like I can manually watch for the "upgrade" request coming from the devServer, and then call HPM's "upgrade" function. CRA is using HPM directly though, instead of the // Pass the scope regex both to Express and to the middleware for proxying
// of both HTTP and WebSockets to work without false positives.
var hpm = httpProxyMiddleware(pathname => mayProxy.test(pathname), {
target: proxy,
logLevel: 'debug',
onError: onProxyError(proxy),
secure: false,
changeOrigin: true,
ws: true
});
devServer.use(mayProxy, hpm);
devServer.listeningApp.on('upgrade', hpm.upgrade); |
I had problems with the proxy websocket closing right after connecting. Found out that socket io was closing unhandled requests. If you are using socket io, the fix is to set the destroyUpgrade option to false: var express = require('express');
var app = express();
var http = require('http').Server(app);
var proxy = require('http-proxy-middleware');
var io = require('socket.io')(http, {destroyUpgrade: false}); |
Also seeing this issue. @mixxen's fix seems to have worked for me. |
Apologies for commenting on an ancient ticket, but I managed to hit this issue (ws proxy only starts working after manually performing a GET call).
... I understand that no resolution was found, but does anyone know any workaround (better than manually doing a GET, I mean)? |
Same here. AFAIU issue on the middleware happens if server is trying to send data socket which has already been closed, but I haven't dig it enough yet. I'll try to find more details, just wanted to underline that issue persists in some conditions. |
Got stuck with this issue in my CRA app. I have changed my
So that the request to React App itself works as a "first manual GET" for the WebSocket (it actually registers the "upgrade" handler) |
I am not entirely sure how this works... but it does! Thank you for sharing this workaround! |
@torrejonv from my understanding it works like lazy loading. Until you make a request to the route where proxy middleware is registered there will be no "upgrade" handler added to the HTTP server. With this ☝️ approach, any request passes through the proxy middleware so that we have the "upgrade" handler registered from the very beginning. |
Thank you for the explanation. Very ingenious indeed! |
Expected behavior
HPM should upgrade websocket requests always
Actual behavior
HPM websocket proxy cannot upgrade websocket connections sporadically
Setup
proxy middleware configuration
server mounting
Howto Reproduce
http stream request
http stream response
getting this response only sometimes, if not the websocket connection never be established
The text was updated successfully, but these errors were encountered: