Search code examples
meteorddp

How do I wait for successful connection using DDP in meteor (server -> server)


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


Solution

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