RxJS - Opérateur de multidiffusion publier

L'opérateur publish () rend ConnectableObservable et doit utiliser la méthode connect () pour s'abonner aux observables.

Syntaxe

publish()

Exemple

import { interval,Observable } from 'rxjs';
import { filter, publish } from 'rxjs/operators';
var observable = new Observable(function subscribe(subscriber) {
   try {
      subscriber.next(Math.random());
   } catch (e) {
      subscriber.error(e);
   }
});

const observable1 = publish()(observable);
const subscribe_one = observable1.subscribe(
   x => console.log("Value from Sub1 = "+x)
);
const subscribe_two = observable1.subscribe(
   x => console.log("Value from Sub2 = "+x)
);
observable1.connect();

Production