Hi,
I using the nodejs sample code that listens to all the events coming from my devices(tried both get-event-stream-forever.js and get-event-stream.js). The code seems to work fine for about 30 mins to an hour(duration varies every try, but seems to work for 30 mins without issue everytime), and then suddenly stops receiving events. Any help in this matter will be very much appreciated.
The get-event-stream-forever.js works fine for me. It only fails if my internet drops, otherwise, it’ll re-establish a connection. Is there an error message when it fails? That should make it easier to debug.
Dear @Moors7
I’m using the same code (get-event-stream-forever). I’d like to ask you if there is any way that I can re-establish a connection if my internet drops?
Thanks in advance.
Ahmed
Here is the code:
/*jslint node: true */
"use strict";
// Setup the modules we need to use
var spark = require('spark');
var fs = require('fs');
var d = 0;
var nd = 0;
var utc = 0;
// Login and start doing things!
var openStream = function() {
//Get the locale time for Columbia instead of UTC/GMT
function calcTime(city, offset) {
d = new Date();
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
nd = new Date(utc + (3600000 * offset));
return " Published at " + nd.toLocaleString();
}
//Get test event for specific core
var req = spark.getEventStream('Spark1', '000000000000000000', function(data) {
console.log("New Event: " + JSON.stringify(data) + ". Logging it to file");
fs.appendFile('Spark.txt', data.data + "," +
calcTime('Columbia', '-5.0') + "," + data.name + "\r\n", function(err) {
if (err)
console.log("File log failure. Error = " + JSON.stringify(err));
else
console.log("File log success");
});
});
req.on('end', function() {
console.log("ended! re-opening in 3 seconds...");
setTimeout(openStream, 3 * 1000);
});
};
spark.on('login', function() {
openStream();
});
// Login as usual
spark.login({ username: 'myemail@yahoo.com', password: 'mypassword'});
Hmm, I haven’t tested this locally, but you could watch for more events on the api request object, error might be good, I’m surprised ‘end’ isn’t being called, but you could try something like:
req.on('error', function() { /* reconnect stuff here */ });
Otherwise you could make an assumption like, "hey I should be getting at least 1 event every five minutes, so reconnect if that’s not true, etc.
for example:
//Get test event for specific core
var req = spark.getEventStream('Spark1', '000000000000000000', function(data) {
// record the time
lastEventTime = new Date();
...
//snip!
...
setInterval(function() {
var elapsed = (new Date()) - lastEventTime;
if (elapsed > (5*60*1000)) {
//it's been more than 5 minutes!
console.log("ended! re-opening in 3 seconds...");
setTimeout(openStream, 3 * 1000);
}
}, 30*1000);
sets the delay for an interval callback. That callback checks the heartbeat clock to see if it's been more than 5 minutes. So that means, every 30 seconds we check to see if it's been longer than 5 minutes.
Yes that's the right way to declare lastEventTime, just make sure it's scoped appropriately so the code can find it.