Best Way to handle a 3rd Party endpoint

I am looking at Restivus for making an API which will encapsulate a 3rd party search endpoint. Is there any method to turn a 3rd party API into a reactive data source, not by necessarily creating a collection for each resource, but is there a way to “publish a collection” by not necessarily storing it? Or is there a way to store it temporarily?

Thanks for all you do.

4 Likes

This is relatively straightforward. You set up a client-only collection and a subscription in the usual way.

RestData = new Mongo.Collection('rest-data'); // client only
Meteor.subscribe('rest-docs');

On the server, you poll the REST endpoint at a suitable interval within a roll-your-own publication. Something along the lines of:

Meteor.publish('rest-docs', function() {
  var init = true;
  var self = this;
  Meteor.setInterval(function() {
    var data = HTTP.get('some url').data; // for JSON
    if (init) {
      self.added('rest-data', 'unique_doc_id', {stuff:data});
    } else {
      self.changed('rest-data', 'unique_doc_id', {stuff:data});
    }
    self.ready();
    init = false;
  }, 10000);
});

Also, you could look at any-database, which allows you to connect to a REST endpoint.

5 Likes

This is completely awesome, thanks sir.

And if I have a method that returns an array that I want to publish? Do I modify it to add each item onto the collection, or do I have to make a Meteor.publish method inside which my method will be called? I don’t quite understand the workflow.

Let me rephrase. Can I add the Meteor.publish inside my Metero.method just before the return of the method, so that I can publish the data to the client?

So, this code you provided works, but why does it need Meteor.setInterval? Why does it not work when I use the native setInterval() provided by Node?