Skip to content

Instantly share code, notes, and snippets.

@codesplicer
Created August 30, 2011 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codesplicer/1181006 to your computer and use it in GitHub Desktop.
Save codesplicer/1181006 to your computer and use it in GitHub Desktop.
Streaming twitter API + socket.io
// SETUP
// Get the required modules
var express = require("express"),
mustachio = require("mustachio"),
sys = require("sys"),
app = express.createServer();
// Configure the app
app.configure(function() {
app.use(express.static(__dirname + '/public'));
app.register('.mustache', mustachio);
app.set('view engine', 'mustache');
});
// CONFIG FOR TWITTER
var config = {
user: "",
password: "",
track: ["#nodetwitter"]};
var socket = require('socket.io').listen(app),
twitter = new (require("twitter-node").TwitterNode)(config);
socket.addListener('clientMessage', function(data, client){
client.sockets.send(data);
});
twitter
.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.emit('clientMessage', tweet, socket);
})
.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();
// END SETUP
// CONTROLLERS
app.get("/", function(req, res){
// Render the layout
res.render('layout', {
title: "Amy and Steve Wedding"
});
});
// END CONTROLLERS
// GO GO GO!
app.listen(3000);
console.log("HTTP Server Started");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment