It was meant to be more of a proof of concept to show that it was actually working. If you check the console, youāll see that it returns your accesstoken, which you can then use to get your functions/variables and fire them.
Once youāve loaded the script, you should be able to use the functions mentioned in the docs. The following things work for me, which lead me to believe the rest should work as well.
Thanks for that, I was able to get that working fine, however spark.getDevice doesnāt appear to be working? I had a look in the spark.js file and couldnāt find any reference of that function?
[Log] API call completed on Login event: (test.html, line 14)
Object
[Error] TypeError: undefined is not a function (evaluating 'spark.getDevice('54ff6e066678574918123456', function(err, device) {
console.log('Device name: ' + device.name);
})')
(anonymous function) (test.html, line 27)
emit (spark.min.js, line 2)
emitAndCallback (spark.min.js, line 1)
(anonymous function) (spark.min.js, line 1)
(anonymous function) ([native code], line 0)
login (spark.min.js, line 1)
global code (test.html, line 34)
[Log] Device name: tasty_station (test.html, line 19)
[Log] - connected?: false (test.html, line 20)
[Log] - variables: undefined (test.html, line 21)
[Log] - functions: undefined (test.html, line 22)
[Log] - version: undefined (test.html, line 23)
[Log] - requires upgrade?: undefined (test.html, line 24)
Ok so I worked it out, appears the documentation is incomplete and still references the older 0.2.0 release rather than the 0.2.4 release which is when the getDevice function was added
Version 0.1.0 Initial release Version 0.2.1 Publishing, test release Version 0.2.2 Pulling in getDevice Version 0.2.4 adding some basic parsing to the getEventStream function, now returns an object
How do we go about submitting a bug report on the documentation?
Youāre absolutely correct! I hadnāt noticed that myself since I have the library locally. You can make Pull Request by clicking the āedit this pageā button at the top right of the docs page.
Seeing as I apparently have editorial rights on the docs, I went ahead and changed it. I guess the docs get updated every X time, so it should probably show up in a while. (Could you verify/check this@kennethlimcp?)
Is it possible to also get your second example included in the documentation as that example instantly provided all the components I needed to get my code working
Glad I could be of assistance !
Technically I could paste it in there, but I guess itāll have to be cleaned up a fair bit, and have some peer review with the other Elites before I try that. Donāt want to mess up the docs, but I agree that some more examples might not be out of place. My example wouldāve also helped me when I just started out (which is not too long ago ;P).
You can of course always make Pull Requests if you feel youāve got something meaningful to add. Iāll take a look at this as well
[Log] API call completed on Login event: (test.html, line 9)
Object
[Error] Error: Bad callback given: undefined
request (spark.min.js, line 1)
getEventStream (spark.min.js, line 1)
getEventStream (spark.min.js, line 1)
(anonymous function) (test.html, line 10)
emit (spark.min.js, line 2)
emitAndCallback (spark.min.js, line 1)
(anonymous function) (spark.min.js, line 1)
(anonymous function) ([native code], line 0)
login (spark.min.js, line 1)
global code (test.html, line 15)
That indeed is strange. That Iāve looked at the library but canāt figure out why itās giving the error. I guess itās time to call in the reinforcements: @kennethlimcp@peekay123@Dave@ScruffR@bko Could one of you perhaps take a look at it? The code above is basically the same as in the examples provided.
I did troubleshoot before I posted but forgot to include the details, it seems all the Spark event functions have the same issue as they all link to a common function that seems to be failing?
Can anyone confirm they are having the same issues when they put the code into a HTML page? I tried to keep the snippet as simple as possible to allow it to be easily debugged
This should be a ācompleteā example, which doesnāt work:
<html>
<head>
<title>Spark Login Example</title>
</head>
<body>
Open the console to see output.
</body>
<script src="http://cdn.jsdelivr.net/sparkjs/0.2.4/spark.min.js" type="text/javascript" charset="utf-8"></script>
<script>
spark.on('login', function() {
console.log("I've just logged in. Now I'll try to subscribe to all events.");
//Get all events
spark.getEventStream(false, false, function(data) {
console.log("Event: " + JSON.stringify(data));
});
/*
//Get your devices events
spark.getEventStream(false, 'mine', function(data) {
console.log("Event: " + JSON.stringify(data));
});
//Get test event for specific core
spark.getEventStream('test', 'CORE_ID', function(data) {
console.log("Event: " + JSON.stringify(data));
});
*/
});
// Login as usual
spark.login({ username: 'mail@provider.com', password: 'pass'});
//spark.login({ accessToken: '012345' });
</script>
</html>
Several other functions work fine, although the listDevices gave some problems since it doesnāt return the functions/variables like itās supposed to. getDevice on the other hand does work.
I actually tried the latest spark.min.js from the git and it still has the same issue, I wonder if there is a bug that only shows up when using it outside of NodeJS?
Hey @dave, did you have a chance to look at this already? The eventStream is still acting weird. I tried to use the code you supplied yesterday in the browser, but I keep getting errors about the callback functions.
Using this (from the docs):
Potentially unhandled rejection [1] Error: Bad callback given: undefined
at request (http://cdn.jsdelivr.net/sparkjs/0.2.4/spark.min.js:1:23577)
at SparkApi.getEventStream (http://cdn.jsdelivr.net/sparkjs/0.2.4/spark.min.js:1:7112)
at Spark.getEventStream (http://cdn.jsdelivr.net/sparkjs/0.2.4/spark.min.js:1:21683)
at file:///C:/Users/jordy/Documents/sparkjs/events.html:17:10
at http://cdn.jsdelivr.net/sparkjs/0.2.4/spark.min.js:1:12041
at tryCatchReject (http://cdn.jsdelivr.net/sparkjs/0.2.4/spark.min.js:7:13954)
at runContinuation1 (http://cdn.jsdelivr.net/sparkjs/0.2.4/spark.min.js:7:13469)
at Fulfilled.when (http://cdn.jsdelivr.net/sparkjs/0.2.4/spark.min.js:7:10707)
at Pending.run (http://cdn.jsdelivr.net/sparkjs/0.2.4/spark.min.js:7:9155)
at runQueue (http://cdn.jsdelivr.net/sparkjs/0.2.4/spark.min.js:6:24063)
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();
},