Home > Big Data > Stream Tweets in MongoDB with Node.JS

Stream Tweets in MongoDB with Node.JS

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);

    { 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!

  1. dapo
    November 4, 2015 at 13:17

    collection.insert({‘tweet’: tweet.text, {safe:true},

    SyntaxError: Unexpected token {
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:414:25)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:134:18)
    at node.js:961:3

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: