Live Twitter Stream Using Raspberry Pi and NodeJS

What’s the Purpose?

The main use case was I wanted to see some geofenced Twitter tweets and/or some relevant keyword terms. Getting a bird’s eye view of some relevant tweets seemed very interesting to me.

What do we need?

With this tutorial we will need the following:

Initial Setup

First we need to have access to Twitter’s Streaming API. For that you will need to create an “Application” in Twitter’s dev website. You can create a new one by logging in and visiting Create an Application - Twitter. Supply an Application name, Description and Website. Then agree to the terms.

Twitter API Signup Page

Then click on the “Keys and Access Tokens” tab:

Twitter Key and Secret Page

We now have our Consumer Key and Secret. Now it’s time to generate an Access Token and Access Token Secret. Click on “Create my access token”.

Your Access Token

Afterwards you should see the newly generated Access Token and Access Token Key:

Your Access Token

Now we have the following Twitter API variables:

Setup our initial project

We will first create a folder where our project files will reside. Then we will initialize the project by using npm init.

mkdir ~/twitter_stream/;
cd ~/twitter_stream/;
npm init;

I just accepted all the defaults:

npm init

Install the dependencies

Now some fun. We get to install the dependencies our NodeJS app will use. The only one we will be using right now is ttezel’s twit Node Twitter API. In order to install it for your project you’ll use the npm command.

npm install twit --save

This will do two things. Install the client and all it’s dependencies and also saves the dependency for your app inside the “package.json” file for later use.

Start the script

You may have already noticed on ttezel’s twit GitHub page there are already TONS of examples in the README.md file.

Lets start by creating a file named index.js. And placing the following script inside the file:

var Twit = require('twit')

var T = new Twit({
    consumer_key:         'sUqQCLB5ufakekeyUT3xNew'
  , consumer_secret:      'SVE8gv1Uz0zpoaQRpTUADBcvESGk7j49CalsoFakegV9dU3Rhh'
  , access_token:         '159618721-anotherFakeTokenUoEgaww2SBkpnAp3IXmTcZQUEp32o'
  , access_token_secret:  '5XtPnLUDEtokenSecretpMdTWF5g8OXIYoQlHBAsZJ'
})

//
//  filter the twitter public stream by the hashtag '#raspberrypi' and keyword
//  'raspberry pi'
//
var stream = T.stream('statuses/filter', { track: ['#raspberrypi', 'raspberry pi'] });

stream.on('tweet', function (tweet) {
  console.log(tweet.text + ' (' + tweet.user.screen_name  + ')');
})

Now exit nano by hitting “ctrl+x” and type “y” and hitting enter. Then you can run the node script by typing:

node index.js

You should hopefully be seeing a live stream of tweets with the hashtag “#raspberrypi” and keyword “RaspberryPi” in them. Exciting! You can leave this up and get (almost) real-time feed of tweets about the Raspberry Pi. Now you can stay up to date with more Raspberry Pi information!

Live Stream of a specific Twitter User

Suppose you want to stalk your favorite Twitter account. My favorite Twitter account right now is WHY MY CAT IS SAD. We would first need to find out the account’s User ID by using the following website: GET TWITTER ID. Using that service I found the account’s User ID is 1083314617. We can then rewrite our code to just pull in those tweets:

var Twit = require('twit')

var T = new Twit({
    consumer_key:         'sUqQCLB5ufakekeyUT3xNew'
  , consumer_secret:      'SVE8gv1Uz0zpoaQRpTUADBcvESGk7j49CalsoFakegV9dU3Rhh'
  , access_token:         '159618721-anotherFakeTokenUoEgaww2SBkpnAp3IXmTcZQUEp32o'
  , access_token_secret:  '5XtPnLUDEtokenSecretpMdTWF5g8OXIYoQlHBAsZJ'
})

//
//  filter the twitter public stream by the hashtag '#raspberrypi'.
//
var stream = T.stream('statuses/filter', { follow: '1083314617' });

stream.on('tweet', function (tweet) {
  console.log(tweet.text + ' (' + tweet.user.screen_name  + ')');
});

Conclusion

Some of the hashtags I stream are #spacex, #nasa, and #tesla. What are some hashtags that you would follow?