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

EventSource upstream connection not closed #1520

Open
carlhopf opened this issue May 6, 2021 · 5 comments
Open

EventSource upstream connection not closed #1520

carlhopf opened this issue May 6, 2021 · 5 comments

Comments

@carlhopf
Copy link

carlhopf commented May 6, 2021

I'm using webpack-dev-server (which uses node-http-proxy) to proxy EventSource connections. Demo project: https://github.com/carlhopf/webpack-dev-server-proxy-eventsource

When calling eventSource.close() client/browser side, the node-http-proxy upstream connection is not closed.

I fixed this attaching a listener in the proxyRes event:

res.on('close', () => {
  if (!res.finished) proxyRes.destroy();
});

I guess this could added to https://github.com/http-party/node-http-proxy/blob/1.18.1/lib/http-proxy/passes/web-incoming.js#L181

Is this the way to go, should I send a pull request?

@carlhopf carlhopf changed the title EventSource connections not closed EventSource upstream connection not closed May 6, 2021
@justice47
Copy link

Is it fixed?

@victorekpo
Copy link

@carlhopf had been searching for hours and your solution fixed everything, yes they should add this to the package for sure!

3ddysan added a commit to 3ddysan/poka that referenced this issue Oct 20, 2022

Partially verified

This commit is signed with the committer’s verified signature.
targos’s contribution has been verified via GPG key.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
- close proxy response on original response close
- see http-party/node-http-proxy#1520
@esfomeado
Copy link

The workaround provided will only work the first time as the proxy is going to be destroyed

@jnorvell
Copy link

jnorvell commented Aug 28, 2023

I am having a similar issue with EventSource connections through http-proxy v1.18.1. Mine work in development (create-react-app), but when deploying to production, the connection fails with the following browser (chromium) error:

EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection.

The portion of my server.js that creates the proxy is:

const httpProxy = require('http-proxy');

var app = express();
var apiProxy = httpProxy.createProxyServer({secure: false});

app.use(express.static(root));
app.use(compression());

/* proxy api calls over to port 9047 */
app.all('/api/*', (req, res) => {
   apiProxy.web(req,res, { target: 'https://localhost:9047' });
});

and the HTML document that React/NodeJS appears to be returning to the browser instead of the EventStream data is:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <link rel="icon" href="/icons/favicon.ico"/>
        <meta name="viewport" content="width=device-width,initial-scale=1"/>
        <meta name="theme-color" content="#000000"/>
        <meta name="description" content="Web site created using create-react-app"/>
        <link rel="apple-touch-icon" href="/icons/threaded_icon_57x57.png"/>
        <link rel="manifest" href="/manifest.json"/>
        <title>My App</title>
        <script defer="defer" src="/static/js/main.8ebfafc7.js"></script>
        <link href="/static/css/main.8c23b3de.css" rel="stylesheet">
    </head>
    <body>
        <noscript>You need to enable JavaScript to run this app.</noscript>
        <div id="root"></div>
    </body>
</html>

I am 100% sure that my backend (springboot in this case) server is returning the correct response type because it works in dev, and it works in production using a non-React/express client. Any suggestions?

@pi0
Copy link

pi0 commented Sep 1, 2023

Thanks to @didavid61202, a possible fix is merged to unjs/httpxy

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

No branches or pull requests

6 participants