简体   繁体   English

使用来自Twitter的流媒体数据与Meteor

[英]Using Streaming Data from Twitter with Meteor

So far I have been able to pull down streaming real time data from Twitter. 到目前为止,我已经能够从Twitter下载流式实时数据。 How do I use this data? 我该如何使用这些数据? I am trying to insert it into collection but I am getting this Error: 我试图将其插入集合,但我收到此错误:

Error: Meteor code must always run within a Fiber. 错误:流星代码必须始终在光纤内运行。 Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment. 尝试使用Meteor.bindEnvironment包装传递给非Meteor库的回调。

I tried wrapping my code with a fiber but it didn't work/or I am not wrapping the right part of the code. 我尝试用光纤包装我的代码,但它不起作用/或者我没有包装正确的代码部分。 Also, I'm not sure if this is the proper way to use streaming data in Meteor. 此外,我不确定这是否是在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})

      })  
    }    
  })  
}

Code that mutates the database needs to be run in a fiber, which is what the error is about. 变异数据库的代码需要在光纤中运行,这就是错误所在。 Code that runs in a callback from a library other than Meteor is not (necessarily) run in a fiber, so you'll need to wrap the callback function to make sure it gets run in a fiber, or at least the part of it that interacts with the database. 从Meteor以外的库回调运行的代码不一定(必然)在光纤中运行,因此您需要包装回调函数以确保它在光纤中运行,或者至少在光纤中运行与数据库交互。
Meteor.bindEnvironment is not currently documented, but it is generally considered the most reliable method of wrapping callbacks. Meteor.bindEnvironment目前尚未记录,但通常被认为是包装回调的最可靠方法。 Meteor.bindEnvironment, which the error talks about, is defined here for reference: https://github.com/meteor/meteor/blob/master/packages/meteor/dynamics_nodejs.js#L63 错误所述的Meteor.bindEnvironment在此处定义以供参考: https//github.com/meteor/meteor/blob/master/packages/meteor/dynamics_nodejs.js#L63

Something like this is probably the easiest way of making this work: 这样的事情可能是最简单的方法:

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

This works for me. 这适合我。 Essential is to call Meteor.bindEnvironment from inside the Twit callback. 必不可少的是从Twit回调中调用Meteor.bindEnvironment

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

I had written a lengthy post about Building Twitter Monitoring Apps with MeteorJS from Scratch , including the Meteor.bindEnvironment part, extract as below. 我写过一篇关于使用Scratch的MeteorJS 构建Twitter监控应用程序的长篇文章,包括Meteor.bindEnvironment部分,摘录如下。

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

There are only two functions added: 只添加了两个功能:

Meteor.bindEnvironment()

This function helps us to bind a function to the current value of all the environment variables. 此函数可帮助我们将函数绑定到所有环境变量的当前值。

Have fun! 玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM