Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Last active April 17, 2019 21:40
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stephenlb/36aef15a165d5bad0d82 to your computer and use it in GitHub Desktop.
Save stephenlb/36aef15a165d5bad0d82 to your computer and use it in GitHub Desktop.
Twitter / PubNub Bridge - Heroku Worker

Heroku Worker Add

It's easy to spin up a Heroku Workers. Here is a set of heroku commands to approach your goal.

heroku keys:add
heroku login
heroku git:clone -a twitter-pubnub-stream
git push heroku master
heroku ps:scale twitter-pubnub-stream=1

Restart

heroku login
heroku git:clone -a twitter-pubnub-stream
cd twitter-pubnub-stream
heroku ps:scale twitter-pubnub-stream=0
heroku ps:scale twitter-pubnub-stream=1
{
"name": "twitter-pubnub-stream",
"version": "1.0.0",
"dependencies": {
"nconf": "^0.6.9",
"pubnub": "^3.6.7",
"spotify-web-api-node": "0.0.11",
"twit": "^1.1.18"
},
"engines": {
"node": "0.10.x",
"npm": "1.2.x"
}
}
tweetstream: node twitter-pubnub-stream.js
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Dependancies and Setup
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
var twitterapi = require('twit');
var pubnub = require('pubnub');
var nconf = require('nconf');
var last_tweet = {};
var tweet_ready = true;
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Load Configuration File
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
nconf.file({ file: 'config.json' }).env();
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Twitter Setup
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
var twitter = new twitterapi({
consumer_key : nconf.get('TWITTER_CONSUMER_KEY'),
consumer_secret : nconf.get('TWITTER_CONSUMER_SECRET'),
access_token : nconf.get('TWITTER_ACCESS_TOKEN'),
access_token_secret : nconf.get('TWITTER_TOKEN_SECRET')
});
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// PubNub Setup
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
twitter.pubnub = pubnub({
publish_key : nconf.get('PUBNUB_PUBLISH_KEY'),
subscribe_key : nconf.get('PUBNUB_SUBSCRIBE_KEY')
});
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Start Stream Service
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
twitter.start = function() {
// Connect to stream and filter by a geofence
// that is the size of the Earth
twitter.streamer = twitter.stream( 'statuses/filter', {
locations : '-180,-90,180,90'
} );
// When Tweet is Received
twitter.streamer.on( 'tweet', function(tweet) {
last_tweet = tweet;
} );
// Ready to go!
return { message : 'Stream created and started.' };
};
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Stop Service
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
twitter.stop = function() {
// Clear Stream
twitter.streamer.stop();
twitter.streamer = null;
// Return stopped
return { message : 'Stopped Stream' };
};
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Publishes Tweet object through PubNub to all clients
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
setInterval( publish, 20 );
setInterval( function() { tweet_ready = true }, 500 );
function publish() {
if (!last_tweet) return;
if (!tweet_ready) return;
tweet_ready = false;
twitter.pubnub.publish({
post : false,
channel : nconf.get('PUBNUB_CHANNEL'),
message : last_tweet,
callback : function() { tweet_ready = true }
});
last_tweet = null;
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Startup
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
twitter.start();
@jamessergeant
Copy link

Just curious as to the spotify dependency?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment