As in the last chapter we have learn about the subject. That subjects are used for multicasting Observables. This means that Subjects will make sure each subscription gets the exact same value as the Observable execution is shared among the subscribers.
Rxjs offers different types of subject.
- BehaviorSubject
- ReplaySubject
- AsyncSubject
Lets see each in details.
BehaviorSubject: BehaviorSubject is one of the variant of subject. BehaviorSubject stores the current value. It means that you can always directly get the last emitted value from the BehaviorSubject. You can get the last emmited value in two ways –
Either you can get the value by accessing the .value
property on the BehaviorSubject or you can subscribe to it. In the second case if you subscribe to it, the BehaviorSubject will directly emit the current value to the subscriber. Even if the subscriber subscribes much later than the value was stored.
Lets see the below example –
In this example firstly we have created the subject and subscribe to that with Subscriber A. The Subject then emits it’s value and Subscriber A will log the random number. Then subject emits it’s next value. Subscriber A will log this again. After that subscriber B starts with subscribing to the subject. Since the subject is a BehaviorSubject the new subscriber will automatically receive the last stored value and log this. The subject emits a new value again. Now both subscribers will receive the values and log them. At last we log the current Subjects value by simply accessing the .value
property. This is quite nice as it’s synchronous. You don’t have to call subscribe to get the value.
One most important thing about the behaviour subject is that you can pass by default or initial value to it when you create the behavior subject. Like this –
const subject = new Rx.BehaviorSubject(Math.random());
ReplaySubject: ReplaySubject can send “old” values to new subscribers. It can record a part of the observable execution and therefore store multiple old values and “replay” them to new subscribers. When creating the ReplaySubject you can specify how much values you want to store and for how long you want to store them.
For example lets see the below code-
In the below example we have created a ReplaySubject and specify that we only want to store the last 2 values then subscribing to the Subject with Subscriber A. We execute three new values trough the subject. Subscriber A will log all three. Now we start subscribing with Subscriber B. Since we told the ReplaySubject to store 2 values, it will directly emit those last values to Subscriber B and Subscriber B will log those. Subject emits another value. This time both Subscriber A and Subscriber B just log that value.
AsyncSubject: In AsyncSubject only the last value of the Observable execution is sent to its subscribers, and only when the execution completes.
Here firstly we have created the AsyncSubject. Then subscribe to the Subject with Subscriber A. The Subject emits 3 values, still nothing happening. Then we subscribe to the subject with Subscriber B.The Subject emits a new value, still nothing happening.The Subject completes. Now the values are emitted to the subscribers which both log the value.