How can I create an Observable that I can just directly push events to, like with Bacon.js's Bus?
Asked
Active
Viewed 2,197 times
4
-
Please, try to avoid using Bus (and Subject in Rx). http://baconjs.blogspot.fi/2014/12/bus-of-doom.html – Niko Ruotsalainen Jan 10 '15 at 12:50
1 Answers
12
The equivalent in RxJS is called Subject. It is both an Observer and an Observable. So to push events to it, you use it's Observer interface: the onNext, onError, and onCompleted methods. Then you can subscribe to it, map, zip, filter it like any Observable. Here's an example from the official docs:
var subject = new Rx.Subject();
var subscription = subject.subscribe(
function (x) { console.log('onNext: ' + x); },
function (e) { console.log('onError: ' + e.message); },
function () { console.log('onCompleted'); });
subject.onNext(1);
// => onNext: 1
subject.onNext(2);
// => onNext: 2
subject.onCompleted();
// => onCompleted
subscription.dispose();
You can check the Subject getting started guide here, and the Subject API docs here.

Ramith Jayatilleka
- 2,132
- 16
- 25