react-use-websocket icon indicating copy to clipboard operation
react-use-websocket copied to clipboard

Reconnect SSE useEventSource on error

Open cubecleveland opened this issue 3 years ago • 3 comments

Hello, im tryin to initiate a reconnect for the useEventSource using Event stream. Problem is that sometime the Server has high traffic and shuts the connection,

Is there any hthing i can do to reinitalize the EventStream to reopen the connection ?

cubecleveland avatar May 12 '22 16:05 cubecleveland

Hi @cubecleveland,

Unfortunately, I don't know enough about SSE to answer this question. If you can provide a code example, I can take a look.

robtaussig avatar May 17 '22 14:05 robtaussig

Thanks so much for the answer!

Im really just using the same example from the docs.

const { lastEvent, getEventSource, readyState } = useEventSource( 'http://localhost:3000/', { withCredentials: true, events: { message: (messageEvent) => { console.log('This has type "message": ', messageEvent); }, update: (messageEvent) => { console.log('This has type "update": ', messageEvent); }, }, } );

This works great. Problem is on a SAP in react when the server is lets say down.... or you are not authenticated.... the Event source is not getting connected...

I want to basically re-initate the call and make the libaray open another EventStream if connection fails...

thought about using a setInterval to reconnect but how do i use the same EventStream Object.. Hope all this make sense...

cubecleveland avatar May 20 '22 12:05 cubecleveland

CAn u guide me to the piece of code wheer the Eventstream is being created we might be able to contribute apiece to handle my use case..

cubecleveland avatar May 31 '22 09:05 cubecleveland