Created
September 18, 2011 15:09
-
-
Save gjohnson/1225151 to your computer and use it in GitHub Desktop.
twitter stream - NCDEVCON
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> | |
</head> | |
<body> | |
<div id="tweets"></div> | |
<!-- | |
Since we bound the socket.io library to our web server, it will know about this file. | |
--> | |
<script src="http://localhost:8000/socket.io/socket.io.js"></script> | |
<script> | |
// connect to our websocket server, need to tell it which port | |
var socket = io.connect('http://localhost', { port: 8000 }); | |
// wait for tweet events | |
socket.on('tweet', function (data) { | |
$('<p></p>').text(data.message).appendTo('#tweets'); | |
}); | |
</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
/** | |
* Net module is needed to create a tcp server. | |
*/ | |
var net = require('net'); | |
/** | |
* Http module for creating web servers. | |
*/ | |
var http = require('http'); | |
/** | |
* System module for misc. things | |
*/ | |
var sys = require('sys'); | |
/** | |
* Fs module for reading the file system. | |
*/ | |
var fs = require('fs'); | |
/** | |
* Twitter module for accessing twitter api. | |
* install it via `npm install twitter` | |
*/ | |
var twitter = require('twitter'); | |
/** | |
* Websocket module for realtime goodness. | |
* install via `npm install socket.io` | |
*/ | |
var io = require('socket.io'); | |
/** | |
* The port to bind our server to. It can be set via | |
* the environment variable, or defaulted. | |
*/ | |
var SOCKET_PORT = process.env.SOCKET_PORT || 8124; | |
/** | |
* The port we want our web server on. Same deal as above. | |
*/ | |
var WEB_PORT = process.env.WEB_PORT || 8000; | |
/** | |
* Create connection to twitter. Put your api keys here. | |
*/ | |
var tweets = new twitter({ | |
consumer_key: '', | |
consumer_secret: '', | |
access_token_key: '', | |
access_token_secret: '' | |
}); | |
/** | |
* Holds our connected clients | |
*/ | |
var sockets = []; | |
/** | |
* Create a simple tcp server | |
*/ | |
var server = net.createServer(function(conn) { | |
// keeps track of our connected clients | |
sockets.push(conn); | |
// when the client disconnected, we need to remove it | |
// other wise there will be a dead socket and when we | |
// attempt to write to it, our server will crash | |
conn.on('end', function() { | |
delete sockets[conn]; | |
}); | |
}); | |
/** | |
* Bind to our port | |
*/ | |
server.listen(SOCKET_PORT); | |
/** | |
* Create a simple http server to host our page and websockets. | |
*/ | |
var web = http.createServer(function(req, res) { | |
fs.readFile(__dirname + '/index.html', 'utf8', function(err, contents) { | |
res.writeHead(200, { 'Content-type': 'text/html' }); | |
res.end(contents); | |
}); | |
}); | |
/** | |
* Bind to our port. | |
*/ | |
web.listen(WEB_PORT); | |
/** | |
* Tell the web socket module to listen with our web server. | |
*/ | |
io = io.listen(web); | |
/** | |
* Tweet stream. This probably is not the right way to do this... any suggestions? | |
*/ | |
tweets.stream('statuses/filter', { track: 'ncdevcon' }, function(stream) { | |
// data is streamed in and will emit the `data` event whenever new data arrives. | |
stream.on('data', function(data) { | |
// the stuff we want from the message | |
var message = data.user.screen_name + ': ' + data.text; | |
sys.puts(message + '\n'); | |
// push it out to all of your connected net clients | |
sockets.forEach(function(sock) { | |
sock.write(message + '\n'); | |
}); | |
// now push it out to your web clients too under the "tweet" event | |
io.sockets.emit('tweet', { | |
message: message | |
}); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment