robtaussig / react-use-websocket

React Hook for WebSocket communication
MIT License
1.63k stars 135 forks source link

Reconnect SSE useEventSource on error #140

Open cubecleveland opened 2 years ago

cubecleveland commented 2 years ago

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 ?

robtaussig commented 2 years ago

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.

cubecleveland commented 2 years ago

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 commented 2 years ago

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..