SubsManager
Subscriptions Manager for Meteor
This is a general-purpose subscriptions manager for Meteor. It also works pretty well with Iron Router, with some limitations.
Why?
When you are subscribing inside a Tracker.autorun
computation, all the subscriptions started on the previous computation will be stopped.
Iron Router runs all subscriptions inside a Tracker.autorun
computation, so this will affect Iron Router too: when you navigate to a new route, all the previous subscriptions will be stopped. The user will have to wait a bit even if they've visited that route previously. That's an UX issue.
Also, this will force the Meteor server to resend data you already had in the client. It will waste your server's CPU and network bandwidth.
Solution
Subscriptions Manager caches your subscriptions and runs all the subscriptions that have been cached when a route is changed. This means that when switching between routes, the user will no longer have to wait. Also, Meteor won't need to re-send data that's already in the client.
In technical terms, Subscriptions Manager runs it's own Tracker.autorun
computation internally. It does not interfere with Iron Router and works independently.
Subscriptions Manager does not cache your individual data. It tells Meteor to cache the whole subscription. So, your data will get updated in the background as usual.
Usage
Installation
1meteor add meteorhacks:subs-manager 2// if you've not yet migrated to Meteor 0.9, apply following: 3// mrt add subs-manager
Usage with Iron Router: just replace Meteor.subscribe()
calls with subs.subscribe()
, where subs
is a new SubsManager()
.
1var subs = new SubsManager(); 2 3Router.map(function() { 4 this.route('home', { 5 path: '/', 6 waitOn: function() { 7 return subs.subscribe('postList'); 8 } 9 }); 10 11 this.route('singlePost', { 12 path: '/post/:id', 13 waitOn: function() { 14 return subs.subscribe('singlePost', this.params.id); 15 } 16 }); 17})
Using with Tracker.autorun:
1var subs = new SubsManager(); 2Tracker.autorun(function() { 3 var postId = Session.get('postId'); 4 subs.subscribe('singlePost', postId); 5});
Resetting
Sometime, we need to re-run our subscriptions may be after some major activity in the app.
Eg:- After a user has update the plan.
In those situations, you can reset Subscription Manager.
1var subs = new SubsManager(); 2 3// later in some other place 4subs.reset();
Clear Subscriptions
In somecases, we need to clear the all the subscriptions we cache. So, this is how we can do it.
1var subs = new SubsManager(); 2 3// later in some other place 4subs.clear();
Limitations
Subscription Manager aims to be a drop-in replacement for Meteor.subscribe
(or this.subscribe()
in Iron Router). At the moment, the following functionality doesn't work (patches welcome):
onError
andonReady
callbacks (issue)- chained
.wait()
call in Iron Router (issue - you can usewaitOn
instead)
Cache Control
Since now you are caching subscriptions, the Meteor server will also cache all your client data. But don't worry - that's not a huge issue. See this Kadira Academy article to learn more about V8 and Meteor memory usage.
But, you should have the capability to control the cache. Subscriptions Manager does that.
1var subs = new SubsManager({ 2 // maximum number of cache subscriptions 3 cacheLimit: 10, 4 // any subscription will be expire after 5 minute, if it's not subscribed again 5 expireIn: 5 6});
The above values are the default values for each option.
Patterns for using SubsManager
Using a global Subscription Manager
You can create a global subscription manager as shown in the Iron Router example. By doing that, all your subscriptions are handled the same way.
Using separate Subscription Managers
If you need more control over caching, you can create separate Subscription Managers for each set of subscriptions and manage them differently. For example,
- you can cache home page subscriptions indefinitely
- but you can control the cache for other routes
1var homeSubs = new SubsManager({cacheLimit: 9999, expireIn: 9999}); 2var singleSubs = new SubsManager({cacheLimit: 20, expireIn: 3}); 3 4Router.map(function() { 5 this.route('home', { 6 path: '/', 7 waitOn: function() { 8 return homeSubs.subscribe('postList'); 9 } 10 }); 11 12 this.route('singlePost', { 13 path: '/post/:id', 14 waitOn: function() { 15 return singleSubs.subscribe('singlePost', this.params.id); 16 } 17 }); 18})
Cache subscriptions you only need
With Subscription Manager you don't need to use it everywhere. Simply use it wherever you need without changing other subscriptions.
For example, to only use Subscription Manager for the home page:
1var subs = new SubsManager(); 2Router.map(function() { 3 this.route('home', { 4 path: '/', 5 waitOn: function() { 6 return subs.subscribe('postList'); 7 } 8 }); 9 10 this.route('singlePost', { 11 path: '/post/:id', 12 waitOn: function() { 13 // These subscriptions will be handled by Iron Router 14 // and do not interfere with the Subscription Manager subscriptions 15 return Meteor.subscribe('singlePost', this.params.id); 16 } 17 }); 18})
Using global ready checking
You can also check the ready status of all the subscriptions at once like this:
1var subs = new SubsManager(); 2subs.subscribe('postList'); 3subs.subscribe('singlePost', 'id1'); 4 5Tracker.autorun(function() { 6 if(subs.ready()) { 7 // all the subscriptions are ready to use. 8 } 9});