Search code examples
node.jsmongodbmeteorreal-time

MeteorJS subscription mechanism


I have a question about subscription/publication in meteor.

When you subscribe to a publication in meteor, is the client database updated or only a copy at one moment of the server database?

In fact, will Meteor update the local database after a subscription, or will it just be an image of the server's database at a T moment where T is the moment of subscription ? And can I get the last data when I call Collection.find() on client side ?


Solution

  • The quick answer is that as long as your subscription is still active, Meteor will keep the client in sync with the server per the rule you defined in the publish() method. It is not a copy at time T, it starts at time T and is amended as the server collection changes.

    For example:

    /server/publish/people.js:

    Meteor.publish('people', function() {
      return People.find();
    }
    

    /client/app.js:

    Meteor.subscribe('people');
    

    The publish()'s 'People.find()' will be monitored and any changes to the query will be replicated on the client. If you have reactive queries (People.find() in a template helper for example) on the client, those will be re-executed automatically and the template updated (see Tracker).

    Good reference for you: https://www.discovermeteor.com/blog/understanding-meteor-publications-and-subscriptions/