redux-websocket icon indicating copy to clipboard operation
redux-websocket copied to clipboard

Attempting reconnection if it fails on the first attempt

Open dsalisbury opened this issue 4 years ago • 4 comments

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?

dsalisbury avatar Apr 20 '20 08:04 dsalisbury

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.

procchio6 avatar Apr 24 '20 19:04 procchio6

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.

dsalisbury avatar May 05 '20 07:05 dsalisbury

Hi @dsalisbury Sorry for the delayed response. I will take a look at your PR and get back to you as soon as possible.

chenghw avatar May 22 '20 18:05 chenghw

Any update on this?

master-elodin avatar Nov 10 '20 16:11 master-elodin