MatthewWid / better-sse

⬆ Dead simple, dependency-less, spec-compliant server-sent events implementation for Node, written in TypeScript.
MIT License
485 stars 14 forks source link

Vite dev server not observing SSE close event from client #50

Open abcd-ca opened 1 year ago

abcd-ca commented 1 year ago

Hello, I have an expressjs back-end and a Vite dev server (running React) on the front-end. Have you encountered the issue where the close event from the client doesn't get triggered? I've inquired with Vite and with one of their dependency libraries, node-http-proxy. I don't think this is an issue with your library, it's lower level and probably with one of those 3rd parties but thought I'd ask.

https://github.com/http-party/node-http-proxy/issues/921#issuecomment-1443966898

https://github.com/vitejs/vite/issues/12157 (contains link to demo for the issue)

yasumuo commented 9 months ago

I am not using this library, so it may be wrong to write here, but I was encountering the same problem as you, so I share the information. I am using Go for the back-end, but the front-end is the same (running React on a vite dev server). The close event of the client was not propagated to the back-end. I added the following settings to vite.config.ts based on the comments you provided(https://github.com/http-party/node-http-proxy/issues/921#issuecomment-1598486248), and now the close event is propagated to the back-end. (The version of vite I am using is 4.3.0)

export default defineConfig({
  ...
  server: {
    proxy: {
      ...
      "/sse": {
        ...
        configure: (proxy, _options) => {
          proxy.on("proxyReq", (proxyReq, req, res) => {
            res.on("close", () => {
              if(!res.writableEnded) {
                proxyReq.destroy()
              }
            })
          })
        }
      }
    },
  },
});