http-proxy-middleware icon indicating copy to clipboard operation
http-proxy-middleware copied to clipboard

Cannot connect to a websocket endpoint using HPM, the socket is immediately closed

Open slvrtrn opened this issue 3 years ago • 3 comments

Is this a question?

No

Is this a bug report?

Yes

Is this a feature request?

No

Steps to reproduce

Steps to reproduce are similar to https://stackoverflow.com/questions/59579433/proxied-websocket-connection-is-immediately-closed However, in my case, we do not use webpack so the accepted answer is not suitable for us.

Expected behavior

A socket to the endpoint is opened and accepting messages, similar to if I connect to the endpoint without HPM.

Actual behavior

If I use a tool like https://websocketking.com/, I receive

Could not connect to "ws://localhost:3003/api/ws". You may be able to find more information using Inspector/Dev Tools on this page.

networks says: Error during WebSocket handshake: Unexpected response code: 400 HPM console says (when using the similar code as specified in the stack overflow question):

13:37:48.231Z DEBUG: socket is destroyed true
[HPM] Upgrading to WebSocket

If I use the same tool but try to connect to the server WS endpoint directly (i.e. by using just a different port), everything works fine.

Setup

Ubuntu 20.04

client info

Node 14, HPM 1.0.6

target server info

A typical WS server that works fine without HPM

slvrtrn avatar Jan 21 '21 13:01 slvrtrn

@slvrtrn I'm having the same issue. Were you able to solve it?

focux avatar Mar 04 '21 17:03 focux

@slvrtrn I'm having the same issue. Were you able to solve it?

No, we had to use some other workarounds (direct traefik routing)

slvrtrn avatar Mar 15 '21 13:03 slvrtrn

Did you try to use the option: changeOrigin: true?

A working example can be found in this repo: https://github.com/chimurai/http-proxy-middleware/blob/master/examples/websocket/index.js#L11-L16

chimurai avatar Mar 27 '21 23:03 chimurai