Skip to content

Instantly share code, notes, and snippets.

@edjafarov
Created May 16, 2012 12:30
Show Gist options
  • Save edjafarov/2709982 to your computer and use it in GitHub Desktop.
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
<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>
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);
@kuddl
Copy link

kuddl commented May 16, 2012

impressive !! thanks 4 sharing!!

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