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

Mik*_*ter 41 javascript publish-subscribe mongodb meteor

无法从文档中充分理解这个例子......我尝试了一些不同的方式运行它,以便我可以观察它是如何工作的,等等.

你怎么订阅这个?我们可以包含使这项工作所需的客户端代码吗?

有一个名为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();
  });
});
Run Code Online (Sandbox Code Playgroud)

deb*_*lis 52

感谢您提示我写一个更清晰的解释.这是我的评论的更全面的例子.我清理了一些错误和不一致的地方.下一个文档发布将使用此.

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

我们将在这里定义一个名为的文档集"counts-by-room",其中包含名为的集合中的单个文档"counts".该文档将包含两个字段:a roomId表示房间的ID,以及count:该房间中的邮件总数.没有名为真正的MongoDB集合counts.这只是我们的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();
  });
});
Run Code Online (Sandbox Code Playgroud)

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

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

然后我们可以订阅.(您可以在声明收集之前实际订阅: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"));
});
Run Code Online (Sandbox Code Playgroud)

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

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

  • 清楚如钟!非常感谢您花时间为我澄清这一点! (2认同)
  • 请注意,`self.flush();`在`added`中将在填充集合时将该订阅推送到客户端.想象一下,你在"room_id"中有1,000,000条"消息".您将收到1,000,000个订阅,从计数1开始,到1,000,000计数结束.这将锁定您的浏览器很长一段时间!更不用说通过电线飞行的数据量...... (2认同)