RxJS multicast() Multicasting OperatorRxJS multicast() operator is a multicasting operator that returns an observable that emits the results of invoking a specified selector on items emitted by a ConnectableObservable that shares a single subscription to the underlying stream. The RxJS multicast() operator is used to share the single subscription created with other subscribers. Syntax:Following is the syntax of the RxJS multicast() multicasting operator: Or Parameter Explanation
Return valueThe RxJS multicast() operator returns an observable that emits the results of invoking the selector on the items emitted by a ConnectableObservable that shares a single subscription to the underlying stream. Let us see some examples of the RxJS multicast() operator to understand it clearly. Before seeing and understanding the working of a multicast() operator, let us first see how the multicast() operator works. Example 1 (A simple observable with subscription)Output: After executing the above example, you will see the following result: In the above example, you can see that the output received from Sub1 and Sub2 are different. This is because when the subscriber gets called, the observable restarts and gives the fresh value available. But we need the subscribers getting called to have the same value. Now, let's see how multicast works. Example 2 (A simple multicast example)Output: After executing the above example, you will see the following result: In the above example, you can see that the same value is shared between the subscribers that are called. Example 3 (Multicast example with standard Subject)Output: After executing the above example, you will see the following result: Example 4 (Multicast example with ReplaySubject)Output: After executing the above example, you will see the following result:
Next TopicRxJS Operators
|