thur
thur

Reputation: 1074

Template level subscription, is running a lot of time... Should I use?

I'm doing my meteor app and it has 1 Collection: Students
In Server I made a Publish that receives 3 params: query, limit and skip; to avoid client to subscribe all data and just show the top 10.

I have also 3 Paths:

Each Template subscribe to the Students collection, but every time the user change between this paths, my Template re-render and re-subscribe.

Should I make just one subscribe, and make the find based on this "global" subscription?

I see a lot of people talking about Template level subscription, but I don't know if it is the better choice. And about making query on server to publish and not send all data, I saw people talking too, to avoid data traffic...

In this case, when I have just 1 Collection, is better making an "global" subscription?

Upvotes: 1

Views: 53

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20226

You're following a normal pattern although it's a bit hard to tell without the code. If there many students then you don't really want to publish them all, only what is really necessary for the current route. What you should do is figure out why your pub-sub is slow. Is it the find() on the server? Do you have very large student objects? (In which case you will probably want to limit what fields are returned). Is the search you're running hitting mongo indexes?

Your publication for a list view can have different fields than for a individual document view, for example:

Meteor.publish('studentList',function(){
  let fields = { field1: 1, field2: 1 }; // only include two fields
  return Students.find({},fields);
});

Meteor.publish('oneStudent',function(_id){
  return Students.find(_id); // here all fields will be included
});

Upvotes: 1

Related Questions