http-proxy-middleware: websocket proxy cannot upgrade websocket connection
Expected behavior
HPM should upgrade websocket requests always
Actual behavior
HPM websocket proxy cannot upgrade websocket connections sporadically
Setup
- http-proxy-middleware: 0.17.1
- server: tornado 4.4.1 - Tornado Github
- webpack: 1.13.2
- webpack-dev-server: 1.16.1
proxy middleware configuration
devServer: {
proxy: {
'/hyperguard/websocket/*': {
target: 'ws://localhost:8082',
ws: true
}
}
}
server mounting
def serve_forever(self):
app = self.__make_app()
self.__http_server = HTTPServer(app)
self.__http_server._handle_connection = self._handle_connection
self.__http_server.listen(self.__port, self.__host)
app.ssl_enabled = self.__ssl_options is not None
self.__ioloop.start()
Howto Reproduce
http stream request
GET /hyperguard/websocket HTTP/1.1
Host: localhost:8079
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Sec-WebSocket-Version: 13
origin: http://localhost:8079
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: yrOcNHCvvVuGgttYgv9nzA==
Connection: keep-alive, Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
http stream response getting this response only sometimes, if not the websocket connection never be established
HTTP/1.1 101 Switching Protocols
upgrade: websocket
connection: Upgrade
sec-websocket-accept: I3u4B7iakSJWk4yLd02hfn+enus=
- that issue also occurs on different platforms (fedora 22, ubuntu 14.04, centos7)
- restarting webpack-dev-server doesnt change anything
About this issue
- Original URL
- State: open
- Created 8 years ago
- Comments: 20 (4 by maintainers)
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).
… “http-proxy-middleware”: “^1.0.6”, “react”: “^17.0.1”, “react-scripts”: “4.0.0”, …
I understand that no resolution was found, but does anyone know any workaround (better than manually doing a GET, I mean)?
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:
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.
Thank you for the explanation. Very ingenious indeed!
Direct connection
I 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)
Workaround
I figured out a way to reproduce a workaround with tornado + HPM + webpack-dev-server. If the problem occurs i just have to request the websocket proxy path via http e.g http://localhost:8079/hyperguard/websocket
response
If i go back to my working url e.g. http://localhost:8079 the websocket connection can be upgraded as expected.
Q&R