redux-websocket
redux-websocket copied to clipboard
Attempting reconnection if it fails on the first attempt
Hey hey
Thanks for the great library and doco to go with it!
One thing which tripped me up was that the library doesn't attempt any reconnection if the initial connection fails. I see in the code there's a comment indicating that this is the intended behaviour:
https://github.com/giantmachines/redux-websocket/blob/master/src/ReduxWebSocket.ts#L251
Rather than having to implement in this retry-if-first-connection-failed-but-not-if-we-already-connected logic in a middleware (or perhaps somewhere else, I'm still kinda new to the Redux Way) , would you be opposed to adding in an optional field to the ReduxWebSocketOptions
interface (and integrating with the ReduxWebSocket
class) to allow users to pick a reconnectionPolicy
, which would default to the existing behaviour.
I'm envisaging something like:
import reduxWebsocket, { Reconnect } from '@giantmachines/redux-websocket';
const reduxWebsocketMiddleware = reduxWebsocket({
reconnectionPolicy: Reconnect.ALWAYS
});
const reduxWebsocketMiddleware = reduxWebsocket({
reconnectionPolicy: Reconnect.AFTER_SUCCESSFUL_CONNECTION
});
// which is equivalent to
const reduxWebsocketMiddleware = reduxWebsocket({
reconnectOnClose: true
});
I'd like this to be backwards-compatible. In a non-backwards-compatible approach (e.g. the v2 I've seen mentioned) this could replace the existing reconnectOnClose
field.
What do you think? Is this something you'd accept a PR for?
Hi @dsalisbury, thanks for opening this issue. You are correct that the library will only attempt to reconnect if the connection was successfully opened at least once. We also have issue #90 for adding different reconnect strategies like exponential backoff and max retries which may fit well with this solution. We'd be happy to work with you on getting something like this implemented.
hey @procchio6! Thanks replying. I've raised this WIP PR to get some general feedback: https://github.com/giantmachines/redux-websocket/pull/123
let me know if you have any thoughts.
Hi @dsalisbury Sorry for the delayed response. I will take a look at your PR and get back to you as soon as possible.
Any update on this?