skip to Main Content

I have two functions that query the twitter api. The query is done through a web interface and I call each function when a checkbox is on. My problem now is after all the querying has been done, I want to be able to store the data and send it back to the web interface. How do I do this ?

    if (string.checktweet1 == 'on') {
      tweets(string.teamname)
    }
    if (string.checkmentions1 == 'on'){
      mentions(string.teamname)
    }
    if (string.checktweet2 == 'on'){
      tweets(string.playername)
    }

    if (string.checkmentions2 == 'on'){
      mentions(string.playername)
    }

      function mentions(x){
         client.get('search/tweets', {q:x, count:1},
         function (err,data,){
            for(var index in data.statuses){
               var tweet = data.statuses[index];
               console.log(tweet.text);
            }
       })
     }

My code is only sending the data for the function “tweets”

      json = {};
     function tweets(y){
       client.get('statuses/user_timeline', {screen_name:y, count:1},
       function(err,data) {
         for(var index in data){
           var tweet = data[index];
           console.log(tweet.text);
         }
          json[index] = tweet
          res.end(JSON.stringify(json));
      })
    }

2

Answers


  1. For saving the data to a file without a database, you can use fs.writeFile():

    fs.writeFile('message.txt', 'Hello Node.js', (err) => {
      if (err) throw err;
      console.log('It's saved!');
    });
    

    https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback

    Server/Node:

    var app = require('http').createServer(handler)
    var io = require('socket.io')(app);
    var fs = require('fs');
    
    app.listen(80);
    
    function handler (req, res) {
      fs.readFile(__dirname + '/index.html',
      function (err, data) {
        if (err) {
          res.writeHead(500);
          return res.end('Error loading index.html');
        }
    
        res.writeHead(200);
        res.end(data);
      });
    }
    
    io.on('connection', function (socket) {
      if(twitterInfoReady){
          socket.emit('twitterInfoIncoming', { 
              twitterInfo1: 'blah',
              twitterInfo2: 'more data',
              twitterInfo3: 'and more data'
          });
      }
    });
    

    Client

    <script src="/socket.io/socket.io.js"></script>
    <script>
      var socket = io('http://localhost');
      socket.on('twitterInfoIncoming', function (data) {
        console.log(data.twitterInfo1);
      });
    </script>
    

    Example taken from Socket.io docs, just modified a tiny bit http://socket.io/docs/

    Login or Signup to reply.
  2. As I understand you are not looking to save the data, but just collect the results of multiple asynchronous calls and once all are completed deliver the data to your client? If so, you could use async or promises.

    There are already examples of this in Stack Overflow, eg. this Node.js: Best way to perform multiple async operations, then do something else? but here anyways simplified implementations for both.

    Using async

    var async = require('async');
    
    // ...
    
    var tweets = function(y) {
      return function(cb) {
        client.get('statuses/user_timeline', {screen_name: y, count: 1},
          function(err, data) {
            // Process the data ...
            cb(null, processedTweets);
          }
        );
      }
    };
    
    var mentions = function(x) {
      return function(cb) {
        client.get('search/tweets', {q: x , count: 1}, 
          function(err, data) {
            // Process the data ...
            cb(null, processedMentions);
          }
        );
      }
    };
    
    app.get('/mytweetsapi', function(req, res) {
      var tasks = [];
      if (string.checktweet1 == 'on') {
        tasks.push(tweets(string.teamname));
      }
      if (string.checkmentions1 == 'on'){
        tasks.push(mentions(string.teamname));
      }
      if (string.checktweet2 == 'on'){
        tasks.put(tweets(string.playername));
      }
      if (string.checkmentions2 == 'on'){
        tasks.put(mentions(string.playername));
      }
    
      async.parallel(tasks, function(err, results) {
        // Process the results array ...
        res.json(processedResults);
      });
    });
    

    Using promises

    var Promise = require('bluebird');
    
    // ...
    
    var tweets = function(y) {
      return new Promise(function(resolve, reject) {
        client.get('statuses/user_timeline', {screen_name: y, count: 1},
          function(err, data) {
            // Process the data ...
            resolve(processedTweets);
          }
        );
      });
    };
    
    var mentions = function(x) {
      return new Promise(function(resolve, reject) {
        client.get('search/tweets', {q: x , count: 1}, 
          function(err, data) {
            // Process the data ...
            resolve(processedMentions);
          }
        );
      });
    };
    
    app.get('/mytweetsapi', function(req, res) {
      var tasks = [];
      // Same if this then tasks.push as in async example here
    
      Promse.all(tasks).then(function(results) {
        // Process the results array ...
        res.json(processedResults);
      });
    });
    

    I don’t know what HTTP client you are using, but you can maybe use var client = Promise.promisifyAll(require('your-client-lib')); to convert the methods to return promises, and then you could convert the tweets and mentions functions to

    var tweets = function(y) {
      return client.get('statuses/user_timeline', {screen_name: y, count: 1});
    };
    

    This way though the results in Promise.all are mixed responses and you would need to identify which are tweets and which are mentions to process them properly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search