Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect two Meteor applications using DDP

Tags:

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.

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?

like image 214
Camilo Avatar asked Aug 21 '13 13:08

Camilo


1 Answers

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

like image 148
Camilo Avatar answered Nov 01 '22 07:11

Camilo