I was never able to get Spark library streaming to work in browser Javascript. I do have it working without the Spark function. Here’s the code I used. It calls my function processPublishNotificationGroup() when events arrive. With any amount of TTL in your messages the Spark Cloud might return several Publish messages from your core in any one notification. I have my spark core put a message number in each Publish so that my code can keep track of events it has already handled.
startMonitoring = function () {
logAdd("entered startMonitoring");
var eventMonitor = new XMLHttpRequest();
eventMonitor.onreadystatechange = function() {
var data = eventMonitor.responseText;
//console.log(data);
if (data.length > 5) {
processPublishNotificationGroup(data);
}
}
var accessToken = window.spark.accessToken;
eventMonitor.open("GET","https://api.spark.io/v1/devices/events/SISEvent?access_token=" + accessToken
,true);
eventMonitor.send();
},
I am new to the community (from France). Seems like I ran into the same kind of problem than what was discussed above: calling some functions from the Spark Javascript API inside a browser does not seem to work for some functions.
I manage to login on the Spark Cloud from a web page thanks to the
code snippet.
I can listDevices() but I cannot callfunction(). Javascript log console gives this: https://api.spark.io/v1/devices/MY ACCESS TOKEN/changeLeds 400 (Bad Request).
The core blinks red like it’s going mad (found out in the documentation that it’s an SOS message ) and then error message sent back is a time out : Error: Timed out. {stack: “Error: Timed out.↵ at Spark.normalizeErr (http:….jsdelivr.net/sparkjs/0.4.2/spark.min.js:1:29864)”, message: “Timed out.”}
Can the callFunction() API be used only on the server side?
Thanks.
Emmanuel
PS : I am really impressed with the products, the community… the creativity around all this. Congratulations to the Spark team and all the contributors.
Hi again,
I used the serial console on the core to check the callFunction() on the core side.
Looks like the call is performed ok. The issue comes from my handling the parameters on the function call on the core side.
So issue is on my side, not the JS Spark library.
Emmanuel