When should I use `publishReplay` vs `shareReplay`?

shareReplay() is basically publishReplay().refCount()

Definitely not.

Both shareReplay and publishReplay (+ calling connect on it) will make the observable behind it hot.

But the very important difference between them is:

  • shareReplay: won’t stop emitting until it’s completed, no matter if there are no subscriptions anymore or not.
  • publishReplay: will stop after the last subscriber unsubscribes if used together with refCount

Imho this is a crucial information.

Leave a Comment