Skip to content

Instantly share code, notes, and snippets.

@mrsteveheyes
Created August 26, 2011 15:58
Show Gist options
  • Save mrsteveheyes/1173743 to your computer and use it in GitHub Desktop.
Save mrsteveheyes/1173743 to your computer and use it in GitHub Desktop.
Twitter Node.JS Client
// 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");
// 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");
var redis = require("redis"),
client1 = redis.createClient(),
client2 = redis.createClient(),
client3 = redis.createClient(),
client4 = redis.createClient(),
msg_count = 0;
redis.debug_mode = false;
client1.on("psubscribe", function (pattern, count) {
console.log("client1 psubscribed to " + pattern + ", " + count + " total subscriptions");
client2.publish("channeltwo", "Me!");
client3.publish("channelthree", "Me too!");
client4.publish("channelfour", "And me too!");
});
client1.on("punsubscribe", function (pattern, count) {
console.log("client1 punsubscribed from " + pattern + ", " + count + " total subscriptions");
client4.end();
client3.end();
client2.end();
client1.end();
});
client1.on("pmessage", function (pattern, channel, message) {
console.log("("+ pattern +")" + " client1 received message on " + channel + ": " + message);
msg_count += 1;
if (msg_count === 3) {
client1.punsubscribe();
}
});
client1.psubscribe("channel*");
var redis = require("redis"),
client1 = redis.createClient(),
client2 = redis.createClient(),
client3 = redis.createClient(),
client4 = redis.createClient(),
msg_count = 0;
redis.debug_mode = false;
client1.on("psubscribe", function (pattern, count) {
console.log("client1 psubscribed to " + pattern + ", " + count + " total subscriptions");
client2.publish("channeltwo", "Me!");
client3.publish("channelthree", "Me too!");
client4.publish("channelfour", "And me too!");
});
client1.on("punsubscribe", function (pattern, count) {
console.log("client1 punsubscribed from " + pattern + ", " + count + " total subscriptions");
client4.end();
client3.end();
client2.end();
client1.end();
});
client1.on("pmessage", function (pattern, channel, message) {
console.log("("+ pattern +")" + " client1 received message on " + channel + ": " + message);
msg_count += 1;
if (msg_count === 3) {
client1.punsubscribe();
}
});
client1.psubscribe("channel*");
// 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: "mrsteveheyes",
password: "n0h4sand",
track: ["#nodetwitter"]};
var io = require('socket.io').listen(app),
twitter = new (require("twitter-node").TwitterNode)(config);
io.addListener("clientMessage", function(socket){
console.log("heard by app");
socket.broadcast.emit('twit', tweet);
});
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
console.log("heard by twitter \n");
io.emit('clientMessage', 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();
// 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");
// 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: "mrsteveheyes",
password: "n0h4sand",
track: ["#nodetwitter"]};
var io = require('socket.io').listen(app),
twitter = new (require("twitter-node").TwitterNode)(config);
io.addListener("clientMessage", function(socket){
console.log("heard by app");
socket.broadcast.emit('twit', tweet);
});
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
console.log("heard by twitter \n");
io.emit('clientMessage', 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();
// 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