node-twitter aims to provide a complete, asynchronous client library for Twitter (and other compliant endpoints), including REST, stream and search APIs. It was inspired by, and uses some code from, technoweenie's twitter-node.
You can install node-twitter and its dependencies with npm: npm install twitter
.
- node v0.2+
- node-oauth
- cookie-node
It's early days for node-twitter, so I'm going to assume a fair amount of knowledge for the moment. Better documentation to come as we head towards a stable release.
var sys = require('sys'),
twitter = require('twitter');
var twit = new twitter({
consumer_key: 'STATE YOUR NAME',
consumer_secret: 'STATE YOUR NAME',
access_token_key: 'STATE YOUR NAME',
access_token_secret: 'STATE YOUR NAME'
});
The convenience APIs aren't finished, but you can get started with the basics:
twit.get('/statuses/show/27593302936.json', {include_entities:true}, function(data) {
sys.puts(sys.inspect(data));
});
Note that all functions may be chained:
twit
.verifyCredentials(function (data) {
sys.puts(sys.inspect(data));
})
.updateStatus('Test tweet from node-twitter/' + twitter.VERSION,
function (data) {
sys.puts(sys.inspect(data));
}
);
twit.search('nodejs OR #node', function(data) {
sys.puts(sys.inspect(data));
});
The stream() callback receives a Stream-like EventEmitter:
twit.stream('statuses/sample', function(stream) {
stream.on('data', function (data) {
sys.puts(sys.inspect(data));
});
});
node-twitter also supports user and site streams:
twit.stream('user', {track:'nodejs'}, function(stream) {
stream.on('data', function (data) {
sys.puts(sys.inspect(data));
});
// Disconnect stream after five seconds
setTimeout(stream.destroy, 5000);
});
- Jeff Waugh (author)
- rick (parser.js and, of course, twitter-node!)
- Complete the convenience functions, preferably generated
- Support recommended reconnection behaviour for the streaming APIs
- Should probably implement basic auth for non-Twitter endpoints