Reducing HTTP requests within an angular 2 app Jamie McInerney Blocked Unblock Follow Following Dec 15, 2016 N.B. This post assumes a working knowledge of angular’s HTTP service, the async pipe and a little Rxjs. If like me you’re taking your first steps with angular 2 and the daunting new world of observables, then the following post will outline an efficient strategy when communicating with an API using angular’s HTTP service. Before we start I need to make something clear. I’m no Rx expert. I’m no Ben Lesh or André Staltz. Far from it. I’m just beginning my journey into the magical world of reactive programming and to properly sum up where I am on that journey, here’s a baby giraffe. So let’s set the scene. I have a wizard service that goes off to the wizard endpoint in the API and fetches some data. The component consumes the data and outputs it to the view. Pretty standard stuff. Our service returns an observable to our component which we then consume in our view via the async pipe. No need to subscribe to the observable as the async pipe takes care of this for us, making our component clean and lean. Everything looks hunky dory, right? On the surface yes but take a peek inside the network tab in your developer tools and you will see a call to the API for every subscription to the observable.

What the fudge! This simply will not do. Why are there so many requests? The answer oddly enough lies in the temperature of the observable. Yes, I said temperature. Observables can be described as either being ‘hot’ or ‘cold’. I will not go into detail about the difference between the two as this video by André Staltz can do a better job of explaining than I ever could. By default an observable returned from angular’s HTTP service is cold and this is the reason for our multiple requests. When you subscribe to a cold observable we are creating a brand new stream every time. Couple this with the async pipe and we are unknowingly setting up multiple subscriptions/streams and ultimately HTTP requests within our component. To rectify the problem we have to inject some heat into our observable. One way to achieve this is by using the share operator provided by the Rx library. The share operator will share or multicast the stream to multiple subscribers at the same time thereby not creating new streams but instead sharing just one. This sounds like what we need so let’s inspect our network tab now.

Thats better but still not good enough. We are still seeing multiple requests for the same data. This is due to the late subscriptions coming from the *ngIf condition in our template and another when we register a new subscriber within the button’s click handler. Turns out share is a sucker for punctuality and doesn’t like late subscribers. What we really want is to be able to cache the value from the initial HTTP call and replay it to all subscriptions no matter when they subscribe. publishReplay(1).refCount() for the win. The publishReplay operator takes care of caching all emitted values from the observable stream and replaying them to any subscribers no matter when they subscribe. Passing in a value of 1 ensures that only the last emitted value is cached and refcount() simply takes care of connecting to/from the observable. Now if we look at our network tab we will only have one request for all of our subscriptions.