Search code examples
javascriptmeteormeteor-publications

Should I use one publish per collection or several?


I am working on a user group system. Each group has several features and I want to make the interaction with the group collection as secure and simple as it can be since it is still at an early stage.

Right now, I have a group section in my website where I use several nested pages. The purpose of the section is to allow the user to get in a group, request membership if the group is private, browse one group objects, etc.

For example, within my group section, I can load in the yield a "see all groups" page, a "create a new group" page or "see only my groups" (the ones I am member of) or a "view group" to get a group details.

My first approach was to create one controller.js file for each subpage, which call one subscription tailored for the subpage needs. For instance, I have an 'all_group' publication/subscription for the "see all groups" subpage and a "my_groups" one for the "see only my groups" subpage.

But this is becoming really messy. Additionally, I declared my "group" collection in the both folder, so I am not sure to follow where the data available to the client comes from.

Now that I explained the situation, here are my questions:

  • when I do a console.table(Groups.find().fetch()); on client, I see fields that shouldn't be there (i.e. not returned by my current publication or any other). Is that because I declared the "group" collection on client side? How to fix that?
  • Should I get rid of all these publications and create only one with everything the client is allowed to see? I would then subscribe to it from the group section page controller and work with a single set of data.
  • Should I simply block any insert/update/remove from client with allow/deny rules and make these using methods only?
  • Would it be safe/advised to put my methods in both folder so I don't lose the latency compensation feature?

EDIT Ok, I was freaking out because I had all my collection data on client-side but it was just a bad query in the publish (I was using both field:1 and field:0 projections). Two questions remain:

  1. If I use methods, I assume I don't have to deny everything in the native driver, I just have to be more restrictive than what method allow, right?
  2. If I put my methods in the both folder, it will be executed both on client and server, so in "client offline" context, even if the client mess with my methods, the server should roll back the changes if the client result is different than his (assuming that the changes couldn't be done using the allow-deny rules)? And I will have latency compensation working even with the methods?

Solution

  • To better control and visualize your subscriptions, you can use msavin:mongol.

    Creating one catch-all publication is not a good idea performance-wise (sending all data to all clients will be a pain to everyone involved).

    If you use methods and have removed autopublish, then yes everything is denied... Except for updates on the user's own profile. You may want to manually deny that too.

    With methods and collection rules you should share the validation code. This way, client and server validate the same way (and should always come up with the same results), so unless your client is screwing up with the console there should be no issue and lag compensation should remain.
    If your server method does something the client should not know about, you can also define the method once on the server, and once on the client. Same effect.