3

I'd like to synchronize Data between two Meteor apps. Therefore I have published a collection with the data in question on both apps (which obviously run the same Meteor version 0.8.1.2 with the exact same packages).

When I run

var testConnection = DDP.connect('http://10.0.10.20:3003/');
var newCollection = new Meteor.Collection('remoteData', testConnection);
testConnection.subscribe('remoteData');
console.log('Data list starts here:');
newCollection.find().forEach(function(data){console.log(data)});

on any client I do get a list of all data like expected. Server side there is nothing so newCollection stays empty (also I know from debugging that the server does actually execute testConnection.subscribe('remoteData') and the other server executes everything within its corresponding publish function just like for clients).

I tried it this way as the poster here https://stackoverflow.com/a/18360441 mentioned something like this works on client and server. Looking in the docs for subscribe ( http://docs.meteor.com/#meteor_subscribe ) it says it only works on the client which would explain that nothing happens on my server but would be a bit strange as DDP.connect ( http://docs.meteor.com/#ddp_connect ) seems to be meant for client and server and supports subscribe.

So do I miss something here? And what would be the best way to get a subscribe like functionality between two servers if subscribe really does not work in this scenario? I know I can work with custom Meteor.methods but this seems a bit like a crutch compared to how nice it would work with subscribe, so I would be very interested in any better solution...

Community
  • 1
  • 1
Jey DWork
  • 1,746
  • 2
  • 16
  • 29
  • Maybe try `newCollection.find()` inside the callback to subscribe to make sure that you are just not querying the collection before it has data. I am a bit surprised the collection is not empty on the client also without any need for callbacks or waiting. – user728291 May 22 '14 at 12:04
  • oh man, thanks a lot! Your suggestion works as well as waiting some time with Meteor.setTimeout or anything else. Maybe on the client find() always waits for a collections ready()? I don't know but because it worked on the client I wasn't thinking the problem was something like that... – Jey DWork May 22 '14 at 13:50
  • Surprisinly when `autopublish` package is used, there's no need to do `.find()` in the callback – Nyxynyx Sep 13 '14 at 17:40

1 Answers1

3

Like user728291 pointed out the problem was that the server in this case isn't waiting for this.ready() in the publish function on the other side and therefore when newCollection.find() is called on the server newCollection still is empty (but will receive data shortly after). It seems that on the client newCollection.find() tries to wait for this.ready() of the servers publish function (also I'm absolutely not sure about this, maybe the reason it works on the client is a totally different one) and therefore on the client it isn't empty at that time.

Anyhow, you are on the safe side when you always trigger find() in the callback of subscribe which will interpret any function as onReady callback (http://docs.meteor.com/#meteor_subscribe).

So what guaranteed works on server and client is

var testConnection = DDP.connect('http://10.0.10.20:3003/');
var newCollection = new Meteor.Collection('remoteData', testConnection);
testConnection.subscribe('remoteData', function() {
    console.log('Data list starts here:');
    newCollection.find().forEach(function(data){console.log(data)});
});
Jey DWork
  • 1,746
  • 2
  • 16
  • 29