Search code examples
meteormeteor-blaze

Subscribe to data range in Meteor


I have a Meteor application which displays a calendar (using fullcalendar.io), and subscribes to bookings within a given date range. The app uses FlowRouter and grabs the date from the URL, and then uses this to subscribe to the bookings (URL date through to URL date + 14 days). This all works fine and I can skip through the days in the calendar, loading events for each day with no refresh as they are coming from minimongo. What I would like to do is to refresh this subscription in the background when the user switches date. This is possible using flow router e.g.:

FlowRouter.go('/diary/2017-04-11')

or by setting the subscription date in a Session / Reactive variable.

This will load the events from 2017-04-11 to 2017-04-25. The issue is that as the entire subscription is recreated there is a slight delay whilst it is loading. What I am trying to achieve is a 'moving window' - for example, if I am subscribed to events from 2017-04-10 and I change the publication to 2017-04-11, then only the 1 extra day gets loaded, rather than all data getting removed and replaced. This would ensure that I am able to skip through the days of the calendar without any load times. If the user selected a date > 14 days in the future manually then they would see the load time, this is perfectly acceptable.


Solution

  • it sounds like your subscriptions are tied to the template that's loaded with each route change. then, when you switch routes, the template is reloaded and the subscription along with it.

    there are a couple options for cache managers, which would allow you to keep a sub active across templates.

    e.g. https://github.com/kadirahq/subs-manager

    note that, while this will allow your client to keep subs active as i've described, it will probably work in an "additive" function. so it won't by itself solve your moving window problem, but it will pick up new items from the publisher as you navigate.

    second note: with this package, you're not limited to a single manager. i've found that it works best if you keep one manager / sub. once i started loading multiple subs to a manager, it started behaving strangely.