Home » RxJS publish() Multicasting Operator

RxJS publish() Multicasting Operator

by Online Tutorials Library

RxJS publish() Multicasting Operator

RxJS publish() operator is a multicasting operator that returns a ConnectableObservable, a variety of Observable that waits until its connect method is called before it begins emitting items to those Observers that have subscribed to it.

In other words, we can say that the RxJS publish() operator returns ConnectableObservable and requires to use connect() method to subscribe to the observables.

Syntax:

Following is the syntax of the RxJS publish() multicasting operator:

Or

Parameter Explanation

  • selector: It is an optional function which can use the multicasted source sequence as many times as required, without causing multiple subscriptions to the source sequence. It default value is undefined.

Return value

The RxJS publish() operator returns a ConnectableObservable that causes the source observable to emit items to its observers when the connection is established.

Let us see some examples of the RxJS publish() operator to understand it clearly.

Example 1 (Connect observable after subscribers)

Output:

After executing the above example, you will see the following result:

RxJS publish() Multicasting Operator

Example 2 (Merge each inner observable into a single one and then subscribe)

Output:

After executing the above example, you will see the following result:

RxJS publish() Multicasting Operator


Next TopicRxJS Operators

You may also like