I wasn't sure whether to post this on StackOverflow or here. Let me know if you think it belongs on StackOverflow.
I have an application that will run semi-persistently, i.e. all the time. The application interacts with a financial service through a websocket connection (we are the client-side). We keep the connection alive to remove connection latency. If, for whatever reason, the websocket disconnects, we should try to reconnect.
Logically, how should we go about attempting reconnection? This is such a common function, there must be some best practices and techniques. I can find lots of examples of how to implement the trivial strategy of just trying to reconnect over and over, with a small interval in between. I have found at least one tool that uses a slightly more complex strategy. I have not found any discussions of the topic in the abstract.
Are there best practices or some theory about the best way to reconnect websockets in general? I feel the obvious approach probably works, but smart people can often make things way better than the obvious solution. In particular...
- Should we attempt to reconnect forever or should we give up and shut down eventually? If we should give up eventually, when?
- Should we vary (i.e. increase) the interval between reconnection attempts?
- When the app tries to use the websocket while it is not connected, should we backlog that interaction and send on reconnect or should we throw an error?
- Should our reconnection attempts be independent of attempts to use the websocket, or should we try harder to reconnect if the app is trying to use the websocket?
I feel there must be some theoretical treatment of these questions. I imagine the answers to these questions would not be "yes" or "no" but rather a discussion of how to decide.