Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
Nerdsie opened this issue Nov 2, 2021 · 6 comments
Open
2 tasks done

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

Nerdsie opened this issue Nov 2, 2021 · 6 comments
Labels

Comments

@Nerdsie
Copy link

Nerdsie commented Nov 2, 2021

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

@Nerdsie Nerdsie added the bug label Nov 2, 2021
@Nerdsie Nerdsie changed the title SSE close events do not make it through proxy on Windows SSE close events do not make it through proxy running on Windows Nov 2, 2021
@Kirlovon
Copy link

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
Copy link

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
Copy link

Avataw commented Aug 2, 2023

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

@Noahkoole
Copy link

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
Copy link

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.

@chuanqisun
Copy link

Should it be

target: 'http://localhost:3002/', 
changeOrigin: true,
on: {
  proxyReq: (proxyRes, req, res) => {
    res.on("close", () => proxyRes.destroy());
  },
},

or

target: 'http://localhost:3002/', 
changeOrigin: true,
onProxyReq: (proxyRes, req, res) => {
    res.on('close', () => proxyRes.destroy());
}

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants