Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor publications and subscription

I'm trying to learn Meteor, and currently trying to wrap my head around publications and subscriptions. I'm following the discover meteor book and one main point just doesn't make sense to me and was hoping some explanations in simple terms can be shared.

So a publication is what "fetches" the data from the mongo database to store within Meteor:

Meteor.publish('posts', function() {
   return Posts.find(); 
});

Then on the client side I subscribe to the publication. Woopy

Meteor.subscribe('posts');

What doesn't make sense is the Template Helpers. Originally Discover Meteor tells you to create an array of static posts that iterate through each post using a template helper. Well now that I'm turning things dynamically my template helper becomes:

Template.postsList.helpers({
    posts: function () {
        return Posts.find();
    }
});

What's the point of running the Posts.find() on both the server and client template helper?

like image 317
Dileet Avatar asked Jan 20 '26 07:01

Dileet


1 Answers

Posts in publishing are server side collection. Posts in helpers are client-side collection which contains all published Posts. If you have thousands of posts you usually don't want to publish all Posts because it will take a few seconds to download the data.

You should publish just the data you need.

Meteor.publish('posts', function(limit) {
   return Posts.find({}, { limit: limit}); 
});

When you call this subscribe function, client-side collection Posts will contain just 100 posts.

var limit = 100;
Meteor.subscribe('posts', limit);
like image 194
Tomas Hromnik Avatar answered Jan 22 '26 23:01

Tomas Hromnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!