chimurai / http-proxy-middleware

:zap: The one-liner node.js http-proxy middleware for connect, express, next.js and more
MIT License
10.63k stars 824 forks source link

SSE close events do not make it through proxy running on Windows #678

Open Nerdsie opened 2 years ago

Nerdsie commented 2 years ago

yarn why http-proxy-middleware OR npm ls http-proxy-middleware output (mask private folder names with *****)

proxy@1.0.0 D:\Development\proxy
`-- http-proxy-middleware@2.0.1

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:

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');

const app = express();

app.use('/', createProxyMiddleware({ target: 'http://localhost:3002/', changeOrigin: true }));
app.listen(3003);

I have a SSE server running on 3002, I connect to it on 3003 (via the proxy)

Step-by-step reproduction instructions

1. Launch an SSE server
2. On Windows, launch given minimal proxy example pointing to SSE server
3. Connect a SSE client to the server via the proxy
4. Close the SSE client

Expected behavior (be clear and concise)

The server should register the SSE "close" event

What http-proxy-middleware configuration are you using?

createProxyMiddleware({ target: 'http://localhost:3002/', changeOrigin: true })

What OS/version and node/version are you seeing the problem?

Windows 11 Pro Insider Preview -- Version 10.0.22489 Build 22489
 -- I'm almost certain I began experiencing this problem before running Windows 11
Node - v16.3.0

Additional context (optional)

Works inside WSL, does not work from Powershell or CMD

Kirlovon commented 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);
gitpushdashf commented 2 years ago

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.

Avataw commented 11 months ago

@gitpushdashf I am currently having the exact same problem. Did you find a way of solving this?

Noahkoole commented 7 months 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);

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

djmoran64 commented 2 months ago

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.