react-hooks-sse icon indicating copy to clipboard operation
react-hooks-sse copied to clipboard

support reconnection on errors (remote server down, etc)

Open jbcpollak opened this issue 4 years ago • 6 comments

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,
	);

jbcpollak avatar Mar 24 '21 20:03 jbcpollak

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.

samouss avatar Mar 25 '21 16:03 samouss

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.

jbcpollak avatar Mar 25 '21 16:03 jbcpollak

@samouss: Please add this initial connection check! 🐱

strarsis avatar Mar 06 '22 22:03 strarsis

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

ghost avatar Nov 02 '22 14:11 ghost

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

hasanyelaldi avatar Nov 01 '23 06:11 hasanyelaldi

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.

jbcpollak avatar Nov 01 '23 15:11 jbcpollak