Connect two Meteor applications using DDP

走远了吗. 提交于 2019-12-28 10:07:25

问题


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?


回答1:


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         
});

Now I can watch for changes in application 1 from application 2 using Items.find().observe()

Warning

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

Update

The bug was solved

Update 2

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



来源:https://stackoverflow.com/questions/18358526/connect-two-meteor-applications-using-ddp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!