Backbone.js Poller

Lets say you have a Backbone.js based messaging system on your application, and you want to stay synced with the server so you could alert your users when a new message arrives. You would like to call the server once every couple of seconds and ask for status updates.
Or maybe your model represents an on going background task, you want to poll for status updates.
You could start with something basic such as:

function poll(collection, delay) {
  setInterval(function(){
    collection.fetch({data: {foo: 'bar'}});
  }, delay);
}

This works, but has some flaws:

  1. You may end up running simultaneous requests hitting the browser’s allowed connection per host limit (typically 6, 2 on IE7, according to browserscope)
  2. Nothing prevents you from running the function multiple time with the same collection
  3. You can’t easily stop the process, and abort running requests
  4. It doesn’t handle cases where we want to stop polling a model on a condition (say, when it’s task is done)

This may results in unnecessary load on the server side

You could run the next request only upon successful fetch request, and write something like this:

function poll(collection, delay) {
  collection.fetch({
    data: {foo: 'bar'},
    success: function(){
      setTimeout(function(){
        poll(collection, delay);
      }, delay)
    }
  });
}

This solves #1 bit doesn’t help other issues.
It takes a little more than that, and starts smelling like a special helper is needed.
You could add a poll() and unpoll() methods to your base models or collections using inheritance or mixin.
I wouldn’t like to promote such approach since it breaks the model’s interface and introduces tight coupling between the model and application state.
This easily becomes a slippery way down to maintenance hell.
A better pattern would be to use an external helper that will keep our interfaces clean and our models stateless and decoupled.

I ended up writing a Backbone Poller, A polling helper utility for Backbone.js.
a basic usage for our messaging system will could like

var inbox = new InboxCollection();
var poller = Backbone.Poller.get(inbox, {
    delay: 2000, 
    data: {sort: 'date desc'}
});
poller.on('success', function(){
    // do something on every successful fetch
});
poller.start();

A more advanced usage for our long going offline task could look like

var options = {
    delay: 2000,
    // when this becomes false the poller will stop
    condition: function(model){
        return model.get('done') !== true; 
    }
};

var task = new OfflineTaskModel();
var poller = Backbone.Poller.get(task, options);
poller.on('complete', function(){
    // task is done, do something
});
task.set('command', 'some-long-procedure');
task.save({},{
    success: function(){
        poller.start();
        
    }
});

The code and examples are available under the MIT license in github.

6 thoughts on “Backbone.js Poller

  1. joel

    Hi, very good library, just that it doesn’t work after setting a new value on options….
    var poller = Backbone.Poller.get(model, { delay: 1000 });
    poller.on(‘success’, function(){
    if(condition){
    poller.set({delay: 2000}).start();
    }
    });
    poller.start();

    Reply
  2. joel

    Yeah it is a valid statement but the poller stops after setting the new value, even if I start it manually it doesn’t run again. What I did was to change this line:
    poller.set({delay: 2000}).start();
    by this:
    poller.options.delay=2000;

    Reply
  3. Ashley Morris

    I like tthe helpful information you provide on your articles.
    I’ll bookmark your weblog and test once more here regularly.
    I’m somewhat certain I wull be told many new stuff proper right here!

    Good luck for the following!

    Reply
  4. knowledge youtube channels

    It may seem acceptable for a government to demannd that
    an internet service provider (ISP) report any customer who might download child pornography.
    For example, inn advertising, while actual lies arre often avoided (if, for few ther
    reason, than nervous about legal ramifications), how these
    facts are interpreted often creates the desired spin. Largest English speakng IT talent huub
    on thee planet, based on NASSCOM report.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *