This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Introduction to the ConnectableObservable and using publish().refCount() in RxJS

3:56 RxJS lesson by

An RxJS cold Observable can be made hot by declaring it with .publish().refCount(). What does publish() do? What does refCount() do? This lesson introduces the ConnectableObservable, and how to use it with publish and refCount.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

An RxJS cold Observable can be made hot by declaring it with .publish().refCount(). What does publish() do? What does refCount() do? This lesson introduces the ConnectableObservable, and how to use it with publish and refCount.

Avatar
Dean

Thanks for the video!
Is it possilble, that in the future - these RxJS videos also perhaps give a real world usage. I think the concepts/tutorials are fantastic, but I am sometimes at a loss to find any usage beyond some async ajax call usage.

In reply to egghead.io
Avatar
Andre

Hi Dean, yes, most likely we will get to that as well. As videos are bite-sized, we need to focus on one point at a time, that's why this video is focused on explaining the publish concepts.

In reply to Dean
Avatar
Bogdan

Hi,

Really great stuff about RxJS. And yes, please give at least some references about real world usages for hot/cold Observables and other topics in the videos. would help a lot..!!

In reply to Andre

When we create hot observables from cold ones, we often do that with the idiom "Publish RefCount." Without this, the clock observable would be cold, and when we add Publish RefCount, it becomes a hot observable.

What do these two methods mean, and why are there two of them? We're going to see how hot observables work under the hood.

If I call it just Publish and not RefCount, I get back a connectable observable. If we console.log when the subscriptions happen and we run the code, we will see an interesting behavior.

When we run it, A subscribes, and after four-and-a-half seconds, B will subscribe, but none of them saw the events that they were expecting to happen here.

What we need to do, as the name indicates -- this clock observable is a connectable observable -- we need to connect it. After a while, we will call clock.connect, after, let's say, five seconds, which is after both B and A have subscribed. We're also going to console.log that it was connected.

A subscribes, and B will subscribe, and at second five, it will get connected and both of them will see event zero and one, and et cetera, and simultaneously because it's hot.

What if instead of connecting it after five seconds, we would connect it immediately when we create this observable? Here, we would say clock.connect, and just to console.log out the moment when it's connected.

What happens now is, it will connect before A or B is subscribed, and that means that A did not see the event zero, so A missed that event.

Now, this is nice because we actually have a race condition here where we want the event zero, but we missed it because we connected it before.

This is where RefCount comes in. If we called RefCount here and we run this again, we see that now A gets the event zero, and one, and et cetera.

What does RefCount do? If you remember the live video analogy, a RefCount and a hot observable is analogous to a live video of a band playing at a concert, but the band doesn't start playing if there isn't anyone in the audience. That would be a waste, right? So, why play if there is no one watching?

RefCount tells the band to play when there is at least one person in the audience, in other words, when the number of observers goes from zero to one.

Another name for RefCount would have been, let's say, how to connect when observed. That would be a perfectly valid name for RefCount, but it's called RefCount. It also works in the opposite direction, that's why it's called reference counting. When the number of observers goes from one to zero, the band stops playing, so the connectable observable gets disconnected.

Often, you do not need to call connect. It is best just to use RefCount, because as a programmer, you don't want to worry about when does the hot observable start and stop? This is why we use both Publish to make it a connectable observable, in other words, hot, and RefCount to automatically connect the hot observable when it has observers.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?