Yes …
You can consider a subscription as the client telling the server to do something. The publication can do whatever you want. Most times that will be a query against a MongoDB collection - but it doesn’t need to be.
The other thing that’s expected of pub/sub is: “while you’re doing that thing, keep me informed about progress while you do it.” Again, you’ll usually just use the inbuilt livedata in Meteor to ensure a collection’s changes are available to the client - but it doesn’t need to be.
… and no
The way server-side changes are communicated to the client is via minimongo. However, it’s important to note that you don’t need an actual (disk-based) MongoDB collection for this. Instead the publication uses the pub/sub API to populate a client-side minimongo collection.
So, the publication could be written like this:
Meteor.publish('buildFiles', function(urls) {
// skipping the sanity checks and error checking for brevity
urls.forEach((url, index) => {
const data = HTTP.get(url);
buildHTML(data);
this.added('dummyCollection', `seq${index}`, { index, url });
this.ready();
});
});
Note that the above code processes the urls sequentially. You could use callbacks or Promises to process them concurrently.
As each url is processed, a new document is added (this.added
) to the client-only collection, dummyCollection
, and made available to the client (this.ready
). The document imitates a MongoDB document, so needs an _id
('seq0'
, 'seq1'
, …). Otherwise, my example just has an index
field and the url. You can, of course, add whatever you want.
On the client, you need to define a matching collection and subscribe in the normal way. Note that you don’t define this collection anywhere on the server - it doesn’t exist there.
const dummy = new Mongo.Collection('dummyCollection');
Template.buildFiles.onCreated(function buildFilesOnCreated() {
this.subscribe('buildFiles', ['url1.com', 'url2.com', 'url3.com']);
});
Template.buildFiles.helpers({
buildProgress() {
return dummy.find();
}
});
You can also make use of autorun
and have reactive parameters in the subscribe
, exactly as with any other subscription, for example for resubscribing with another url list.