0

This is what we am trying to achieve, we need to call an API every five min and refresh a page in meteor and we do not wish to use MongoDB; so the API call is directly made before the template renders and the output is displayed.

Below is the code snippet - I am calling the API every 5 min and setting it in session variable during the template rendering

Template.list.rendered = function(){
    Meteor.setInterval(function(){
    Meteor.call('getListFromAPI', function(error,data){
            if(error){
                console.log(error);
            }
            else{
                Session.set('availableList', data);
            }
    });
    }, 60000);
}

Here is the helper function which returns "availableList" to HTML for rendering -

Template.list.helpers({
'getList': function(){      
    return Session.get('availableList');
}
});

Problem: It works as is meaning the page refreshes every FIVE min but the problem is when the first time the page loads, it waits for five min before the rendering.

I am fairly new to Meteor, is there any other better way I tried to move the session variable to server but session is not supported in server side. I can understand that the reactive source "session - availableList" should be moved from rendered function but I am not sure what approach to use.

Please let me know if you have any question.

1 Answers1

0

Even though you're using Meteor methods this is really just a Javascript question. See this post for how to solve it.

Basically you just need to call your method before you start your interval so it would look something like this...

Template.list.rendered = function(){
    Meteor.call('getListFromAPI', function(error,data){
        if(error){
            console.log(error);
        } 
        else {
            Session.set('availableList', data);
        }
    }
    Meteor.setInterval(function(){
        Meteor.call('getListFromAPI', function(error,data){
            if(error){
                console.log(error);
            }
            else{
                Session.set('availableList', data);
            }
        });
    }, 60000);
}

Also just as a side note and I'm pretty sure you already know this... but 60000 ms is only 1 minute.

Community
  • 1
  • 1
Shaded
  • 17,276
  • 8
  • 37
  • 62