【问题标题】:Connect two Meteor applications using DDP使用 DDP 连接两个 Meteor 应用程序
【发布时间】:2018-06-03 02:05:38
【问题描述】:

我有两个应用程序需要同步。其中一个将接收来自用户的数据,另一个将显示数据。这两个应用程序将在不同的服务器上运行。它们有时可能会断开连接,并且需要继续工作直到重新连接,因此我会将第一个应用程序的数据复制到第二个应用程序上。

在 Meteor 文档中,我找到了 DDP.connect(url)但我不确定如何使用它。我发现了许多使用 DDP 将非 Meteor 应用程序与 Meteor 连接起来的问题和示例,但对于连接两个 Meteor 应用程序却一无所获。

我的第一个方法是这样的:

应用 1

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

应用 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
  } 
}

在第二个应用程序中,我如何从第一个应用程序中获取项目?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    我从这个问题How to properly 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         
    });
    

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

    警告

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

    更新

    问题已解决

    更新 2

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

    【讨论】:

    • 问题 (1543) 现在似乎已修复。 (流星>0.8.3)
    • @Camilo 上面的代码是在客户端还是服务器端?
    • @Haikal Nashuha 它对双方都有效。你可以在这里看到一个在服务器端工作的例子https://github.com/camilosw/ddp-servers-test
    • 为什么这是必要的?他不能提供外部mongo源吗? MONGO_URL=mongodb://anotherserver:8000/db_name 只想知道什么是最佳实践。
    • @user1623481 在某些情况下,您只想将一部分数据公开给另一个应用程序,而不是公开整个数据库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 2017-08-02
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多