0

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.

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.

It is heavily based off the Nest rest-streaming example here: https://github.com/nestlabs/rest-streaming and my code is as follows:

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

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.

If I cannot figure out a way to stop this, then every EventListener will run indefinitely which is obviously not suitable.

Has anyone got any suggestions on how to overcome the issue?

K20GH
  • 6,032
  • 20
  • 78
  • 118
  • To check I understood: your "put" event listener will only ever get called once? And after that you don't need the `EventSource` object any more? – Darren Cook Jan 18 '17 at 08:49
  • So I have my Controller as above that can be access by several thousand users. Each user calling that function creates an event listener for their unique API. The issue is, how do I manage each of these listeners. ie, User XYZ wants to turn off event streaming, how do I access their specific event listener and stop it from running. The Put event is called every time the Nest API updates, which is where i forward the data on – K20GH Jan 18 '17 at 08:52

1 Answers1

2

Each SSH client connection is a dedicated socket.

If a particular client doesn't want event streaming, don't make the connection. If they start event streaming, but want to turn it off, call source.close();source=NULL;

If from server-side you want to stop sending the messages, close the socket. You didn't show the server-side code, but if it is running a dedicated process per SSE client then you just exit the process. If you are maintaining a list of sockets, one per connected client, close the socket. On node.js you might be running a function on setInterval. To close the connection you do and clearInterval() and response.end();.

Darren Cook
  • 27,837
  • 13
  • 117
  • 217
  • The issue isn't between my Node server and the Nest API. I am starting my Node event listener by receiving a POST call from the user. That call contains their ST endpoint where the data is sent to using another POST call. The issue is that ST cannot use Sockets, so it relies on HTTP requests to send the data to it, so I cannot easily just close the user - Node connection. I need a way that the user can make a HTTP request to my node server to cancel their Nest listener – K20GH Jan 18 '17 at 10:21