function deprecated operator
Returns a ConnectableObservable, which is 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.
Will be removed in v8. Use the connectable observable, the connect operator or the share operator instead. See the overloads below for equivalent replacement examples of this operator's behaviors. Details: https://rxjs.dev/deprecations/multicasting
publish(): UnaryFunction<Observable<T>, ConnectableObservable<T>>Returns a connectable observable that, when connected, will multicast all values through a single underlying Subject instance.
There are no parameters.
UnaryFunction<Observable<T>, ConnectableObservable<T>>
publish(selector: (shared: Observable<T>) => O): OperatorFunction<T, ObservedValueOf<O>>Returns an observable, that when subscribed to, creates an underlying Subject, provides an observable view of it to a selector function, takes the observable result of that selector function and subscribes to it, sending its values to the consumer, then connects the subject to the original source.
selector | (shared: Observable<T>) => O | A function used to setup multicasting prior to automatic connection. |
OperatorFunction<T, ObservedValueOf<O>>
Makes a cold Observable hot
Make source$ hot by applying publish operator, then merge each inner observable into a single one and subscribe
import { zip, interval, of, map, publish, merge, tap } from 'rxjs';
const source$ = zip(interval(2000), of(1, 2, 3, 4, 5, 6, 7, 8, 9))
.pipe(map(([, number]) => number));
source$
.pipe(
publish(multicasted$ =>
merge(
multicasted$.pipe(tap(x => console.log('Stream 1:', x))),
multicasted$.pipe(tap(x => console.log('Stream 2:', x))),
multicasted$.pipe(tap(x => console.log('Stream 3:', x)))
)
)
)
.subscribe();
// Results every two seconds
// Stream 1: 1
// Stream 2: 1
// Stream 3: 1
// ...
// Stream 1: 9
// Stream 2: 9
// Stream 3: 9
© 2015–2022 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/operators/publish