DDP: how do I wait for a connection?

I basically want to do something like this, but can’t figure out how:

Meteor.connectToRemote = function (remoteUrl) {
  return new Promise(function (resolve, reject) {
    let remoteConnection = DDP.connect(remoteUrl);
    remoteConnection.on('connect', function () {
      Meteor.connection = this;
      Accounts.connection = this;
    remoteConnection.on('connection_failed', reject);

Found the solution:

Meteor.connectToRemote = function (remoteUrl) {
  return new Promise(function (resolve, reject) {
    let remote = DDP.connect(remoteUrl);
    Tracker.autorun(() => {
      if (remote.status().connected) {
        Meteor.connection = remote;
        Accounts.connection = remote;
      } else if (remote.status().offline) {
        reject(new Meteor.Error(503, "Connection unavailable"));

Actually, you don’t need any of those. Check http://docs.meteor.com/#/full/ddp_connect and http://docs.meteor.com/#/full/meteor_status

DDP.connect returns an object which provides:

  • subscribe - Subscribe to a record set. See Meteor.subscribe.
  • call - Invoke a method. See Meteor.call.
  • apply - Invoke a method with an argument array. See Meteor.apply.
  • methods - Define client-only stubs for methods defined on the remote server. See Meteor.methods.
  • status - Get the current connection status. See Meteor.status.
  • reconnect - See Meteor.reconnect.
  • disconnect - See Meteor.disconnect.
  • onReconnect - Set this to a function to be called as the first step of reconnecting. This function can call methods which will be executed before any other outstanding methods. For example, this can be used to re-establish the appropriate authentication context on the new connection.

So basically, you can do

Meteor.connectToRemote = DDP.connect(remoteUrl);
  if (Meteor.connectToRemote.status === 'connected') 
    // do stuff
1 Like