-
Notifications
You must be signed in to change notification settings - Fork 20
Part 1. PubNub Setup

The key to this entire project is PubNub. Their slogan, "Realtime Apps Made Simple", is true. They provide over 70 SDKs and have plenty of pre-connected services in the form of "Blocks". These blocks are basically bits of code that process your data before or after it's sent to it's destination - no server needed!
We will be using both the IBM Watson Sentiment Analysis block and the Initial State block. We will also be using PubNub's live Twitter stream.
Note: PubNub's Twitter stream only pulls in 50 messages a second ("only" 😂) compared to the 6,000 that actually come in. I chose to use it instead of the Twitter API because I'm already throttling the number of messages I send to IBM Watson's sentiment analysis to just 1,000/day to stay within their free trial guidelines (I was meeting this cap after about an hour of running the script unregulated). If you're interested in sifting through all of the tweets every second because you have a less popular keyword, or if you're paying for IBM's services, checkout the last section: BONUS - Part 7: The Unlimited Stream
First things first, you need to create a PubNub account. You can register here: https://admin.pubnub.com/#/register
Once you're in and on the main page, scroll down to create an app. Apps are basically treated like projects, so each one has unique keys and blocks. Enter the name you want for your app (I named mine "Current Emotion in US") and click "Create New App". Click on your new app!
You will be taken to the "Key Info" page for your app. We'll need these keys later! Click on the Key box (should be named "Free") to be taken to where you can enable features for said key set. Under the "Application add-ons" section, click on the slider next to "PubNub Blocks" to turn them on.
Click on the "Blocks" tab on the left hand side of the page. Here is where our Watson and Initial State code is going to go.
Create a block and name it - I just called mine "Sentiment". You should only have one key option because we didn't create any extra. Click "Create".
Click on your new block to be taken to its event handlers. Since you haven't created any yet, you will be prompted to.
Name this one "IBM Watson" and name the channel "sentiment-analysis". Select "Before publish or fire" as the type.
You'll be taken to a page with a code box and debug console. We are going to replace all of the code inside of the box with code for the IBM Watson: Sentiment and Context Analysis block. You can copy the old code here if using IBM's Alchemy API, but I suggest copying directly from PubNub's new block code. PubNub just updated this block to reflect the changes to the IBM Sentiment Analysis handler.
Click the save icon to keep your changes. We're going to add our Watson API key later.
Click the "+" next to the IBM Watson tab. We can now create the Initial State event handler. I named it "Initial State" with the channel name "initial-state-streamer". Select "After publish or fire" as the type.
Replace this handler's code with the code for the Initial State block. You can copy this code from the InitialState.js file.
Click the save icon to keep your changes. We're going to add our Initial State access key later.
Let's setup our IBM Bluemix account so we can use their sentiment analyzer!
Initial State (https://www.initialstate.com)
(c) 2018 Initial State Technologies, Inc.