Skip to content

Operators Used in Multicasting

RxJS provides several dedicated operators to achieve "multicasting", sharing the same Observable output to multiple subscribers.

This page briefly introduces the typical operators related to multicasting from the as operators perspective, and organizes their usage and points to keep in mind.

❗ For conceptual explanations of multicasting, structural explanations using Subjects, and concrete code examples, see Multicasting Mechanism.

OperatorFeaturesNotes
share()The easiest multicast method. Internally equivalent to publish().refCount()Sufficient for many use cases
shareReplay()In addition to multicasting, provides recent values when resubscribingWhen state reuse is required
publish() + refCount()Multicast configuration with controllable execution timingClassic and flexible configuration
multicast()Low-level API that explicitly passes SubjectUseful when you want to use a custom Subject

Comparison of Multicasting Patterns

OperatorFeaturesUse Case
share()Basic multicastSimultaneous use across multiple components
shareReplay(n)Buffer past n valuesLate subscription/state sharing
publish() + refCount()More fine-grained control possibleWhen advanced control is needed
multicast(() => new Subject())Full customizationWhen special Subject types are needed

Cautions When Using Multicasting

  1. Understanding timing: Understand that the value you receive depends on when the subscription starts
  2. Lifecycle management: Especially when using refCount, the stream is completed when the number of subscribers reaches zero
  3. Error handling: If an error occurs in a multicast Observable, it will affect all subscribers
  4. Memory management: Be aware of memory leaks when using shareReplay, etc.

Released under the CC-BY-4.0 license.