使用DDP连接两个Meteor应用程序 [英] Connect two Meteor applications using DDP

查看:86
本文介绍了使用DDP连接两个Meteor应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我在Meteor文档上找到了DDP.connect(url),但是我不确定如何使用它.我发现了许多问题和示例,这些问题和示例使用DDP将非Meteor应用程序与Meteor连接起来,但是与连接两个Meteor应用程序无关.

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:

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

在第二个应用程序上,如何从第一个应用程序中获取物品?

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

推荐答案

我从这个问题中得到了一个线索如何正确使用流星. connect()与另一个Meteor服务器连接.我想念它的原因是因为原来的Meteor.connect()更改为DDP.connect().

I got a clue from this question How to properly use Meteor.connect() to connect with another Meteor server. I missed it because it was about the old Meteor.connect() that changed to DDP.connect().

这适用于客户端和服务器

This worked on client and server

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()

警告

Meteor上存在一个错误,该错误将停止应用程序之间的连接:

There is a bug on Meteor that will stop the connection between applications:

  • https://github.com/meteor/meteor/issues/1543
  • DDP between two servers doesn't reconnect

更新

该错误已解决

更新2

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

This is a sample project tested with Meteor 0.6.6.2 https://github.com/camilosw/ddp-servers-test

这篇关于使用DDP连接两个Meteor应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆