Lost event stream [solved]

Hi,

When subscribing to events using a simple server in Node, the eventstream is lost after some time (usually a few minutes) and the server needs to be restarted. Is there a way to check if the stream is lost somehow? If there is, it would be possible to subscribe again without restarting the process.

Some sample code:

var Spark = require('spark');

var subscribe = function(){
  Spark.getEventStream(false, 'mine', function(data) {
    console.log(data);
  });
}

Spark.login({accessToken: 'token'}, function(err, body) {
  subscribe();
});
1 Like

I just tried subscribing with the command line interface and found that the process dies in the same way. Any ideas how to keep it alive?

@Dave, could you take a look at this, I’ve been experiencing similar issues?

Hey All,

While it’s not unexpected that a SSE stream will end at sometime, they do happen a little more frequently than I’d like. Here’s some example code you can write with SparkJS to automatically resume a stream if it stops:

//example code to keep SSE stream alive

 var openStream = function() {

    //Get your event stream
    var req = spark.getEventStream(false, 'mine', function(data) {
      console.log("Event: " + JSON.stringify(data));
    });

    req.on('end', function() {
        console.log("ended!  re-opening in 3 seconds...");
        setTimeout(openStream, 3 * 1000);
    });
};

spark.on('login', function() {
    openStream();
});

Thanks!
David

6 Likes

Awesome, thanks a lot :)! I was thinking it has to do with pooling, since that’s what I found on the Web, but this should work just fine.

1 Like

Yes thanks, just what I was looking for!

1 Like

@Dave, my question is somewhat related. I would like to know how to end the event stream. I have a web page that listen to a specific device and its events but I would like to end the stream from coming in to my server when I leave the page. I am using the code below and events come in as they should.

particle.getEventStream({ deviceId: Device_Id, auth: UserAccess_Token }).then(function(stream) {
stream.on(‘event’, function(data) {
//console.log("P Device Event: " + data);
cb(null,data);

});

Hi @mtun009,

Good question! I believe you should be able to simply call:

stream.abort();

That syntax feels a bit confusing to me, but that’s what the code seems to indicate. ( https://github.com/spark/particle-api-js/blob/master/src/EventStream.js )

I hope that helps!

Thanks,
David

@Dave, thanks.

1 Like

@Dave,

I tried stream.abort() to abort event stream as follows. None works.

particle.getEventStream({ deviceId: Device_Id, auth: UserAccess_Token }).then(function(stream) {
    stream.abort();
   
  }); 

or

particle.getEventStream({ deviceId: Device_Id, auth: UserAccess_Token }).then(function(stream) {
   
    stream.on('abort', function(data) {
      console.log("P Abort Device Event: " + data);
      cb(null,data);

    });
    
  });

None of the above option works. Please let me know what I am missing.

Thanks.