16

I have two applications that I need to synchronise. One of them will receive data from users and the other will display the data. Both applications will work on different servers. They could be disconnected at some times and they need to continue working until reconnect, so I will replicate the data from the first application on the second application.

On Meteor documentation I found DDP.connect(url)but I'm not sure how to use it. I found many questions and examples connecting non Meteor applications with Meteor using DDP, but nothing about connecting two Meteor applications.

My first approach was something like this:

Application 1

Items = new Meteor.Collection('items');
Items.insert({name: 'item 1'});
if (Meteor.isServer) {
  Meteor.publish('items', function() {
    return Items.find();
  });
}

Application 2

Items = new Meteor.Collection('items')
if (Meteor.isServer) {
  var remote = DDP.connect('http://server1.com/);
  remote.onReconnect = function() {
    remote.subscribe('items');
    var items = Items.find();
    console.log(items.count());  // expected to be 1 but get 0
  } 
}

On the second application, how can I get the items from the first application?

4

1 回答 1

34

我从这个问题How to proper use Meteor.connect() to connect with another Meteor server中得到了线索。我错过了它,因为它是关于旧Meteor.connect()的更改为DDP.connect().

这适用于客户端和服务器

var remote = DDP.connect('http://server1.com/');
Items = new Meteor.Collection('items', remote); 

remote.subscribe('items', function() {
  var items = Items.find();
  console.log(items.count());  // get 1         
});

现在我可以使用应用程序 2 观察应用程序 1 的变化Items.find().observe()

警告

Meteor 上有一个错误会停止应用程序之间的连接:

更新

错误已解决

更新 2

这是一个使用 Meteor 0.6.6.2 https://github.com/camilosw/ddp-servers-test测试的示例项目

于 2013-08-21T14:39:03.797 回答