This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->
node logo

Hello everyone! This is the seventh post in my new node.js modules you should know about article series.

The first post was about dnode - the freestyle rpc library for node, the second was about optimist - the lightweight options parser for node, the third was about lazy - lazy lists for node, the fourth was about request - the swiss army knife of HTTP streaming, the fifth was about hashish - hash combinators library, the sixth was about read - easy reading from stdin.

This time I'll introduce you to ntwitter - asynchronous Twitter REST, streaming and searching client API. This module is maintained by Charlie McConnell, aka AvianFlu. Ntwitter was actually created by technoweenie, but since then maintainers have changed several times.

To use it, you'll need to get the API keys. That can easily be done at dev.twitter.com. Just register a new App, and you'll get the keys.

Here is an example ntwitter app that tweets:

var twitter = require('ntwitter');

var twit = new twitter({
  consumer_key: 'Twitter',
  consumer_secret: 'API',
  access_token_key: 'keys',
  access_token_secret: 'go here'
});

twit
  .verifyCredentials(function (err, data) {
    if (err) {
      console.log("Error verifying credentials: " + err);
      process.exit(1);
    }
  })
  .updateStatus('Test tweet from ntwitter/' + twitter.VERSION,
    function (err, data) {
      if (err) console.log('Tweeting failed: ' + err);
      else console.log('Success!')
    }
  );

Here is how to search through ntwitter API:

var twitter = require('ntwitter');

var twit = new twitter({
  consumer_key: 'Twitter',
  consumer_secret: 'API',
  access_token_key: 'keys',
  access_token_secret: 'go here'
});

twit.search('nodejs OR #node', function(err, data) {
  if (err) {
    console.log('Twitter search failed!');
  }
  else {
    console.log('Search results:');
    console.dir(data);
  }
});

You can also use Twitter's streaming API:

var twitter = require('ntwitter');

var twit = new twitter({
  consumer_key: 'Twitter',
  consumer_secret: 'API',
  access_token_key: 'keys',
  access_token_secret: 'go here'
});

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

Here is a list of all the possible statuses/* that you can stream from.

Note how you don't need to login into twitter to do searching and streaming.

You can stream someone's tweets this way:

var twitter = require('ntwitter');

var twit = new twitter({
  consumer_key: 'Twitter',
  consumer_secret: 'API',
  access_token_key: 'keys',
  access_token_secret: 'go here'
});

twit.stream('user', {track:'nodejs'}, function(stream) {
  stream.on('data', function (data) {
    console.dir(data);
  });
  stream.on('end', function (response) {
    // Handle a disconnection
  });
  stream.on('destroy', function (response) {
    // Handle a 'silent' disconnection from Twitter, no end/error event fired
  });
});

Finally, you can stream tweets from certain locations, defined by bounding boxes:

var twitter = require('ntwitter');

var twit = new twitter({
  consumer_key: 'Twitter',
  consumer_secret: 'API',
  access_token_key: 'keys',
  access_token_secret: 'go here'
});

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

You can use Cole Gillespie's node-finden module to get the geographical coordinates for bounding boxes. Here is how Cole's software looks:

That's it. Super easy way to do all things twitter with ntwitter.

You can install ntwitter through npm as always:

npm install ntwitter

Ntwitter on GitHub: https://github.com/AvianFlu/ntwitter.

Sponsor this blog series!

Doing a node.js company and want your ad to appear in the series? The ad will go out to 14,000 rss subscribers, 7,000 email subscribers, and it will get viewed by thousands of my blog visitors! Email me and we'll set it up!

Enjoy!

If you love these articles, subscribe to my blog for more, follow me on Twitter to find about my adventures, and watch me produce code on GitHub!

This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->

Comments

nisc Permalink
December 10, 2011, 03:24

This is my favorite in this series so far!

nisc Permalink
December 10, 2011, 04:28

PS: It should probably be noted that the search API doesn't require any tokens or secrets.

December 11, 2011, 22:10

Good thinking! Adding a note.

April 02, 2012, 19:32

I'm new to node so thanks for the series. I'm building an app that needs to be continuously connected 24/7 to the streaming API so I have been looking for a node module that supports auto-reconnecting to the stream API. I don't think ntwitter does, are you aware of any that do?

September 14, 2013, 19:12

Merrick G.I. Smoked Beefy Jerky Bone 4-6″, 4-Pack By:

Ivan Garcia Martinez Permalink
October 08, 2012, 22:38

hi! i am doing an app with this lib, and i have a doubt,
how many words can i put in the track when I use the method statuses / filter

How I can use this method several times inside the application?

thank a lot

guest Permalink
February 13, 2013, 13:32

How do I create automatic listener to show me the updates when they arrive?
I would liek to have search set for particular tag, and when there is a new search result I get notified

October 28, 2013, 04:31

It appears that ntwitter doesn't work anymore. Streaming works, but not search.

Annee Permalink
March 03, 2014, 05:44

I really impress of your great work and you deserve for appreciating and uk bets essay available students.

March 20, 2014, 02:42

I was very happy that I discovered this website. dobre kredyty I needed to thank you for this excellent information . I have bookmark this page and after work I read interesting things to update myself and to refresh my mind

Hodges Permalink
March 27, 2014, 21:06

I like this mathematics and http://www.usessay.com/thesis-writing this is helpful.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the first letter of your name: (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements