Lost event stream [solved]

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