Javascript Meteor文档中的消息计数示例是如何工作的?
很难完全理解这个例子。。。我试着用多种不同的方式运行它,以便观察它是如何工作的,等等 你怎么订阅这个?我们是否可以包含实现此功能所需的客户端代码 是否有一个名为“消息计数”的集合?Javascript Meteor文档中的消息计数示例是如何工作的?,javascript,mongodb,meteor,publish-subscribe,Javascript,Mongodb,Meteor,Publish Subscribe,很难完全理解这个例子。。。我试着用多种不同的方式运行它,以便观察它是如何工作的,等等 你怎么订阅这个?我们是否可以包含实现此功能所需的客户端代码 是否有一个名为“消息计数”的集合?房间是一组信息吗?我们可以在示例中包括集合定义吗 任何关于这方面的提示都会很好 注:这是该问题最初发布(2012年5月)时出现的代码。现在更简单了 // server: publish the current size of a collection Meteor.publish("messages-count", f
房间
是一组信息吗?我们可以在示例中包括集合定义吗
任何关于这方面的提示都会很好
注:这是该问题最初发布(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();
});
});
谢谢你提醒我写一个更清楚的解释。这里有一个更完整的例子和我的评论。我已经清理了一些bug和不一致之处。下一个docs版本将使用这个 它相当灵活。它不仅限于将现有MongoDB集合发布到客户端:我们可以发布任何我们想要的内容。具体来说,
Meteor.publish
定义了一组客户可以订阅的文档。每个文档都属于某个集合名称(一个字符串),有一个唯一的\u id
字段,然后有一组JSON属性。当集合中的文档发生更改时,服务器会将更改发送到每个订阅的客户端,使客户端保持最新
我们将在这里定义一个文档集,名为“counts by room”
,它在名为“counts”
的集合中包含一个文档。文档将有两个字段:带有房间ID的roomId
,以及count
:该房间中的邮件总数。没有名为counts
的真正MongoDB集合。这只是Meteor服务器将要发送到客户端的集合的名称,并存储在名为counts
的客户端集合中
为此,我们的发布函数接受一个将来自客户端的roomId
参数,并观察该房间中所有消息(在别处定义)的查询。我们可以在这里使用观察查询的更有效的observeChanges
形式,因为我们不需要完整的文档,只需要知道添加或删除了一个新文档。每当使用我们感兴趣的roomId
添加新邮件时,我们的回调会增加内部计数,然后使用更新的总数向客户端发布新文档。当消息被删除时,它会减少计数并向客户端发送更新
当我们第一次调用observeChanges
时,对于已经存在的每条消息,一些添加的回调将立即运行。然后,无论何时添加或删除消息,都将触发将来的更改
我们的发布功能还注册了一个onStop
处理程序,以便在客户端取消订阅时(手动或断开连接时)进行清理。此处理程序从客户端删除属性,并删除正在运行的observeChanges
每次新客户端订阅“按房间计数”
时,发布函数都会运行,因此每个客户端都将有一个代表其运行的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将对传入的更新进行排队,直到有地方放置它们。)订阅的名称是“按房间计数”
,它有一个参数:当前房间的ID。我将其包装在Deps.autorun
中,以便作为会话.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.");
刚刚找到了一个解决方案,解决了self.flush()向客户端发送数千次更新的问题-计算时只需使用u.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毫秒无更改后设置计数并刷新订阅
多亏了@impabilities on#meteor提供的提示。正如Leonhardt Wille所说,此解决方案的缺点是meteor从Mongo服务器下载整个项目集合只是为了计算它们。他在的解决方案更好,但插入新项目时计数器不会更新 这是我的反应式解决方案 收藏: 服务器: 客户:
清清楚楚!非常感谢您花时间为我澄清这一点!请注意,
self.flush()添加的中的code>将在集合填充时将该订阅向下推送到客户端。想象一下,在“房间id”中有1000000条“消息”。您将收到1000000份订阅,从计数1开始,到计数1000000结束。这将在相当长的一段时间内锁定您的浏览器!更不用说传输的数据量了…@matb33,有没有更好的解决刷新问题的方法?作为临时修复,您可以限制对self.flush()的调用使用setTimeout
技巧添加中的code>,例如:cleartimout(t);t=setTimeout(函数(){self.flush();},10);没关系,只是在下面看到了你的代码!看起来你已经明白了这个解决方案的缺点是meteor会将整个收藏下载到服务器上,因此如果你使用相对较慢的mongoDB远程连接,那么在你的应用程序启动后会有明显的延迟(至少如果你的DB中有10000个文档)
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;
}
}