Meteor docs中的messages-count示例如何工作?

时间:2012-05-12 16:51:11

标签: javascript mongodb meteor publish-subscribe

the docs无法完全理解这个例子......我尝试了一些不同的方式运行它,以便我可以观察它是如何工作的等等。

您如何订阅?我们可以包含使这项工作所需的客户端代码吗?

是否有一个名为messages-count的集合? Room是一组消息吗?我们可以在示例中包含集合定义吗?

关于此的任何提示都会很棒!

注意:这是最初发布此问题时出现的代码(2012年5月)。它现在更简单了。

// server: publish the current size of a collection
Meteor.publish("messages-count", function (roomId) {
  var self = this;
  var uuid = Meteor.uuid();
  var count = 0;

  handle = Room.find({room_id: roomId}).observe({
    added: function (doc, idx) {
      count++;
      self.set("messages-count", uuid, "count", count);
      self.flush();
    },
    removed: function (doc, idx) {
      count--;
      self.set("messages-count", uuid, "count", count);
      self.flush();
    }
    // don't care about moved or changed
  });

  // remove data and turn off observe when client unsubs
  self.onStop(function () {
    handle.stop();
    self.unset("messages-count", uuid, "count");
    self.flush();
  });
});

3 个答案:

答案 0 :(得分:52)

感谢您提示我写一个更清晰的解释。这是我的评论的更全面的例子。我清理了一些错误和不一致的地方。下一个docs release将使用它。

Meteor.publish非常灵活。它不仅限于将现有的MongoDB集合发布到客户端:我们可以发布任何我们想要的东西。具体而言,Meteor.publish定义客户可以订阅的文档集。每个文档都属于某个集合名称(字符串),具有唯一的_id字段,然后具有一些JSON属性集。随着集合中的文档发生变化,服务器会将更改发送到每个订阅的客户端,使客户端保持最新状态。

我们将在此处定义一个名为"counts-by-room"的文档集,其中包含名为"counts"的集合中的单个文档。该文档将包含两个字段:roomId,ID为房间,count:该房间的邮件总数。没有名为counts的真正MongoDB集合。这只是我们的Meteor服务器将发送到客户端的集合的名称,并存储在名为counts客户端集合中。

为此,我们的发布函数采用来自客户端的roomId参数,并观察该房间中所有消息(在别处定义)的查询。我们可以使用更高效的observeChanges形式来观察查询,因为我们不需要完整的文档,只需要添加或删除新文档的知识。每当我们感兴趣的roomId添加新消息时,我们的回调会增加内部计数,然后使用更新的总计将新文档发布到客户端。当消息被删除时,它会减少计数并向客户端发送更新。

当我们第一次调用observeChanges时,对于已经存在的每条消息,会立即运行一些added次回调。然后,无论何时添加或删除消息,都将触发未来的更改。

我们的发布函数还注册了一个onStop处理程序,以便在客户端取消订阅(手动或断开连接)时进行清理。此处理程序从客户端删除属性并删除正在运行的observeChanges

每次新客户订阅"counts-by-room"时都会运行发布功能,因此每个客户都会代表其运行observeChanges

// server: publish the current size of a collection
Meteor.publish("counts-by-room", function (roomId) {
  var self = this;
  var count = 0;
  var initializing = true;

  var handle = Messages.find({room_id: roomId}).observeChanges({
    added: function (doc, idx) {
      count++;
      if (!initializing)
        self.changed("counts", roomId, {count: count});  // "counts" is the published collection name
    },
    removed: function (doc, idx) {
      count--;
      self.changed("counts", roomId, {count: count});  // same published collection, "counts"
    }
    // don't care about moved or changed
  });

  initializing = false;

  // publish the initial count. `observeChanges` guaranteed not to return
  // until the initial set of `added` callbacks have run, so the `count`
  // variable is up to date.
  self.added("counts", roomId, {count: count});

  // and signal that the initial document set is now available on the client
  self.ready();

  // turn off observe when client unsubscribes
  self.onStop(function () {
    handle.stop();
  });
});

现在,在客户端上,我们可以将其视为典型的Meteor订阅。首先,我们需要一个Mongo.Collection来保存我们计算的计数文档。由于服务器正在发布到名为"counts"的集合中,因此我们将"counts"作为参数传递给Mongo.Collection构造函数。

// client: declare collection to hold count object
Counts = new Mongo.Collection("counts");

然后我们可以订阅。 (您可以在声明集合之前实际订阅:Meteor将对传入的更新进行排队,直到有一个位置放置它们。)订阅的名称是"counts-by-room",它需要一个参数:当前房间的ID。我已将其封装在Deps.autorun内,以便当Session.get('roomId')更改时,客户端将自动取消订阅旧房间的计数并重新订阅新房间的数量。

// client: autosubscribe to the count for the current room
Tracker.autorun(function () {
  Meteor.subscribe("counts-by-room", Session.get("roomId"));
});

最后,我们在Counts中获得了文档,我们可以像使用客户端上的任何其他Mongo集合一样使用它。每当服务器发送新计数时,任何引用此数据的模板都将自动重绘。

// client: use the new collection
console.log("Current room has " + Counts.findOne().count + " messages.");

答案 1 :(得分:2)

正如Leonhardt Wille所说,这个解决方案的缺点是流星从Mongo服务器下载整个项目集合只是为了计算它们。他在gist.github.com/3925008的解决方案更好,但是当插入新项目时,计数器不会更新。

这是我的反应性解决方案

类别:

Players = new Meteor.Collection("players");
PlayersCounts = new Meteor.Collection("players_counts")

服务器:

Meteor.publish("players_counts", function(){
    var uuid = Meteor.uuid()
    var self = this;

    var unthrottled_setCount = function(){
        cnt = Players.find({}).count()
        self.set("players_counts", uuid, {count: cnt})
        self.flush()
    }

    var setCount = _.throttle(unthrottled_setCount, 50)

    var handle = Meteor._InvalidationCrossbar.listen({collection: "players"}, function(notification, complete){
        setCount();
        complete();
    })

    setCount();
    self.complete()
    self.flush()

    self.onStop(function(){
        handle.stop();
        self.unset("players_counts", uuid, ["count"]);
        self.flush();
    });
});

客户端:

Meteor.subscribe("players_counts")

Template.leaderboard.total = function(){
    var cnt = PlayersCounts.findOne({})
    if(cnt) {
        return cnt.count;
    } else {
        return null;
    }
}

答案 2 :(得分:0)

刚刚找到解决问题的方法,其中self.flush()向客户端发送了数千个更新 - 只需在计数时使用_.debounce:

count = 0
throttled_subscription = _.debounce =>
  @set 'items-count', uuid, count: count
  @flush()
, 10
handle = Items.find(selector).observe
  added: =>
    count++
    throttled_subscription()
  removed: =>
    count--
    throttled_subscription()

这只会在10毫秒无变化后设置计数并刷新订阅。

感谢#meteor上的@possibilities提示。