Whenever we have multiple observers sharing the same execution, some of those observers may arrive too late. For instance, here we have a situation where observer B subscribes to the subject two seconds after these three values were emitted on the subject.
If you run this and see what happens, observer A was subscribed in time to receive the values, but B was too late. Because B subscribing doesn't create a new execution, it just shares the execution that is ongoing, it only sees values that will come in the future, but not those that happened in the past.
Let's try to draw a Marble diagram of this situation, so that we know exactly what we're dealing with, here. This will be the subject execution, or basically the values being emitted on the subject. It emits at some point here one, then emits two, and then emits three, but the observer A was subscribed before these three values were sent.
If we draw here where A appeared, A appeared somewhere here, before. That's why it was able to see one, two, and three. It keeps on observing the future values, as well. But B, it was subscribed much after the value three was delivered. Maybe somewhere here, B subscribed. It is still observing what's happening on the subject, but it doesn't see any values in the future.
How can we fix this? Because there are legitimate cases where we need a late observer like B to receive the current value from the subject. For instance, we would like B to see the value three. That's how we can use the so-called behavior subject.
A behavior subject is a subject that always has a current value. That's why actually we need to initialize the behavior subject with the value, because it always has a value, then it also has to start with a value. That's because this type of subject is able to remember, what was the last value emitted?
Then when late observers arrive, such as B, then the behavior subject will pass on to B, what was that value remembered inside the behavior subject? Now that we hit this run, we're going to see when A subscribes, A is actually going to receive, here at this spot, zero. Because that's the current value of the subject. Then when B subscribes, B will actually see here, three, like that.
The behavior subject is quite useful to model a value over time compared to an event stream. A typical example of that would be your age versus your birthdays, because your birthdays are a stream of events. They're events that happen annually, but your age is a value. You always have an age. You always have that value.
These are interrelated because, of course, your age changes on every birthday, but they are still fundamentally different ideas. You can converge from birthday to ages by remembering what was your latest birthday. That's essentially what we do here with behavior subject.
The behavior subject is meant for values over time like an age is. But subject, the normal subject is meant for event streams like birthdays.