Open Nerdsie opened 2 years ago
It seems to be a problem with the http-proxy module (which is used by http-proxy-middleware), since I had the same problem trying to proxy SSE connections through it.
According to this issue that i found in their repository, it can be fixed this way:
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
app.use('/', createProxyMiddleware({
target: 'http://localhost:3002/',
changeOrigin: true,
onProxyReq: (proxyRes, req, res) => {
res.on('close', () => proxyRes.destroy());
}
}));
app.listen(3003);
I'm having perhaps the inverse of this. When my backend server doing SSE dies, the proxy neither reconnects, nor disconnects the client so it can try to reconnect.
@gitpushdashf I am currently having the exact same problem. Did you find a way of solving this?
It seems to be a problem with the http-proxy module (which is used by http-proxy-middleware), since I had the same problem trying to proxy SSE connections through it.
According to this issue that i found in their repository, it can be fixed this way:
const express = require('express'); const { createProxyMiddleware } = require('http-proxy-middleware'); const app = express(); app.use('/', createProxyMiddleware({ target: 'http://localhost:3002/', changeOrigin: true, onProxyReq: (proxyRes, req, res) => { res.on('close', () => proxyRes.destroy()); } })); app.listen(3003);
This also solved my similar issue where I was streaming a mjpeg stream from my API and the backend relies on a close event to clean up certain connections. without this the API would never receive a close callback
THANKS - This fixed my issue as well. I am seeing it on MacOS so not a Windows issue. MacBook Pro - Model Identifier: Mac14,7 with M2 chip running on macOS 14.4.1 (23E224)
The problem only happens for me when using registry.npmjs.org/http-proxy/-/http-proxy-1.18.1
which I use in development (create-react-app). In production (no proxy) all works as expected.
yarn why http-proxy-middleware
ORnpm ls http-proxy-middleware
output (mask private folder names with *****)http-proxy-middleware
.Describe the bug (be clear and concise)
When proxy is run on Windows, although data passes to the client, the server doesn't receive any SSE "closed" events until the proxy itself is stopped.
Here is the minimal proxy I am running:
I have a SSE server running on 3002, I connect to it on 3003 (via the proxy)
Step-by-step reproduction instructions
Expected behavior (be clear and concise)
The server should register the SSE "close" event
What http-proxy-middleware configuration are you using?
What OS/version and node/version are you seeing the problem?
Additional context (optional)
Works inside WSL, does not work from Powershell or CMD