Meteor publish and subscription

Hi, when i subscribe to a publish method like

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

I noticed that if i have 2k posts, the client will download all these 2k posts

I want to ask, the only way i know of to control how much data is being sent to the client is by doing pagination publication

to send the publish method, offest and limit with a specific query as well

Is there any other way to handle this? or better practices? so on client i only for example download 10 at a time or 20 at at time

also if i publish Posts.find(query), the only data is being sent to client is the cursor with that selected query?

Your query expressly states that it should get all the data in the Posts collection.

You need to provide a query that will limit the results.

As for limiting, again see the docs, there is the options fields where you can use things like limit and skip, sort, which you can use to create pagination.

So your code should look something like this:

Meteor.publish('get.posts', function (page) {
  check(page, Number);
  const limit = 10;
  const skip = (page - 1) * skip;
  return Posts.find({}, { limit, skip, sort: { createdAt: -1 } });
1 Like

I have seen on other posts that people prefer aggregation while using methods over publication

and in the aggregation simply do the pagination and skipping, I was wondering why is that, why it costs more to use publication/subscription, and how is it possible to achieve live data changes with out useTracker, the reason why i’m using publication/subscription so i later subscribe on useTracker and get live update of data

I’m simply trying to grasp a better understanding of the data transferring while using meteor

thank you for your answer

In most cases in Meteor you’d want to load data via methods and in those methods run simple Mongo find queries. So when in doubt, go with that.

Pub/sub provides you with live queries that automatically push new data to the client when anything relevant changes in the database. Very nice and beneficial in some cases, but this functionality comes with a steep price both in terms of performance (think reduced scalability) and technical complexity. So this should not be your default go-to tool.

Mongo aggregation is an alternative way to query data from the database. Gives you many tools to filter and process data in flexible ways. But in most cases a simple normal find query will do the job just fine. You should look at aggregation only when you have determined that your query is complex enough that a regular find query falls short. For the majority of apps that situation never arises.


In regards to aggregation that is an advance feature as @vooteles has described. Usually that means you want to pull data from multiple collections and do some other manipulations on larger data sets.

As I showed in my example you use limit and skip to create pagination. Don’t also forget to put in a proper selector to only get the posts that you want for that particular subscription.
If you don’t need the data to be reactive, then you should use methods or GraphQL.

1 Like

Thank you so much for your reply, this seems to answer all my questions that I had

Thank you so much for your reply, I appreciate it a lot, I got all the answers I need