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

support reconnection on errors (remote server down, etc) #651

Open
jbcpollak opened this issue Mar 24, 2021 · 6 comments
Open

support reconnection on errors (remote server down, etc) #651

jbcpollak opened this issue Mar 24, 2021 · 6 comments

Comments

@jbcpollak
Copy link

I tried this library out and the interface is great. I'd love to use it, but I noticed it doesn't support reconnecting when the EventSource errors out. Specifically, something like this would be useful:

	eventSource.addEventListener(
		'error',
		(e) => {
			console.log(
				'Something went wrong with the EventSource connection. Trying again in 1 second.',
				e,
			);
			eventSource.close();

			// this part gets trick with the current structure of the code
			setTimeout(() => createEventSource(), 1000);
		},
		false,
	);
@samouss
Copy link
Owner

samouss commented Mar 25, 2021

Hello @jbcpollak! Indeed this on my list of things to improve. IIRC by default, the EventSource exposed by the browser should manage the reconnection to the endpoint in case of failure. Otherwise, you are absolutely right it is not trivial to implement. Even with a new source created we have to backport the listeners.

Today the interface is too much tied to the EventSource. We already have our abstraction for managing the listeners, we shouldn't have to expose this knowledge to the source. In the end, we should manage the listeners and only use the source as a transport layer for the data. In case of failure, we can let the source deal with it. The listeners will not run until the source is back online.

@jbcpollak
Copy link
Author

IIRC by default, the EventSource exposed by the browser should manage the reconnection to the endpoint in case of failure.

this is true, but doesn't work when the initial connection fails, ie, if the server is not up when you start the process. In our case, we are running an embedded application with a backend process and a kiosk app written with electron and react. The frontend can be up before the backend, so the initial connection can fail.

Details here:

https://developer.mozilla.org/en-US/docs/Web/API/EventSource/error_event

because (I think) the problem only happens on initial connection, you may not need to preserve all of the listeners, though because of the way the lazy loading works I guess you'll have to preserve the first one.

@strarsis
Copy link

strarsis commented Mar 6, 2022

@samouss: Please add this initial connection check! 🐱

@ghost
Copy link

ghost commented Nov 2, 2022

I'm currently working with a source that through a proxy that terminates idle connections after 60 seconds. I could solve it with some sort of keepalive, but being able to reconnect would be better.

I notice that this package (although kind of outdated) reimplements an event source handler on to of fetch https://github.com/Azure/fetch-event-source and it supports reconnect as well as using methods over than GET

@hasanyelaldi
Copy link

Hi, does this error still occur?
@jbcpollak @samouss

@jbcpollak
Copy link
Author

Unfortunately I cannot say, we eventually implemented our own ReconnectingEventSource and removed our dependency on the library because it wasn't working for us at the time.

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

4 participants