Publish with observeChange


#1

This is the code snippet from the Meteor API documentation on publish / subscribe.

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

// observeChanges only returns after the initial added callbacks
// have run. Until then, we don’t want to send a lot of
// self.changed() messages - hence tracking the
// initializing state.
var handle = Messages.find({roomId: roomId}).observeChanges({
added: function (id) {
count++;
if (!initializing)
self.changed(“counts”, roomId, {count: count});
},
removed: function (id) {
count–;
self.changed(“counts”, roomId, {count: count});
}
// don’t care about changed
});

Can I observe changes made to an existing document? In the above example that would mean that an existing Message document’s attribute field(s) change. Is this the correct way to observe a change of an existing document:

var handle = Messages.find({roomId: roomId}).observeChanges({
changed: function (id) {

self.changed(…);
}

Also what does the ‘id’ variable refer to in the changed: function(id) line?

Thanks