Publish
convert an ordinary Observable into a connectable Observable
See Also
- Connect
- RefCount
- Replay
- Introduction to Rx: Publish & Connect
- 101 Rx Samples: Publish — Sharing a subscription with multiple Observers
- Wedding Party: Share, Publish, Refcount, and All That Jazz by Kaushik Gopal
Language-Specific Information
RxGroovy publish
RxJava 1․x publish
RxJS let letBind multicast publish publishLast publishValue
The above operators are available in the following packages:
rx.all.js
rx.all.compat.js
-
rx.binding.js
(requires eitherrx.js
orrx.compat.js
) rx.lite.js
rx.lite.compat.js
RxJS also has a multicast
operator which operates on an ordinary Observable, multicasts that Observable by means of a particular Subject that you specify, applies a transformative function to each emission, and then emits those transformed values as its own ordinary Observable sequence. Each subscription to this new Observable will trigger a new subscription to the underlying multicast Observable.
Sample Code
var subject = new Rx.Subject(); var source = Rx.Observable.range(0, 3) .multicast(subject); var observer = Rx.Observer.create( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); } ); var subscription = source.subscribe(observer); subject.subscribe(observer); var connected = source.connect(); subscription.dispose();
Next: 0 Next: 0 Next: 1 Next: 1 Next: 2 Next: 2 Completed
The multicast
operator is available in the following packages:
rx.all.js
rx.all.compat.js
-
rx.binding.js
(requires eitherrx.lite.js
orrx.compat.js
) rx.lite.js
rx.lite.compat.js
There is also a let
operator (the alias letBind
is available for browsers such as Internet Explorer before IE9 where “let
” is forbidden). It is similar to multicast
but does not multicast the underlying Observable through a Subject:
Sample Code
var obs = Rx.Observable.range(1, 3); var source = obs.let(function (o) { return o.concat(o); }); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); }); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Next: 1 Next: 2 Next: 3 Next: 1 Next: 2 Next: 3 Completed
The let
(or letBind
) operator is available in the following packages:
rx.all.js
rx.all.compat.js
rx.experimental.js
It requires one of the following packages:
rx.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
RxPHP multicast multicastWithSelector publish publishLast publishValue
© ReactiveX contributors
Licensed under the Apache License 2.0.
http://reactivex.io/documentation/operators/publish.html