Created
May 16, 2012 12:30
-
-
Save edjafarov/2709982 to your computer and use it in GitHub Desktop.
Minimum viable example of streaming twitter feed to browser using node.js 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
<script type="text/javascript" src="/socket.io/socket.io.js"></script> | |
<script type="text/javascript"> | |
var socket = io.connect("http://localhost"); | |
socket.on("twit", function(data){ | |
console.log(data); | |
var div = document.createElement("div"); | |
div.innerHTML = data.text; | |
document.body.insertBefore(div, document.body.childNodes[0]); | |
}) | |
</script> | |
<body> | |
<div>Hello world!</div> | |
</body> |
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 app = require("http").createServer(handler); | |
var io = require("socket.io").listen(app); | |
var observer = new (require("events").EventEmitter)(); | |
var fs = require("fs"); | |
io.sockets.on("connection", function(socket){ | |
observer.on("twit", function(data){ | |
socket.emit("twit", data); | |
console.log(data.text); | |
}) | |
}) | |
var twitter = require("ntwitter"); | |
var twit = new twitter({ "consumer_key":"YOURKEY", | |
"consumer_secret":"YOURSECRET", | |
"access_token_key":"YOURTOKENKEY", | |
"access_token_secret":"YOURTOKENSECRET"}); | |
//lets find out how much truth in the internet | |
twit.stream("statuses/filter", {track:"true"}, function(stream){ | |
stream.on("data", function(data){ | |
observer.emit("twit" , data); | |
}) | |
}) | |
function handler(req, res){ | |
fs.readFile(__dirname + "/twi.html", function(err, data){ | |
res.end(data); | |
}) | |
} | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
impressive !! thanks 4 sharing!!