public
Last active

Access the Twitter Streaming API with ease (Node.js).

  • Download Gist
twitter_streaming.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
var util = require('util'),
http = require('http'),
events = require('events');
 
var Twitter = function(opts) {
this.username = opts.username;
this.password = opts.password;
this.track = opts.track;
this.data = '';
};
 
Twitter.prototype = new events.EventEmitter;
 
Twitter.prototype.getTweets = function() {
var opts = {
host: 'stream.twitter.com',
port: 80,
path: '/1/statuses/filter.json?track=' + this.track,
method: 'POST',
headers: {
'Connection': 'keep-alive',
'Accept': '*/*',
'User-Agent': 'Example Twitter Streaming Client',
'Authorization': 'Basic ' + new Buffer(this.username + ':' + this.password).toString('base64'),
},
},
self = this;
 
this.connection = http.request(opts, function(response) {
response.setEncoding('utf8');
response.on('data', function(chunk) {
self.data += chunk.toString('utf8');
 
var index, json;
 
while((index = self.data.indexOf('\r\n')) > -1) {
json = self.data.slice(0, index);
self.data = self.data.slice(index + 2);
if(json.length > 0) {
try {
self.emit('tweet', JSON.parse(json));
} catch(e) {
self.emit('error', e);
}
}
}
});
});
 
this.connection.write('?track=' + this.track);
this.connection.end();
};
 
module.exports = Twitter;

Any chance of an example for us ignorant people just starting out with Node?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.