-
-
Save mailtruck/3088835 to your computer and use it in GitHub Desktop.
Twitter streaming API example twitter-node and socket.io
This file contains 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> | |
<head> | |
<title>Codesnippit NodeJS Twitter Tracker Client</title> | |
</head> | |
<body> | |
<ul></ul> | |
<script> | |
(function() { | |
var script = document.createElement("script"); | |
script.src = "http://code.jquery.com/jquery.min.js"; | |
script.onload = function() { | |
$.getScript("http://localhost:3000/socket.io/socket.io.js", function() { | |
var socket = new io.Socket("localhost", {"port": 3000}); | |
socket.on("message", function(json) { | |
data = JSON.parse(json); | |
$("<li></li>").text("@" + data.user.screen_name + " " + data.text).prependTo("ul"); | |
}); | |
socket.connect(); | |
}); | |
}; | |
document.getElementsByTagName("head")[0].appendChild(script); | |
})(); | |
</script> | |
</body> | |
</html> |
This file contains 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
var http = require('http'); | |
var config = { | |
user: "", | |
password: "", | |
track: ["ipl"]}, | |
sys = require('sys'); | |
server = http.createServer(function(req, res) { // create the server | |
res.writeHead(200, {'Content-Type': 'text/html'}); | |
res.end('<h1>Codesnippit Twitter Tracker Server is <span style="color: #bada55">operational</span></h1>'); | |
}); | |
server.listen(3000); | |
var socket = require('socket.io').listen(server), | |
twitter = new (require("twitter-node").TwitterNode)(config); | |
.addListener('error', function(error){ // Always check for errors or they popup client side | |
console.log(error.message); | |
}) | |
.addListener('tweet', function(tweet){ // A new tweet that matches the criteria has been located | |
socket.broadcast(JSON.stringify(tweet)); | |
}) | |
.addListener('limit', function(limit){ // New limit has been sent from the API | |
sys.puts('LIMIT: ' + sys.inspect(limit)); | |
}) | |
.addListener('delete', function(del){ // A delete event occured | |
sys.puts('DELETE: ' + sys.inspect(del)); | |
}) | |
.addListener('end', function(resp){ // API disconnect | |
sys.puts('wave goodbye...' + resp.statusCode); | |
}) | |
.stream(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment