Using streaming data from Twitter with Meteor

So far, I have been able to output live streaming data from Twitter. How to use this data? I am trying to insert it into a collection, but I am getting this error:

Error: Meteor code should always work inside Fiber. Try exchanging callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.

I tried wrapping my code with fiber, but it did not work / or I am not wrapping the right part of the code. Also, I'm not sure if this is the right way to use streaming data in Meteor.

Posts = new Meteor.Collection('posts');

if (Meteor.isClient) {
  Meteor.call("tweets", function(error, results) {
    console.log(results); //results.data should be a JSON object
  });
}

if (Meteor.isServer) {    
  Meteor.methods({
    tweets: function(){

      Twit = new TwitMaker({
        consumer_key: '...',
        consumer_secret: '...',
        access_token: '...',
        access_token_secret: '...'
      });

      sanFrancisco = [ '-122.75', '36.8', '-121.75', '37.8' ];

      stream = Twit.stream('statuses/filter', { locations: sanFrancisco });


      stream.on('tweet', function (tweet) {
        userName = tweet.user.screen_name;
        userTweet = tweet.text;
        console.log(userName + " says: " + userTweet);
        Posts.insert({post: tweet})

      })  
    }    
  })  
}
+3
source share
3 answers

, , , . , , Meteor, () , , , , , , . Meteor.bindEnvironment , . Meteor.bindEnvironment, , : https://github.com/meteor/meteor/blob/master/packages/meteor/dynamics_nodejs.js#L63

- , , :

tweets: function() {
  ...

  // You have to define this wrapped function inside a fiber .
  // Meteor.methods always run in a fiber, so we should be good here. 
  // If you define it inside the callback, it will error out at the first
  // line of Meteor.bindEnvironment.

  var wrappedInsert = Meteor.bindEnvironment(function(tweet) {
    Posts.insert(tweet);
  }, "Failed to insert tweet into Posts collection.");

  stream.on('tweet', function (tweet) {
    var userName = tweet.user.screen_name;
    var userTweet = tweet.text;
    console.log(userName + " says: " + userTweet);
    wrappedInsert(tweet);
  });
}
+6

. Meteor.bindEnvironment Twit.

Meteor.methods({
    consumeTwitter: function () {

    var Twit = Meteor.npmRequire('twit');

    var T = new Twit({
        consumer_key:         'xxx', // API key
        consumer_secret:      'yyy', // API secret
        access_token:         'xxx',
        access_token_secret:  'xxx'
    });

    //  search twitter for all tweets containing the word 'banana'
    var now = new Date().getTime();

    var wrappedInsert = Meteor.bindEnvironment(function(tweet) {
      Tweets.insert(tweet);
    }, "Failed");


    T.get('search/tweets',
        {
            q: 'banana since:2011-11-11',
            count: 4
        },
        function(err, data, response) {
          var statuses = data['statuses'];

          for(var i in statuses) {
             wrappedInsert(statuses[i]);
          }
        }
    )}
});
+1

I wrote a long post about Creating Twitter monitoring applications using MeteorJS from Scratch , including the Meteor.bindEnvironment part, as shown below.

var Twit = Meteor.npmRequire(‘twit’);
var conf = JSON.parse(Assets.getText(‘twitter.json’)); 
var T = new Twit({
 consumer_key: conf.consumer.key, 
 consumer_secret: conf.consumer.secret,
 access_token: conf.access_token.key, 
 access_token_secret: conf.access_token.secret

// 
// filter the twitter public stream by the word ‘iwatch’. 
//

var stream = T.stream(‘statuses/filter’, { track: conf.keyword })

stream.on(‘tweet’, Meteor.bindEnvironment(function (tweet) {
 console.log(tweet);
 Tweets.insert(tweet);
}))

Only two functions are added:

Meteor.bindEnvironment()

This function helps us bind the function to the current value of all environment variables.

Good luck

0
source

All Articles