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

Proxied SSE connections do not receive "onerror" events #2930

Open
martinszeltins opened this issue Dec 10, 2024 · 0 comments
Open

Proxied SSE connections do not receive "onerror" events #2930

martinszeltins opened this issue Dec 10, 2024 · 0 comments

Comments

@martinszeltins
Copy link
Contributor

Environment

------------------------------
- Operating System: Linux
- Node Version:     v20.11.0
- Nuxt Version:     3.11.2
- CLI Version:      3.11.1
- Nitro Version:    -
- Package Manager:  [email protected]
- Builder:          -
- User Config:      devtools
- Runtime Modules:  -
- Build Modules:    -
------------------------------

Reproduction

SSE server: https://github.com/martinszeltins/nitro-sse-server
SSE client: https://github.com/martinszeltins/nuxt-sse-client-with-proxy

Describe the bug

As @danielroe suggested, I am creating this issue here in Nitro repo.

Here is the issue:
I created an SSE (Server-sent Events) server using Nuxt with the help of @atinux gist here: https://gist.github.com/Atinux/05836469acca9649fa2b9e865df898a2. Thanks @atinux!

Then I created another Nuxt project to be the client that will connect to the SSE endpoint (using a Nitro proxy route). The connection works and event stream works too. 👍🏻

The problem is that when I turn off the server (manually cause an error), I get an error in the console in SSE server but on the client the eventSource.onerror doesn't receive any error event (!). This prevents me from being able to attempt a reconnect on the client. It seems the Nitro proxy does not forward the error to the client and hangs in an active state that is broken. The client still thinks the connection is active but in reality it is not.

How to reproduct the issue:

  1. Clone the SSE Server Nuxt project: https://github.com/martinszeltins/nitro-sse-server
  2. Run pnpm install && pnpm dev - this should start the project on port 3000
  3. Clone the SSE Client Nuxt project: https://github.com/martinszeltins/nuxt-sse-client-with-proxy
  4. Run pnpm install && pnpm dev - this should start the project on port 3001
  5. Open http://localhost:3001 and you should see messages coming in from SSE server app. (if the ports are 3000 and 3001)
  6. Now go over to the terminal that is running the SSE Server project and stop the dev server.
  7. Now go over to the terminal that is running the SSE Client project and you will see this error:
 ERROR  [nuxt] [request error] [unhandled] [500] terminated
  at Fetch.onAborted (node:internal/deps/undici/undici:10916:53)  
  at Fetch.emit (node:events:518:28)  
  at Fetch.terminate (node:internal/deps/undici/undici:10102:14)  
  at Object.onError (node:internal/deps/undici/undici:11034:38)  
  at _Request.onError (node:internal/deps/undici/undici:7200:31)  
  at errorRequest (node:internal/deps/undici/undici:9765:17)  
  at Socket.onSocketClose (node:internal/deps/undici/undici:8927:9)  
  at Socket.emit (node:events:518:28)  
  at TCP.<anonymous> (node:net:337:12)  
  at TCP.callbackTrampoline (node:internal/async_hooks:130:17)

It says that this is unhandled error but there is no way to handle this error. I tried adding a try...catch in server/routes/sse.ts but it does not work.

The main problem though is that this code does not trigger this client code:

// This does not work with Nuxt/Nitro proxy :(
// onerror never gets called (!)
eventSource.onerror = (event) => {
  console.error('EventSource failed:', event)
  // We should attempt to reconnect here
}

The client does not receive a onerror event. And this is a big problem because if I don't know there is a problem I can't attempt a reconnect or notify the user that something is wrong with the backend server.

Additional context

No response

Logs

No response

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

No branches or pull requests

1 participant