I am very new to Meteor and began writing a meteor app with connection to a non-web based server. As all data should be routed through this server, it has a REST interface for fetching and manipulating the relevant Objects.
Now I am stuck at how to build a collection for this web service. I tried to use the approach shown in here: https://medium.com/meteor-js/how-to-connect-meteor-js-to-an-external-api-93c0d856433b
This works for fetching the current results. But when I add/delete/update records, there is no update at all.
On the server side, I am publishing like this:
Meteor.publish('someThings', function() {
var self = this;
try {
var records = HTTP.get("http://localhost:6789/things/", {auth: "user:passwd"});
_.each(records.data, function(record) {
var thing = {
uuid: record.uuid,
title: record.title,
};
self.added('things', thing.uuid, thing);
});
self.ready();
} catch (error) {
console.log(error);
}
}
);
then globally, I have a collection:
SomeThings = new Meteor.Collection("things");
and I use it in the React component like this:
SomeThings = new Meteor.Collection("things");
getMeteorData() {
return {
things: SomeThings.find({}).fetch()
};
},
Somewhere in the client, I added this additionally (as in the howto):
Tracker.autorun(function() {
Meteor.subscribe('someThings');
});
Finally on the server side I have some functions which do the manipulation, once via the REST interface, once on the collection (example: insertion):
addThing: function(title) {
result = Meteor.http.post("http://localhost:6789/things/",
{auth: "user:passwd", params: {title:title}});
SomeThings.insert(result.data);
}
I read something about the added()
function and similar functions in Meteor.publish()
but could not understand how/if I could use this to enable "instant" synchronization between the server and client or the collection and the ui elements.
So basically I would like to know how to build a reactive collection which is not based on a database but on a REST interface instead.
Could someone give me some advice/hints on how I can achieve this?
Publications do not automatically re-run HTTP requests. You will need to somehow detect when the data in the API has changed (the simple way would be to run the query on a Meteor.setInterval
) and call this.updated
and this.removed
to notify the client of changes.