Posts Tagged ‘nodejs’
Stream Tweets in MongoDB with Node.JS
July 5, 2013
1 comment
Suppose we want store al our “mongodb” tweets in a MongoDB database.
We need 2 additional node packages:
1) ntwitter (Asynchronous Twitter REST/stream/search client API for Node.js)
2) mongodb (A Node.js driver for MongoDB). Of course there are more MongoDB drivers.
Create a Node.js project “twitterstream” and add the 2 packages with the following commands:
$ npm install ntwitter $ npm install mongodb
We need an existing twitter account and make a credential file for example credentials.js.
var credentials = { consumer_key: '3h7ryXnH209mHNWvTgon5A', consumer_secret: 'tD5OdqXw1qbDMrFbrtPIRRl4fEyUsKFXT2kZLQaMpVA', access_token_key: '474665342-wuRquALXNQZPYABUiOnXCmVSxyU2LIinV6VwpWMW', access_token_secret: 'k01HuXdl8umwt5rZcDDk0OgQJbhkiFlPv2dCAmHXQ' }; module.exports = credentials;
And now we create the main file twitter.js with the following code:
var twitter = require('ntwitter'); var credentials = require('./credentials.js'); var t = new twitter({ consumer_key: credentials.consumer_key, consumer_secret: credentials.consumer_secret, access_token_key: credentials.access_token_key, access_token_secret: credentials.access_token_secret }); var mongo = require('mongodb'); var Server = mongo.Server, Db = mongo.Db, assert = require('assert') BSON = mongo.BSONPure; var server = new Server('localhost', 27017, {auto_reconnect: true}); db = new Db('twitterstream', server); // open db db.open(function(err, db) { assert.equal(null, err); t.stream( 'statuses/filter', { track: ['mongodb'] }, function(stream) { stream.on('data', function(tweet) { db.collection('streamadams', function(err, collection) { collection.insert({'tweet': tweet.text, {safe:true} , function(err, result) {}); }); }); } ); });
Simply start the twitter.js with:
$ node twitter.js
Succes with Node.JS and MongoDB!