1

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:

  • student/list -> Bring top 10, based on search input and pagination (using find);
  • student/:id -> Show the student (using findOne)
  • student/:id/edit -> Edit the student (using findOne)

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?

thur
  • 964
  • 1
  • 15
  • 27

1 Answers1

1

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
});
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
  • Today I have more than 7k objects, but I don't know if it is a very large quantity.... I need to publish all fields, because when I go to edit, I need all the fields... I need to verify the mongo indexes... But I don't think that is that, because when I do a findOne with _id, it takes the same time... – thur Jun 06 '16 at 12:53
  • If you need all fields for edit then only publish those when you're editing, publishing a smaller set when you're just viewing a list. How many ms does `.findOne(_id)` take? Does it take the same time from the meteor mongo console as from the browser? – Michel Floyd Jun 06 '16 at 23:45
  • I have 2 questions: - How can I publish those just when I'm editing? - How can I measure the findOne in browser? – thur Jun 07 '16 at 13:44
  • See updated answer for the first part. For measuring `findOne()` see for example http://stackoverflow.com/questions/1210701/compute-elapsed-time – Michel Floyd Jun 07 '16 at 22:06
  • 1
    So, if I do this 2 publish.. In my template I'll do this.subscribe('studentList') and in the other I'll do this.subscribe('oneStudent',_id) ?? – thur Jun 08 '16 at 12:30
  • Thank you, I do those 2 publish and it works better!! – thur Jun 09 '16 at 20:47