Does subscription download collection each times is it call?

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:

I suggest reading the Meteor Guide on subscriptions:

2 Likes

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ā€¦

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

3 Likes

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

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ā€¦

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

1 Like

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 !

1 Like