Xyand
Xyand

Reputation: 4488

Detecting when a subscription is cancelled in Meteor

How can I detect when a subscription is cancelled?

There are many ways it can be cancelled and I would like to analyze cancellation/subscription behavior.

Upvotes: 1

Views: 119

Answers (1)

travellingprog
travellingprog

Reputation: 1189

Set a handle to your collection subscription, e.g.

var subHandle = Meteor.subscribe('subscription-name')

Subscription handles have a method called ready() that is a reactive data source. If the subscription has been cancelled, it will return false. Because it is a reactive data source, you could place an if statement inside a reactive computation to detect when the the subscription is cancelled.

Example:

Deps.autorun(function() {
  if (subHandle && (! subHandle.ready())) {
    // subscription has been cancelled
  }
});

However, make sure you run this function after subHandle has been defined. This computation will not register with a reactive dependency until it calls subHandle.ready().

Upvotes: 1

Related Questions