Meteor call method issue

I’m facing some problem with meteor methods in reactJS. I’m calling the method getData from the componentWillMount method.

Code -

componentWillMount() {
this.intervalCallback = setInterval(()=> this.getData(), 1000)
}

getData() {
// Some skip or limit logic
// clear the interval when method called 5 times
Meteor.call(‘getReportData’, {skip, limit}, (err, response) => {
// err & response logic (setState)
})
}

As you can see above, In my case getData method called 5 times as per mention duration and server process the request one by one. The above logic executes on the route ‘/report’ and there are some more routes in the application i.e ‘/dashboard’, ‘/settings’ etc.

The problem occurs when I switch the route, while 5 methods response processing is in progress Because of the method called 5 times, server busy to handle those requests and not respond to the method which is called on the new route (In which I switched).

How to handle this kind of situation in the meteor ? Meteor method should be stopped in the server side If component unmount.

Would be really appreciated If someone has the better idea of resolving this issue.

First off, why do you need to make the same meteor call 5 times for each route?!?

I’d start by not doing that before looking at more complex solutions.

Is there a reason you aren’t just publishing the data to the client and using minimongo?

Otherwise, you can tell Meteor to run calls asynchronously instead of in order by using this.unblock() in your Method.
Only do this if the order of returned results is not important as they may return in any order.

1 Like

@coagmano Thanks for the reply. I’ve to call that method five times one by one as per the set interval and can’t use minimongo because data is coming from 3rd party APIs. Also, can’t use this.unblock() because it still makes server busy.

Is there any other way to tell the meteor server to stop the ‘getReportData’ method execution and move on to server new request ?

Unfortunately there’s no way to cancel a method call (or any other kind of call in js).
((Unless you can break the task into small chunks that are processed in a loop that checks if it should continue on each iteration.))

Can’t you query all the APIs together and return the aggregated results?
Or poll the APIs and keep a cache of the last results that you can quickly send to the client?

@coagmano I can’t query all API together. Do you any meteor developer support member?

Why don’t you do the queries of the remote API’s and cache the results in your local database? That way you don’t have to refetch the data every time your user clicks around

@mikkelking data is not fixed on the remove server. It changes everytime. That’s why I’m not storing the result in local DB.

Can you place the method (on the server) in a try-catch block and throw an error when you unmount the component?

@AnnotatedJS But It would be on front-end side. Can not do any impact on server side?

@shubhama I know. That’s just a minor detail you have to resolve :grinning:

But first, something simpler. @coagmano wrote,

Unless you can break the task into small chunks that are processed in a loop that checks if it should continue on each iteration.

And that is of course possible given you have 1 method called 5 times. That would give a significant performance improvement?

1 Like