In RxJS, the connect
operator is a method of the ConnectableObservable
prototype. You can use the publish
operator to convert an ordinary Observable into a ConnectableObservable
.
Call a ConnectableObservable
’s connect
method to instruct it to begin emitting the items from its underlying Observable to its Subscribers.
The connect
method returns a Disposable
. You can call that Disposable
object’s dispose
method to instruct the Observable to stop emitting items to its Subscribers.
You can also use the connect
method to instruct an Observable to begin emitting items (or, to begin generating items that would be emitted) even before any Subscriber has subscribed to it. In this way you can turn a cold Observable into a hot one.
Sample Code
var interval = Rx.Observable.interval(1000);
var source = interval
.take(2)
.do(function (x) { console.log('Side effect'); });
var published = source.publish();
published.subscribe(createObserver('SourceA'));
published.subscribe(createObserver('SourceB'));
// Connect the source
var connection = published.connect();
function createObserver(tag) {
return Rx.Observer.create(
function (x) { console.log('Next: ' + tag + x); },
function (err) { console.log('Error: ' + err); },
function () { console.log('Completed'); });
}
Side effect
Next: SourceA0
Next: SourceB0
Side effect
Next: SourceA1
Next: SourceB1
Completed
Completed
connect
is found in the following packages:
rx.all.js
rx.all.compat.js
rx.all.binding.js
connect
requires one of the following packages:
rx.js
rx.compat.js
rx.lite.js
rx.lite.compat.js