Skip to content
This repository has been archived by the owner on Apr 27, 2019. It is now read-only.

getEventStream stops returning events after a long time #72

Closed
dgmltn opened this issue Jul 30, 2015 · 8 comments
Closed

getEventStream stops returning events after a long time #72

dgmltn opened this issue Jul 30, 2015 · 8 comments

Comments

@dgmltn
Copy link

dgmltn commented Jul 30, 2015

I have a motion detector attached to my spark core, that outputs events like:

Spark.publish("motion-detected", NULL, 60, PRIVATE);

I'm using this script to monitor it:

https://github.com/dgmltn/pimatic-particle/blob/master/test-eventstream.js

It stops logging events after a long time (after a day or two). I noticed the same issue when using the EventSource js module instead of the official sparkjs module. I know it's a problem with the client, because if I restart the client script, it continues chugging along.

@lourd
Copy link
Contributor

lourd commented Jul 31, 2015

Not that this is a fix, but you can explicitly restart it if it gets an end event. This is in some of the examples:

function openStream() {
  const req = Spark.getEventStream( false, 'mine', function(...) {...}  );
  req.on('end', function() {
    console.warn("Spark event stream ended! re-opening in 3 seconds...");
    setTimeout(openStream, 3 * 1000);
  });
}

@mfferreira
Copy link

Bump.

Is there a way to explicitly kill the current stream?

@lourd
Copy link
Contributor

lourd commented Jan 19, 2016

Spark.getEventStream returns an instance of Request. I thought closing it was as simple as calling end or destroy but having played around with that in the debugger neither of those actually work... ❓

This is the key snippet I was using in conjunction with Node Inspector to play around.

  const stream = particle.getEventStream(false, 'mine', handler);

  // Sometimes the stream ends, we want to be sure to restart it
  stream.on('end', () => {
    console.log('Particle event stream ended! Restarting in 3 seconds...');
    // TODO track the stream closing event
    setTimeout(openEventStream, 3000);
  });

  setTimeout(() => {
    const s = stream; // For some reason `stream` was not in the debugger scope unless I reassigned it here
    debugger
    // Calling s.end() did nothing to stop the stream, earlier event listener was not fired
    // Calling s.destroy() did nothing to stop the stream
  }, 10000)

@brycekahle
Copy link
Contributor

Give abort a try.

@lourd
Copy link
Contributor

lourd commented Jan 19, 2016

Yeah, that did the trick. What the hell, how is that not documented in the Node Stream API?

@brycekahle
Copy link
Contributor

request doesn't return a stream but a Request object. It is a request method.

@lourd
Copy link
Contributor

lourd commented Jan 19, 2016

Ah right, thank you. Well, not documented in their API either 😕 Here's the open issue asking for it

@mfferreira
Copy link

Great. Thanks @brycekahle

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants