Skip to content

Instantly share code, notes, and snippets.

@jfrazee
Created January 10, 2012 23:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfrazee/1591854 to your computer and use it in GitHub Desktop.
Save jfrazee/1591854 to your computer and use it in GitHub Desktop.
Twitter Streaming API with Node.js Request module
var request = require('request');
function filter(options, callback){
var params = {
uri: "https://stream.twitter.com/1/statuses/filter.json",
}
if (typeof options['oauth'] !== 'undefined'){
params.oauth = options.oauth;
delete options.oauth;
}
else if (typeof options['basic'] !== 'undefined') {
params.uri = params.uri.replace(/^https?:\/\//,
'https://' + options.basic.username + ':' + options.basic.password + '@'
);
delete options.basic;
}
params.form = options;
var req = request.post(params, function(err, response, body){
if (err) console.error(err);
});
// The callback for request.post is called when the response returns so you
// have to save the post object and add a callback for the data event.
req.on('data', function(buffer){
callback(JSON.parse(buffer.toString()));
});
};
filter({ basic: { username: '', password: '' }, track: 'new hampshire' },
function(tweet) { console.log(tweet); }
);
@Lissy93
Copy link

Lissy93 commented Sep 11, 2015

4 years on and still really helpful - thanks.
Here's an updated version for the new Twitter API
See the repository readme for documentation :)

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