首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用DDP连接两个Meteor应用程序

使用DDP连接两个Meteor应用程序
EN

Stack Overflow用户
提问于 2013-08-21 21:17:29
回答 1查看 9.6K关注 0票数 16

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

在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
  } 
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-21 22:39:04

我从这个问题中得到了一个线索How to properly use Meteor.connect() to connect with another Meteor server。我错过了它,因为它是关于更改为DDP.connect()的旧Meteor.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()从应用程序2监视应用程序1中的更改

警告

Meteor上有一个bug,它将停止应用程序之间的连接:

更新

这个bug已经解决了

更新2

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

票数 34
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18358526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档