twat

Simple Twitter Streaming for Node.js

Twat: Simple Twitter Streaming for Node.js

Twat is a heavily-modified version of AvianFlu's ntwitter, which is an improved version of jdub's node-twitter, which in turn was inspired by, and uses some code from, technoweenie's twitter-node.

The goal of Twat is to provide an extremely easy, consistent connection to Twitter's streaming API, and handle reconnection (including backoff strategies) automatically so you don't have to.

You can install Twat and its dependencies with npm: npm install twat.

The keys listed below can be obtained from dev.twitter.com after setting up a new App.

var Twat = require('twat');
 
var twit = new Twat({
  consumer_key: 'Twitter',
  consumer_secret: 'API',
  access_token: 'keys',
  access_token_secret: 'go here'
});

The stream() callback receives a Stream-like EventEmitter.

Here is an example of how to call the statuses/sample method:

twit.stream('statuses/sample', function(stream) {
  stream.on('tweet', function(tweet) {
    console.log(tweet);
  });
});

Here is an example of how to call the 'statuses/filter' method with a bounding box over San Fransisco and New York City ( see streaming api for more details on locations ):

twit.stream('statuses/filter', {'locations':'-122.75,36.8,-121.75,37.8,-74,40,-73,41'}, function(stream) {
  stream.on('tweet', function(tweet) {
    console.log(tweet);
  });
});

Twat also supports user and site streams:

twit.stream('user', {track:'nodejs'}, function(stream) {
  stream.on('tweet', function (tweet) {
    console.log(tweet);
  });
  stream.on('end', function(response) {
    // Handle a disconnection 
  });
  stream.on('destroy', function(response) {
    // Handle a 'silent' disconnection from Twitter, no end/error event fired 
  });
  // Disconnect stream after five seconds 
  setTimeout(stream.destroy, 5000);
});

To get tweets from the stream:

twit.stream('statuses/sample', function(stream) {
  stream.on('tweet', function(tweet) {
    console.log(tweet);
  });
});

Twat will emit an error if there is a problem with the stream (auto-reconnect or not)

twit.stream('statuses/sample', function(stream) {
  stream.on('error', function(typeinfo) {
    console.log(type + " " + info);
  });
});

When Twat begins auto reconnection, it will emit reconnect status for every attempt

twit.stream('statuses/sample', function(stream) {
  stream.on('reconnect', function(info) {
    console.log(info.error);    // The error causing reconnection 
    console.log(info.attempts); // Number of reconnects attempted 
  });
});

If Twitter sends a FIN packet to end the open connection, Twat emits an end

twit.stream('statuses/sample', function(stream) {
  stream.on('end', function(response) {
    console.log(response);
  });
});

When the connection to Twitter ends, Twat also emits a destroy to signal the socket has closed

twit.stream('statuses/sample', function(stream) {
  stream.on('destroy', function(msg) {
    console.log(msg);
  });
});

Lots of people contribute to this project. You should too!