netifi / netifi-js-client

Netifi JS Client www.netifi.com
Apache License 2.0
4 stars 1 forks source link

Add reconnect on websocket errors to client #5

Closed hypothete closed 5 years ago

hypothete commented 5 years ago

This PR adds reconnection attempts and handling to netifi-js-client. Reconnects were not possible prior to now because the RpcClient used to facilitate a connection to the transport returned a Single from its connect() method. This means that changes in the transport's connection could not be passed up the the gateway - recipients of RpcSockets from the RpcClient (like groups and destinations) could know when their connection failed, but the gateway itself was oblivious.

The first change to address this problem was to extend the RpcClient itself with what I'm calling the FlowableRpcClient class. Updates from the connection status of the transport are returned through onNext() to subscribers, so that they can know when changes occur to the transport. Unfortunately there's more copied code from RpcClient.js than I'd like, but it's necessary given the available exports.

The second change was to restructure the gateway. The gateway now has a subscriber called this._rpcClientSubscriber that attaches handlers to the FlowableRpcClient. If an error reaches the onNext handler, the current connection is closed, and then after a retry duration a replacement RpcClient is built and attached to this._rpcClientSubscriber. Subscribers are now tracked at the gateway level rather than in an IIFE available only to this_connecting (which became this._connectionStatus). Subscribers hold a reference to this._connection, which is our always-fresh ReactiveSocket, returned by successful communication from the FlowableRpcClient.

Lastly, I've made a number of changes to the Babel setup in order to facilitate some of the new code, and to support class extension better. You'll need to blow away your current node_modules before rebuilding and testing. I've been testing on a custom branch of netifi-quickstart js: https://github.com/netifi/netifi-quickstart-js/tree/bugfix/reconnect This project also needs to be rebuilt from scratch for Babel updates. The easiest way to test is to yarn link the client into the quickstart, and then run yarn build in the client.

hypothete commented 5 years ago

Addressing some problems - please hold off review for now.

hypothete commented 5 years ago

Well, turns out it was only working with regard to that specific test case. After re-reading through things and asking myself "how did that even work in the first place?", I have edits ready for review.

One thing I confirmed with this rev was that streams would break and throw an error on service/ broker loss. Streams can't resume (I believe that's a larger project that we've discussed?) but the clients reconnect and can request new streams.