Member-only story
RxJS: Understanding the publish and share Operators
I’m often asked questions that relate to the publish
operator:
What’s the difference between publish and share?
How do I import the refCount operator?
When should I use an AsyncSubject?
Let’s answer these questions — and more — by starting with the basics.
A mental model for multicasting
Multicasting is the term used to describe the situation in which each notification emitted by a single observable is received by multiple observers. Whether or not an observable is capable of multicasting depends upon whether that observable is hot or cold.
Hot and cold observables are characterised by where the producer of the observable’s notifications is created. In his article Hot vs. Cold Observables, Ben Lesh discusses the differences in detail, and the differences can be summarised as follows:
- An observable is cold if the producer of its notifications is created whenever an observer subscribes to the observable. For example, a
timer
observable is cold; each time a subscription is made, a new timer is created. - An observable is hot if the producer of its notifications is not created each time an observer subscribes to the observable. For example…