title: Terminate EventSource event listener?
body: <p>I'm trying to work around a problem to do with rest streaming between the Nest API and a service (ST) that does not support streaming.</p>
  
  <p>To get around this, I have built a service on Sails which takes a post request from ST containing the Nest Token, and then triggers an EventSource event listener that sends the data back to ST.</p>
  
  <p>It is heavily based off the Nest rest-streaming example here:
  <a href="https://github.com/nestlabs/rest-streaming" rel="nofollow noreferrer">https://github.com/nestlabs/rest-streaming</a> and my code is as follows:</p>
  
  <pre><code>    startStream: function(req, res) {
          var nestToken = req.body.nestToken,
                  stToken = req.body.stToken,
                  endpointURL = req.body.endpointURL,
                  source = new EventSource(sails.config.nest.nest_api_url + '?auth=' + nestToken);
  
  
          source.addEventListener('put', function(e) {
              var d = JSON.parse(e.data);
              var data = { devices: d.data.devices, structures: d.data.structures},
                      config = { headers : {'Authorization': 'Bearer ' + stToken}};
  
              sendData(endpointURL, data, config);
          });
  
          source.addEventListener('open', function(e) {
              console.log("Connection opened");
          });
  
          source.addEventListener('auth_revoked', function(e){
              console.log("Auth token revoed");
          });
  
          source.addEventListener('error', function(e) {
          if (e.readyState == EventSource.CLOSED) {
            console.error('Connection was closed! ', e);
          } else {
            console.error('An unknown error occurred: ', e);
          }
        }, false);
      }
  };
  </code></pre>
  
  <p>The problem I foresee though is that once a request is received by the node server, it start the event listener, however I cannot for the life of me figure out how I can kill the event listener.</p>
  
  <p>If I cannot figure out a way to stop this, then every EventListener will run indefinitely which is obviously not suitable.</p>
  
  <p>Has anyone got any suggestions on how to overcome the issue? </p>