reactivex.github.io icon indicating copy to clipboard operation
reactivex.github.io copied to clipboard

Clarify hot/cold Observable behavior

Open DavidMGross opened this issue 9 years ago • 3 comments

Apparently this remains a tough nut to crack for ReactiveX users.

See for instance ReactiveX/RxJava#3709

We have a brief mention here (http://reactivex.io/documentation/observable.html):

“Hot” and “Cold” Observables When does an Observable begin emitting its sequence of items? It depends on the Observable. A “hot” Observable may begin emitting items as soon as it is created, and so any observer who later subscribes to that Observable may start observing the sequence somewhere in the middle. A “cold” Observable, on the other hand, waits until an observer subscribes to it before it begins to emit items, and so such an observer is guaranteed to see the whole sequence from the beginning.

DavidMGross avatar Feb 15 '16 19:02 DavidMGross

I think we should start avoiding the terminology "cold"/"hot". With functions, for instance, we don't talk about cold/hot functions.

We should instead introduce Observables as if they would always be "cold", because they by default. For instance check this StackOverflow answer https://stackoverflow.com/questions/25338930/reactive-programming-rxjs-vs-eventemitter-in-node-js/25340716

staltz avatar Feb 16 '16 10:02 staltz

@staltz is right, but we do need some sort of idiom.

Really it's that the Producer is "hot". And the producer could live outside of the Observable.

e.g. this is "hot":


const socket = new WebSocket('ws://someserver/socket');

const soHotRightNow = Observable.create(observer => {
  socket.onmessage = (e) => observer.next(x);
  socket.onerror = (e) => observer.error(e);
  socket.onclose = () => observer.complete();
});

benlesh avatar Feb 16 '16 17:02 benlesh

... even a "published" observable falls under that. It's "hot" because the Observable that was published is still active. The "published observable" just connects your Observer to the active source observable when you subscribe.

benlesh avatar Feb 16 '16 17:02 benlesh