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

查看:21
本文介绍了使用 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?

推荐答案

我从这个问题中得到了一个线索如何正确使用 Meteor.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:

更新

问题已解决

更新 2

这是一个使用 Meteor 0.6.6.2 测试的示例项目 https://github.com/camelosw/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天全站免登陆