Created
December 8, 2011 16:11
-
-
Save esatterwhite/1447456 to your computer and use it in GitHub Desktop.
Live Twitter feed for Node.js, spark & mootools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <title>Socket.IO Example</title> | |
| <script src="http://apollo.dyn.mke.corvisa.com:8080/static/js/mootools.js"></script> | |
| <script src="http://apollo.dyn.mke.corvisa.com:4000/socket.io/socket.io.js"></script> | |
| <style> | |
| html,body{background:#888;} | |
| h1{color:#FF9900;font-family:'Trebuchet MS',helvetica, arial, sans-serif;border-bottom:1px solid #333; padding:6px;margin-bottom:4px;text-shadow:#333 0 1px 0;} | |
| ul{margin:0;padding:0;} | |
| #tweets li{ | |
| background-color:#eee; | |
| padding:4px; | |
| -webkit-border-radius:4px; | |
| -moz-border-radius:4px; | |
| border-radius:4px; | |
| -webkit-box-shadow:#000 1px 1px 3px inset; | |
| -moz-box-shadow:#000 1px 1px 3px inset; | |
| box-shadow:#000 1px 1px 3px inset; | |
| list-style:none; | |
| margin-bottom:5px; | |
| } | |
| .username{ font-size:small; color:#666;} | |
| .tweet{font-weight:bold; font-size:xx-small;} | |
| </style> | |
| <script> | |
| var Queue = new Class({ | |
| Implements:[Events,Options], | |
| options:{ | |
| onPop:Function.from(), | |
| onPush:Function.from(), | |
| onEmpty:Function.from(), | |
| interval:5000 | |
| }, | |
| initialize: function( options ){ | |
| this._queue = []; | |
| this.setOptions( options ); | |
| this.addEvent('empty', function(){ | |
| clearInterval( this.intervalID ); | |
| this.intervalID = null; | |
| }); | |
| this.watch(); | |
| }, | |
| pop: function(){ | |
| var next, Q; | |
| next = this._queue.shift(); | |
| if( !!next ){ | |
| Q = this._queue.clone(); | |
| this.fireEvent('pop', next, Q ); | |
| } else { | |
| this.fireEvent('empty'); | |
| } | |
| }.protect(), | |
| push: function( items ){ | |
| var arr = Array.from( items ) | |
| this._queue.append( arr ); | |
| if( !this.intervalID ){ | |
| this.watch(); | |
| } | |
| }, | |
| updateFn:function(){ | |
| this.pop(); | |
| }, | |
| watch: function(){ | |
| if( !this.intervalID ){ | |
| this.intervalID = this.updateFn.periodical( this.options.interval,this ); | |
| } | |
| } | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| <h1>Tweets</h1> | |
| <ul id="tweets"> | |
| </ul> | |
| <script> | |
| var socket = io.connect('http://apollo.dyn.mke.corvisa.com:4000'); | |
| var tweetEl = document.id( 'tweets' ); | |
| var Q = new Queue({ | |
| interval:1000, | |
| onPop: function( tweet, queue ){ | |
| var li = new Element('li'); | |
| new Element('div', { | |
| 'class':'username' | |
| ,text:tweet.user.name | |
| ,styles:{ | |
| color:tweet.user.profile_link_color | |
| } | |
| }).inject( li ); | |
| new Element('div',{ | |
| 'class':'tweet' | |
| ,text:tweet.text | |
| ,styles:{ | |
| color:tweet.user.profile_text_color | |
| } | |
| }).inject( li ); | |
| li.inject( tweetEl , 'top'); | |
| li.highlight(); | |
| console.log( tweet ); | |
| } | |
| }); | |
| socket.on('news', function( data ){ console.log( "You have a connection!" )}) | |
| socket.on('tweet', function( data ){ | |
| Q.push( data ); | |
| }) | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* this is meant to be used with Dave Glass` spark2 | |
| * which handles the binding to a port, spawning child processes | |
| * and load balancing | |
| * | |
| * written for node -v 0.4.12 | |
| * | |
| * npm install socket.io | |
| * npm install -g spark2 | |
| * spark2 -p 4000 -n 3 -E development -v --respawn | |
| * | |
| * file must be called app.js or server.js for spark2 | |
| */ | |
| var http = require('http') | |
| ,app = http.createServer( handler ) | |
| ,io = require('socket.io').listen( app ) | |
| ,events = require('events') | |
| ,fs = require('fs'); | |
| module.exports = app | |
| var twitter_client = http.createClient(80,'api.twitter.com'); | |
| var tweet_emitter = new events.EventEmitter(); | |
| function handler( req, res ){ | |
| fs.readFile(__dirname + '/index.html', function( err, data ){ | |
| if( err ){ | |
| res.writeHead( 500 ); | |
| return res.end("Error loading index.html"); | |
| } | |
| res.writeHead( 200 ); | |
| res.end( data ); | |
| }) | |
| } | |
| function get_tweets(){ | |
| var request = twitter_client.request("GET", "/1/statuses/public_timeline.json",{"host":"api.twitter.com"}); | |
| request.addListener('response', function( response ){ | |
| var body = ""; | |
| response.addListener('data', function( data ){ | |
| body += data; | |
| }); | |
| response.addListener('end', function(){ | |
| var tweets = JSON.parse(body); | |
| if( tweets.length ){ | |
| tweet_emitter.emit("tweets", tweets); | |
| } | |
| }); | |
| }); | |
| request.end(); | |
| }; | |
| io.sockets.on('connection', function( socket ){ | |
| socket.emit( 'news', {hello:'world'}); | |
| }); | |
| setInterval(get_tweets, 3000); | |
| tweet_emitter.on( "tweets", function( tweets){ | |
| io.sockets.emit('tweet', tweets); | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment