Ok, so I\'m a complete beginner with Rx and unfortunately very new to js and streams in js as well. Im using this https://github.com/trygve-lie/twitter-stream-api to connect
Here's an example using desmondmorris/node-twitter and rxjs 5.
const Observable = require('rxjs').Observable;
Observable
.of(new require('twitter')({
consumer_key: 'xxxx',
consumer_secret: 'xxxx',
access_token_key: 'xxxx',
access_token_secret: 'xxxx',
})).mergeMap(twitter =>
Observable
.fromEvent(twitter.stream('statuses/filter', {
track: 'Stack Overflow'
}),
'data'))
.filter(tweet => tweet.user.follow_count > 10000)
.subscribe(console.log);
RxJS 5 doesn't have any methods to convert stream from/to an Observable so you'll need to do this by yourself. Ideally with Observable.create
.
const Rx = require('rxjs');
const Observable = Rx.Observable;
var TwitterStream = require('twitter-stream-api'),
...
var source$ = Observable.create(observer => {
var Twitter = new TwitterStream(keys);
Twitter.stream('statuses/filter', {
track: filter
});
Twitter.on('data', function (obj) {
observer.next(obj);
});
return () => {
Twitter.close();
};
});
This makes a cold Observable that'll connect to Twitter only when you subscribe to it. The Observable.create
static methods let's you push values to the observer and at the end return a tear down function then just closes the connection. This function is called when you unsubscribe or when the Observable completes.
Then you can chain this Observable with whatever you want:
source$.filter(...).map(...)
Note, that there're also methods Observable.bindCallback()
and Observable.bindNodeCallback()
but these wouldn't help you much in your situation.
Read more:
http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-create
https://medium.com/@benlesh/hot-vs-cold-observables-f8094ed53339#.scxgvqp49
https://medium.com/@benlesh/learning-observable-by-building-observable-d5da57405d87#.2099xwi13