Reputation: 190
I was wondering if there is a proper way to stop client's subscriptions.
For example, i have 2 pages, on the first i subscribe the client to users
publication, and on the second i subscribe to projects
publication.
When the client goes from users page to the projects page, he is still subscribed to the users, although he does not need it on projects page.
So my first question is: Does it take memory or CPU? When the client is subscribing to more and more publications, when he goes from route to route.
And the second: What is the proper way to unsubscribe?? For example when he goes to projects page, unsubscribe from the users publication.
Some packages i see does that. For example reactive-table
.
Thank you in advance!
Upvotes: 0
Views: 796
Reputation: 390
Generally if you want to stop subscriptions you do it with Meteor.stop(). From the guide, on how to stop subscriptions:
if you call Meteor.subscribe() conditionally inside a reactive context (such as an autorun, or getMeteorData in React) or via this.subscribe() in a Blaze component, then Meteor’s reactive system will automatically call this.stop() for you at the appropriate time.
So normally you don't have to stop subscriptions. It's done automatically.
-For your first question, yes, depending on the application it can be CPU intensive and this is why you want to avoid multiple pub/sub in chat applications, for example.
-For the second question, in your case what you need to do is template-level subscription and not router-level, in order to have the data you need only to the template you need. See this example to learn how you do it with Flow Router, and the same applies to other routers, as well.
Template.blogPost.onCreated(function() {
var self = this;
self.autorun(function() {
var postId = FlowRouter.getParam('postId');
self.subscribe('singlePost', postId);
});
});
Upvotes: 3