Ending Device Event Stream

I would like to end the device event stream after calling the code below:

particle.getEventStream({ deviceId: 'mine', auth: token }).then(function(stream) {
  stream.on('event', function(data) {
    console.log("Event: " + data);
  });
});

I have tried stream.abort() and stream.end() as below:

particle.getEventStream({ deviceId: 'mine', auth: token }).then(function(stream) {
  stream.abort() ;
OR
stream.end();
  });
});

Not working. events from particle are still coming in. can some one help me with this.? thanks.

With calling particle.getEventStream() a second time you are creating a new stream which you end/abort, but that has no effect on any previously created stream (which is a seperate instance).

You need to act on the original instance to make your end/abort work on that one.

@ScruffR, thanks for the suggestion. Can you tell me how I can keep track of the instances? I don’t see anything similar to it returning from particle data object.

I’m not really into these promises (or JS at all :blush:), but from my naive JS understanding you get the stream object in the then() callback.
There - I guess - you could assign that to a “global” variable which can be reused later on when you want to kill that stream.

I’m sure there are more elegant ways, but that’s what I’d do without really knowing what I’m doing.

@ScruffR, I look at the call back data object there is no instance id. I am only getting this:

console.log(" Event: " + JSON.stringify(data));

***return data
{“data” {…}",“ttl”:“60”,“published_at”:“2016-11-18T17:23:20.953Z”,“coreid”:"…",“name”:“Settings”}

Isn’t that “stream” the Stream object you’re trying to end (I’m not a JS programmer either, but I think this is what @ScruffR is talking about )? Can’t you set a global variable equal to that, and then call end or abort on that global?

particle.getEventStream({ deviceId: 'mine', auth: token }).then(function(stream) {
  myGlobalStreamObject = stream;
  stream.on('event', function(data) {
    console.log("Event: " + data);
  });
});
1 Like