Skip to content Skip to sidebar Skip to footer

Node.js How To Use Callback To Send Data To Web Interface

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

Solution 1:

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) {
  returnfunction(cb) {
    client.get('statuses/user_timeline', {screen_name: y, count: 1},
      function(err, data) {
        // Process the data ...
        cb(null, processedTweets);
      }
    );
  }
};

var mentions = function(x) {
  returnfunction(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

varPromise = require('bluebird');

// ...var tweets = function(y) {
  returnnewPromise(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) {
  returnnewPromise(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 herePromse.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.

Solution 2:

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

functionhandler(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

<scriptsrc="/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/

Post a Comment for "Node.js How To Use Callback To Send Data To Web Interface"