Force subscription rerun

Helo Everybody!,
Sometimes I want to force a subscription to be reruned, even if the parameters are the same. But Meteor is “intelligent” and it thinks that is the same subscription so it reuse the observer and nothing is done. That’s because I want to do a reactive join when the subscriptoion is ready:

Template.projectDetailsInvite.onCreated(function () {
    var instance = this;
    instance.autorun(function () {

        var project = Template.currentData().project;
        instance.subscribe(
            'a_subscription',
            State.getCompanyId(), project._id,
            function () {
               // I want this to be rerruned, so i need the subscription to be done.
              // The data of Secure.find... depends on the data of the subscription.
                var invitableIds = Secure.findInvitableUsersToProject(State.getCompanyId(), project._id);
                instance.subscribe('userList', invitableIds);
            }
        );
    });

});

I “fix” it adding a parameter to the subscription, Random.fraction() for example, so the subscription thinks that the parameters are different and rerun. Is there any way better to do this? I hope yes :smiley:

Thanks!

1 Like

You could do this from the client by calling .stop(); on the subscription handler and then subscribe again.

http://docs.meteor.com/#/full/meteor_subscribe

"Meteor.subscribe returns a subscription handle, which is an object with the following properties:

stop()
Cancel the subscription. This will typically result in the server directing the client to remove the subscription’s data from the client’s cache."

Alternatively, you could create an in-memory Collection by passing the constructor null.

ResetCollection = new Meteor.Collection(null);

Then you could register a dependency in your subscription with a find(); call and trigger resets by having clients/servers insert() into the Collection.

Meteor.publish('my_publish', function(id) {

ResetCollection.find({});

  return RealData.find({
    _id : id});

});

Note: I haven’t actually tested this approach, its just a theory.

I also use a dummy parameter to rerun subscription in case of reactive joins managed from the client:

var dummy = 0;
self.autorun(function () {
  self.subscribe(..., dummy++);
  ...
});
2 Likes

In my opinión, there should be something like a flag or a method inside
the publication like, this.alwaysRerun() if you want to rerun a publication
always. But maybe the flag is better:
subscribe (name, params, flags, callback)?

Anyone find an actual solution to this?

What is your use case?

Hi Rob,

I ended up creating another topic outlining my use case in detail here: Clearing minimongo - is it possible without a page refresh?

1 Like