title: How to use a client side reconnection event in Meteor
body: <p>How do I use a client side reconnection event in Meteor.</p>
  
  <blockquote>
    <p>On the client, Meteor.apply takes a new wait option, which ensures
    that no further method calls are sent to the server until this method
    is finished; it is used for login and logout methods in order to keep
    the user ID well-defined. You can also specifiy an onReconnect handler
    which is run when re-establishing a connection; Meteor Accounts uses
    this to log back in on reconnect.</p>
  </blockquote>
  
  <p>Can someone provide an example.</p>
  
  <p>Here's the example in the accounts package.</p>
  
  <pre><code>  Accounts._makeClientLoggedIn = function(userId, token) {
      Accounts._storeLoginToken(userId, token);
      Meteor.default_connection.setUserId(userId);
      Meteor.default_connection.onReconnect = function() {
        Meteor.apply('login', [{resume: token}], {wait: true}, function(error, result) {
          if (error) {
            Accounts._makeClientLoggedOut();
            throw error;
          } else {
            // nothing to do
          }
        });
      };
      userLoadedListeners.invalidateAll();
      if (currentUserSubscriptionData) {
        currentUserSubscriptionData.handle.stop();
      }
      var data = currentUserSubscriptionData = {loaded: false};
      data.handle = Meteor.subscribe(
        "meteor.currentUser", function () {
          // Important! We use "data" here, not "currentUserSubscriptionData", so
          // that if we log out and in again before this subscription is ready, we
          // don't make currentUserSubscriptionData look ready just because this
          // older iteration of subscribing is ready.
          data.loaded = true;
          userLoadedListeners.invalidateAll();
        });
    };
  </code></pre>
  
  <p>I assume you can't just define another default_connection.onReconnect if you want the accounts one to still work?</p>
  
  <p>Thanks.</p>
  
  <p><strong>Edit</strong>:</p>
  
  <p>Thinking about it a bit more, instead of using onReconnect are you perhaps supposed to use <code>Meteor.status()</code> instead?</p>