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.
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();
});
@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.