Live Twitter Stream Using Raspberry Pi and NodeJS
13 Feb 2015What’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:
- Raspberry Pi
- Twitter Streaming API access
- NodeJS (Install Here)
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.
Then click on the “Keys and Access Tokens” tab:
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”.
Afterwards you should see the newly generated Access Token and Access Token Key:
Now we have the following Twitter API variables:
- Consumer Key
- Consumer Secret
- Access Token
- Access Token Secret
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.
I just accepted all the defaults:
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.
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:
Now exit nano by hitting “ctrl+x” and type “y” and hitting enter. Then you can run the node script by typing:
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:
Conclusion
Some of the hashtags I stream are #spacex, #nasa, and #tesla. What are some hashtags that you would follow?