title: AngularJS: server call on tab/browser close
body: <p>I have a section in my application where I establish a communication between the server and the client that loops forever until the user leaves this screen. When the user leaves the screen, I detect this issue with <code>$rootScope.$on('$stateChangeStart')</code> and send a $http call to stop the process in the server.</p>
  
  <p>I want to perform that same $http when the user closes the current tab or browser but I cannot get this done.</p>
  
  <p>First of all this is the service function that makes the $http call:</p>
  
  <pre><code>function cancelCommProcess(webId, commActionId) {
            return $http.get(BASE_URL + '/rest/comm/kill-current-com-process/' + webId + '/' + commActionId)
              .then(
                      function(response) {
                          return response;
                      }
              );
        }
  </code></pre>
  
  <p>As I said, when I call this function in the <code>$rootScope.$on('$stateChangeStart')</code> or some other point, it works.</p>
  
  <p>Now I have the following code to call this function when tab/browser is closed:</p>
  
  <pre><code>$window.onbeforeunload = function (evt) {
              if ($rootScope.CMC_ACTION_IDENTIFIER.CMC_ACTION_MONITORING === currentCommActionId) {
                  CMCService.cancelCommProcess(currentWebId, currentCommActionId)
                      .then(
                              function() {
                                  console.log('HELLO');
                              }
                      );
              }
          }
  </code></pre>
  
  <p>This way the $http call doesn't reach the server, but if I simply made the next change, then it works:</p>
  
  <pre><code>$window.onbeforeunload = function (evt) {
              if ($rootScope.CMC_ACTION_IDENTIFIER.CMC_ACTION_MONITORING === currentCommActionId) {
                  CMCService.cancelCommProcess(currentWebId, currentCommActionId)
                      .then(
                              function() {
                                  myTest();
                              }
                      );
              }
          }
  
          function myTest() {
              console.log('HELLO');
          }
  </code></pre>
  
  <p>Even more, previous method works in Chrome and Firefox but not in Edge. But it works (I mean, the $http call reaches the server) if I do it like this:</p>
  
  <pre><code>$window.onbeforeunload = function (evt) {
      if ($rootScope.CMC_ACTION_IDENTIFIER.CMC_ACTION_MONITORING === currentCommActionId) {
          CMCService.cancelCommProcess(currentWebId, currentCommActionId)
              .then(
                      function() {
                          myTest();
                          //console.log('HELLO');
                      }
              );
      }
      return "";
  }
  
  function myTest() {
      console.log('HELLO');
  }
  </code></pre>
  
  <p>Of course I get the confirm message to leave or stay the page, but I don't know why the call reaches the server just by showing this message.</p>
  
  <p>I don't really understand how this works so I appreciate your help. The idea of the $http call is to tell the server to interrupt the current communication process but I feel this method is not truthful.</p>
  
  <p>Would you help me to figure out what's the best approach for this?</p>
  
  <p>Thank you.</p>