Streams for multiplexing

One does not rise to a position of power on the King's council in Westeros without being a master at building networks of spies. Often the best spy is one who can respond the quickest. Similarly, we may have some code which has the option of calling one of many different services which can fulfill the same task. A great example would be a credit card processor: it doesn't really matter which processor we use as they're pretty much all the same.

To achieve this, we can kick off a number of HTTP requests to each of the services. If we take each of the requests and put them into a stream, we can use it to select the fastest to respond processor and then perform the rest of the actions using that processor.

With RxJS this looks like the following:

var processors = Rx.Observable.amb(processorStream1, processorStream2);

You could even include a timeout in the amb call which would be called to handle the eventuality that none of the processors responded in time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset