Subscriptions
Let's see how we can solve the problem associated with cold observables and make a single request but distribute it to multiple subscribers with the use of a "share" and "publish" operator in RxJS.
We'll cover the following...
Sometimes we don’t want to trigger the creation logic every time another part of your application wants to listen to the result.
Consider an AJAX request that fetches a user object:
let user$ = ajax('/user');
Subscriptions
You might naively pass around user$
to all the components in your application, where all of the listeners happily subscribe and pull whatever information they need.
This solution works until the backend engineer comes chasing after us with a pile of excessive log files demanding to know why every page load makes seventeen requests to the /user
endpoint.
Cold observables
Oops. That’s because observables are “cold” by default. Each new subscription creates an ...