RxJs Tutorial Series: Part 3 – Understanding Subscription
- Posted by Muthukumar Dharmar
- Categories RxJs for Beginners
- Date July 28, 2020
Subscription
As we discussed in part 2, subscription plays a major role in RxJs stream without the subscription there won’t be any stream at all.
- Subscription: It is a control for the execution or the cancellation of the process/observables.
One important point that we must remember all the time. If there is no consumer, then no point in emitting events (or) data stream right!. That’s why the Observables are lazy by design, meaning until there is a consumer begins to listen, Observables won’t produce any events. Now you understand why we call the consumers as Observers.
A Real-World Example
For better understanding, we will continue reviewing our television example.
Technically speaking, only the devices which have the subscription can play programs to the subscribers. In other words without the subscription, the television can’t play the channel/program. right!.
It’s like the same in RxJs, if we need to observe some data, we have to subscribe to it. Otherwise, the stream/observables might be there in memory but it won’t be emitted/passed to the observer.
Time to see the code in action
Let’s relate the terms that we learned so far with the above code
Here the:
- Producer is myControl(FormControl) object
- Consumer is this UI component.
- Functions in DataFlow Pipeline are filter, debouceTime, tap & switchMap
- Subscription is where we assign the result to filterOptions
We will discuss these operator functions in more detail in the upcoming series.
The source code for this demo is available here.
Tag:rxjs, scheduler, task maager, task scheduler
Over 17 yrs of experience in the IT industry.
Skill Set: Angular, RxJS Reactive Programming, AspNet WebApi, WCF, WPF, Azure, etc.,