1

Continuing the discussion from DDP: how do I wait for a connection?:

Based on the thread above, we an leverage Tracker.autorun to wait and confirm for a successful connection between a client and meteor server.

I would like to do the same on a server : server connection

Basically, I have a meteor server (server1), that will need to “test” and see if another meteor server (server2) is available.

Each time I run DDP.connect(remoteUrl).status() within server1’s meteor method, it always says “connection”. I know it connects in the next second or two, but I’m unable to wait for checking the connection success flag.

How do i do this on the server?

Thanks

blueren
  • 2,730
  • 4
  • 30
  • 47

1 Answers1

2

The idea of reactivity doesn't exist in this form on the server, so something like the Tracker is not an option. Fortunately though there is the onReconnect callback you can use. You can steal the required logic from my meteor-serversync package:

const connection = DDP.connect(URL);
connection.onReconnect = function() {
  console.log("(re)connected");
  if (!initialized) {
    options.onConnect && options.onConnect();
  }
};
Christian Fritz
  • 20,641
  • 3
  • 42
  • 71
  • 1
    Stolen, thanks! :) – blueren Jan 03 '22 at 17:23
  • A continuation question.. Is there a way to run this in a loop? I have an array of URLS to check for connectivity. And then update a db record based on the outcome. Something doesn't seem to work right when I put the above code in a loop. I have a feeling the responses are not coming in the same order as it was sent. – blueren Jan 04 '22 at 05:45
  • It should be possible, but I'd need to see your code to tell why it might be failing. Can you open a new question? – Christian Fritz Jan 04 '22 at 15:20