Hot and cold observables
The preceding sections covered most of the factory operators available in RxPY. All of these operators start to emit items at subscription time. This is the default behavior in ReactiveX. Nothing happens until an observer subscribes to an observable. It is the subscription that makes the observable start emitting its items. This behavior is very important to understand when coding with ReactiveX, and it has a name: cold observables. Another behavior of observables is possible: hot observables.
So, what is the difference between cold and hot observables?
Cold and hot observables can be described as similar to the different ways of watching TV shows. Sometimes people watch TV shows on demand, and sometimes people watch TV shows live.
When somebody watches a TV show on demand, the content is sent to his TV only when he effectively asks to view the show (that is, when he presses the play button). If another person, or one thousand other people, want to view the same show, each of them will also receive this content; exactly when they request it, they will all receive their own video stream. This allows each person to pause the playback, go forward, or resume the playback without any impact on the other people viewing. This is the behavior of a cold observable. A cold observable starts to emit items at subscription time, and each time a new subscription occurs, a new sequence of items is started.
The following figure shows this behavior; each observer receives its own sequence, starting at subscription time:
On the other hand, when somebody watches a live show on TV then he does not choose when the event will start. The viewer has to be in front of his TV when the show starts; otherwise, he will miss the beginning of it. Also, if thousands of people watch the same live show, they all see the same content. At the very same time, they all receive the same video stream. This is great for events that people want to watch as they happen in real time, such as sports events, music shows, and so on. This is the behavior of a hot observable. A hot observable emits items whenever their triggering event happens. These items are emitted whatever the number of subscribers to it.
If an observer does not register before the observable starts to emit items then they will miss some items. The following figure shows this behavior; all observers receive the same items at the same time when they are emitted by the observable:
Both behaviors are necessary, and the best pattern must be used for each use case. Most of the time, cold observables correspond to the required behavior. That is probably why they are the default type of observable in ReactiveX. If a hot observable is needed, there are two ways to implement it.
The first way is with the create operator. Since it can be used to implement any logic, one can expose a hot observable with the create operator. For example, with the create operator, it is possible to create an observable which emits an item each time lightning hits the ground somewhere on earth. This is a typical hot behavior; observers need to know when lightning occurs, but there is no way for them to force lightning to happen.
The second one is with dedicated operators that can convert a cold observable to a hot observable. Let's look at how to use them in the next section.