Search code examples
meteorflow-router

Meteor Flow Router: Route subscriptions vs Template subscriptions


I'm working on a Meteor app with Flow-Router.

I'm retrieving an entire user list (multiple users), by creating a publish function that publishes all users. In my route, I'm subscribing to this (Route based subscription) and then I create a template helper method in which I'm returning these users in a specific order.

I'm also displaying the profile of the current user. I tried to do this via the routes subscription (as explained above basically) but I could not get it to work. I then converted this to template based subscription and got it to work. So I put an autorun in the Template onCreated function, created a helper to retrieve that user and in the template I'm doing a Template.subscriptionsReady.

Long story short: can someone explain me what is the difference between Route based subscription and Template based subscription. I have been reading this multiple times, I can get both to work well but to be honest I still don't understand when to use what exactly.


Solution

  • From Kadira, the team behind flow router, https://kadira.io/academy/meteor-routing-guide/content/subscriptions-and-data-management . Explains why you should use template subscription and NOT router subscriptions.

    See extract:

    Subscriptions and Data Management

    One of the essential parts of a Meteor app is data and subscription management. In the past, we invoked subscriptions in the router itself and manage data inside it. However, now we consider that to be an anti-pattern.

    Why?

    In the client side, we've no control on when data will be available for the app. You need to wait for subscription to send the data. Router needs to wait for data and it needs to be reactive. This leads to a lot of unpredictable behavior specially when your app became larger. While data are loading, we may need to show loading messages. So we would need to find ways to interact with the router and the rendering layers. Showing a global loader is not a good pattern. Now, wherever you reuse the template, you need to define data requirement for that in the router again.