Does subscription download collection each times is it call?


#1

Hello Meteor community !

I try to make an apps which can manage patients files.

For now I do my subscription in the router.js inside the lib directory:

Router.route("/patientelle", {
    name: "patientsList",
    subscriptions: function(){

	if (Session.get("trashMode"))
	    return Meteor.subscribe("MyOldPatients");
	else
	    return Meteor.subscribe("MyPatients");

    }
});

All works good, but after some times on working on my project my ram memory explode.
I have to shut the page and launch it again.

I assume that is it due to my multiple subscriptions ?
Maybe it’s not a good idea to subscribe here?
What if I subscribe only once ? ( because I try but it doesn’t work…)

Thanks for helping newbie :slight_smile:


#2

I suggest reading the Meteor Guide on subscriptions:


#3

Thanks @robfollows,

I already read this doc, but I reread it and I find the stop() function, so I did that :

Router.route("/patientelle", {
    name: "patientsList",
    onStop: function() {
	Session.get("subscriptionHandler").stop();
    },
    subscriptions: function(){

	 let handler = Meteor.subscribe("MyPatients");
	 Session.set("subscriptionHandler",handler);
	 return handler;

    }
});

But this doesn’t work, (no function stop()), but I also see

However, 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.

Is my utilization of subscribe does not enter in this condition ?

I’m sorry I surely don’t understand something simple…


#4

Noticed you are using Iron Router… I would strongly recommend you not to use it - too much complexity. Try FlowRouter instead, and handle the subscriptions at the template level. See this: https://guide.meteor.com/data-loading.html#organizing-subscriptions


#5

Ok, I will seek in this direction, thank you , I have to make big changes, but I will come back here when I found the solution. :slightly_smiling_face:

See you later community


#6

Hello again, I tried lot of stuff and read a lot, I’ve the impression to understand what I did (continuing using iron:router (sorry @hluz, but the solution I “found” used it) ).
The problem is with what I understand, I stop my subscription, so logically, my memory shouldn’t be getting more and more full (until explosion) anymore. But it did…

I show you where I am now:

Router.route("/patientsList", {
    name: "patientsList",
    waitOn: function() {
	let cursor = Meteor.subscribe("MyPatients");
	this.params.cursor = cursor;
	return cursor;
    },
    action: function() { 
	this.render("patientsList");
    },
    onStop: function() {
	this.params.cursor.stop();
    }
});

And I m reasonably certain that the stopping of subscription is done.
So why my memory is overload ?

:sweat:

I surely miss something…


#7

Are you sure you want to keep changing your subscription? There are other ways, some examples:

  1. Just subscribe to all and then filter old/new patients in the find() method.

  2. Use a reactive variable for new/old patients and use it as a parameter for the subscription:

    Meteor.subscribe(‘chat’, { room: Session.get(‘currentRoom’) });

Example is from bottom here: https://docs.meteor.com/api/pubsub.html#Meteor-subscribe


#8

Ok, I didn’t need to subscribe each time, it’s really not useful so thanks @lucfranken

now I just subscribe once in the Template.mainlayout.onCreated() fonction, and my app is faster !

But it doesn’t remove the memory overload problem…

Maybe I don’t really care (but I do…) because in real use of my app, you don’t have to reload the page 10 times by minutes…

Thanks everybody !