Question

Is there a way to send events between two Meteor servers? I know I can connect Server1 to Server2 (and vice versa) using DDP.connect and just call methods between the two servers. This will not work for me, because one of my servers (Server1) will be at my house behind a dynamic IP and firewall. DDP.connect requires a url. What is the best way, if any, to communicate between the two servers? I am thinking the only way to do this would be something like Socket.io where Server1 connects to Server2. I'm not sure if this can be done in Meteor though. Thanks.

Was it helpful?

Solution

You can do this by using DDP.connect to connect server 1 to server 2 on startup, then subscribing to a collection that server 2 publishes, for example:

On Server 2 (known URL):

var Events = new Meteor.Collection("events");

Meteor.publish("events", function () {
  return Events.find({});
}

On Server 1 (at your house):

var EventConnection = DDP.connect("<server 2 URL>");
var Events = new Meteor.Collection("events", {connection: EventConnection});

EventConnection.subscribe("events");

Events.find({}).observe({
  added: function (newEvent) {
    // do something with newEvent
  }
});

Then, whenever server 2 adds an object to the Events collection, you will get it on server 1 via the connection. Watch out, though - every time server 1 connects to server 2 it will get all previous events as well. If you don't want that to happen, you need to use the ready callback on subscribe:

Revised code for Server 1:

var EventConnection = DDP.connect("<server 2 URL>");
var Events = new Meteor.Collection("events", {connection: EventConnection});

EventConnection.subscribe("events", function () {
  Events.find({}).observe({
    added: function (newEvent) {
      // do something with newEvent
    }
  });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top