question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proxying websockets to multiple backends doesn't work

See original GitHub issue

Is this a bug report?

Yes

Steps to reproduce

  1. Set up a proxy server with
app.use(
  '/sockjs-node',
  createProxyMiddleware('/sockjs-node', {
    target: 'http://127.0.0.1:3000',
    ws: true,
  })
);
app.use(
  '/socket.io',
  createProxyMiddleware('/socket.io', {
    target: 'http://127.0.0.1:4000',
    ws: true,
  })
);

The /sockjs-node server can be quickly spun up with create-react-app since that is the socket for Webpack hot reload. /socket.io can be quickly spun up with a socket.io example server.

  1. Load up the React app in your browser. The /sockjs-node requests are not proxied correctly. The HPM log output shows that it is trying to proxy the /sockjs-node request to port 4000, resulting in an ECONNREFUSED error.

Setup

  • http-proxy-middleware: 1.0.5
  • http-proxy-middleware configuration: see above
  • server: express 4.17.1

client info

Chrome (latest) / Ubuntu 20.04 (Linux)

target server info

One is a webpack dev server, one is a simple express server with socket.io config

Reproducible Demo

https://github.com/bduffany/hpm-bug

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:7
  • Comments:14

github_iconTop GitHub Comments

15reactions
bduffanycommented, Apr 4, 2022

I found that if I change

app.use(
  '/sockjs-node',
  createProxyMiddleware('/sockjs-node', {
    target: 'http://127.0.0.1:3000',
    ws: true,
  })
);

to

app.use(
  createProxyMiddleware('/sockjs-node', {
    target: 'http://127.0.0.1:3000',
    ws: true,
  })
);

Then the issue is fixed. No ideas yet as to why. However, I think this fix results in worse performance, because express is now going to be running that proxy middleware on every request, even ones that don’t match /sockjs-node. It might be OK though if you’re only using this for local development.

7reactions
Cheyenne55commented, Sep 24, 2020

You just saved my day with your fix. Spent 3 hours because I had this exact problem.

When proxying to several websocket servers Chrome was showing errors like “Invalid frame header” or “WebSocket connection failed: One or more reserved bits are on: reserved1 = 0, reserved2 = 1, reserved3 = 1” which googling them up led to absolute non-sense.

I think this issue should really be patched up.

Read more comments on GitHub >

github_iconTop Results From Across the Web

nginx proxy websocket to multi nodes - Stack Overflow
I use nginx as proxy server, backend are socketio services. ... to different backend server, have tried hash directive but doesn't work
Read more >
NGINX double proxy not allowing websocket connections
When I only had one proxy I was able to make successful websocket connections, but after moving to two all websocket upgrade requests...
Read more >
Improve reverse proxy, automatic handling of websockets
Fortunately it is possible to handle websocktets automatically by using the rewrite enginge: E.g. Reverse proxy xy.mydomain host to xy-backend.
Read more >
How to Reverse Proxy Websockets with Apache 2.4 - Serverlab -
WebSockets were introduced to open two-way interactive ... send traffic to multiple backend servers, the proxy Load Balancer module is used.
Read more >
Using NGINX as a WebSocket Proxy
For enterprise production use, where multiple WebSocket servers are needed for performance and high availability, a load balancing layer ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found