With the Twitter stream API, you can only open one stream at a time. So, if you have multiple filters, the tweets in result will be mixed up, you'll need to do some post-processing.
This module lets you open multiple channels with there own filters, on the same stream. And then, you can add events to each of them individually, to listen to there results, like if you had open multiple streams.
twitter-stream-channels handles all the post-processing, the connexion layer (OAuth, etc ...) is handled by twit. See the FAQ about both topics.
You can see Topheman Datavisual which is a project I made, using this module for the Twitter Stream part.
##Installation
Just run npm install twitter-stream-channels
##Example :
file my.twitter.credentials.json
{
"consumer_key": "XXXXX",
"consumer_secret": "XXXXX",
"access_token": "XXXXXX",
"access_token_secret": "XXXXX"
}
example
var TwitterStreamChannels = require('twitter-stream-channels');
var credentials = require('./my.twitter.credentials.json');
var client = new TwitterStreamChannels(credentials);
var channels = {
"languages" : ['javascript','php','java','python','perl'],
"js-frameworks" : ['angularjs','jquery','backbone','emberjs'],
"web" : ['javascript','nodejs','html5','css','angularjs']
};
var stream = client.streamChannels({track:channels});
stream.on('channels/languages',function(tweet){
console.log('>languages',tweet.text);
});
stream.on('channels/js-frameworks',function(tweet){
console.log('>frameworks',tweet.text);
});
stream.on('channels/web',function(tweet){
console.log('>web',tweet.text);
});
setTimeout(function(){
stream.close();
console.log('>stream closed after 100 seconds');
},100000);
##API
You can find an API doc generated from the source code on http://labs.topheman.com/twitter-stream-channels/.
There are also examples in the repo, and the API is not that complicated ... But something that you could enjoy is the mocked version of the module that allows you to code without needing to connect to Twitter, since it has some connection limits over every 15 minutes (those limits are not greatly specified for the streaming API).
With this simple code, you retrieve your fake data (see also example) :
var credentials = require('./twitter.credentials.json');
require('twitter-stream-channels').launchMockDataRetriever(credentials,{
output: "./save/your/tweets.json",
track:['blue','white','yellow','green','orange','kiwi','apple','lemon','coconut','Luke','Leia','Han','Yoda'],
maxNumber:200,
timeout:50000
});
And then, you can use it like :
var TwitterStreamChannelsMocked = require('twitter-stream-channels').getMockedClass();
var client = new TwitterStreamChannelsMocked({
tweets: require('./save/your/tweets.json'),
singleRun: false
});
I use it for the unit tests of the module as well as when I code some application based on it.
##FAQ
####To what events can I subscribe ?
.on('channels')
: will listen to all the incoming tweets.on('channels/languages')
: will only listen to the tweets where the keywords from the channel "languages" were matched (like javascript, java, php, python or perl).on('keywords/angularjs')
: will only listen to the tweets where the keyword "angularjs" was matched
####Where do I find out the matched keywords and channels ?
Two attributes are added to the tweet you retrieve :
- $channels : a key/value object : key : name of the channel, value : array of keywords retrieved in this channel on this tweet
- $keywords : an array of all the keywords you're following that were matched on this tweet.
####The matched keywords are lower cased, why ?
Since the Twitter streaming filter API is case-insensitive, I lower case before matching, so the keywords matched you will find in tweet.$channels['yourChannel']
are lower cased.
####How much of post-processing does this cover ?
As specified in the Twitter streaming API :
The text of the Tweet and some entity fields are considered for matches. Specifically, the text attribute of the Tweet, expanded_url and display_url for links and media, text for hashtags, and screen_name for user mentions are checked for matches.
So I match your keywords not only against tweet.text but also against expanded_url
, display_url
and screen_name
when they are available (this is a work you won't have to bother to do ...)
####How to unsubscribe ?
The object returned by (new require('twitter-stream-channels')(credentials)).stremChannels(options)
extends the standard events.EventEmitter of nodejs, so you can use on()
, off()
, addEventListener()
...
####I can't connect to Twitter, or I have disconnections, how about that ?
- First : check your credentials.
- Second : have tried too many times to connect ? Wait a couple of minutes.
If this persists, you can file an issue. But know that the twitter-stream-channels modules doesn't handle itself the network layer. It relies on twit for this part, so check if that doesn't come from it. If twit is upgraded with fixes, I will upgrade my module.
####Can I also use this module for other parts of the Twitter API than only streams ?
You can do anything the Twitter API offers, via the twit client which is exposed by getApiClient(). Once you retrieved the root client, you can call the API exposed by twit to interract with Twitter.
##For contributors
- Installation :
- Once you've git cloned this repo, just
npm install
to install the dependencies. - If you don't have jasmine-node globally, run
sudo npm install jasmine-node@1.14.2 -g
(specified version - for the moment last version is buggy) to install the jasmine framework + test runner for node that will let you run the unit tests - If you plan to generate doc, and don't have yuidoc gobally, run
npm install yuidocjs -g
- If you want to execute the examples or online tests, copy
twitter.credentials.default.json
to twitter.credentials.json
, set your own credentials (you can get some at https://apps.twitter.com/app/new if you don't have any already)
- How to run the tests :
- offline tests (don't need AND won't create any connexion to Twitter) - run them as many time as you want :
- single run :
npm run test
- continuous :
npm run test-watch
(I set it up but don't use it for the moment)
- online tests (needs the connexion to Twitter to use the streming API) : make sure you set your credentials in the
twitter.credentials.json
file. Don't run them too many times (if you attempt too much connexion, your IP could be black listed from Twitter) :
- single run :
npm run test-online
(none for the moment and not so sure there should be any)
- How to generate the doc :
- Once :
- Make sure you have yuidoc, if not
npm install yuidocjs -g
- Install the theme dependencies :
cd extras/yuidoc-theme-blue
then npm install
- After that, at the root, just run
npm run yuidoc