Meteor:从外部源(套接字)发布数据

时间:2015-04-25 05:06:23

标签: sockets meteor publish-subscribe ddp

我在他们的库中使用getstream.io API。他们的JS库中有以下钩子:

newsFeed.subscribe(function callback(data) {
        //do something with the data   
 });

我创建了一个包装来包装NPM库。 我现在想要使用此功能并将结果发布到仅客户端集合。

我要尝试这个,但我不知道是否可以:

Meteor.publish("newsFeed", function () {
  var self = this;
  var newsFeed = GetstreamClient.feed('notification', 'newsFeed');
  //TODO : Use MeteorAsync ?
  newsFeed.subscribe(function callback(data) {
        self.added("clientNewsCollection", Random.id(), {event: data});
    });
  self.ready();
});

这可以吗?或者我应该这样做?

// This will be used to store the subscription objects for later management
var subs = { };

// The helper publication
Meteor.publish('newsFeed', function() {

       var subscription = this;
       subs[subscription._session.id] = subscription;

       subscription.onStop(function() {
          delete subs[subscription._session.id];
       });
});

var newsFeed = GetstreamClient.feed('notification', 'newsFeed');
//TODO : Use MeteorAsync ?
 newsFeed.subscribe(function callback(data) {
        for (var subscriptionID in subs) {
             var subscription = subs[subscriptionID];
             subscription.added("clientNewsCollection", Random.id(), {event: data});
        }
  });

0 个答案:

没有答案