I don’t know how to catch the event from EventSource with javascript. I am doing it like that, but “running” never appears in the console.
var es = new EventSource('https://api.spark.io/v1/devices/events/spark?access_token=myAccesToken');
es.addEventListener("time", function(e){
console.log("running")
}, false);
es.onopen = function(){
console.log('open')
},
es.onerror = function(e){
console.log('error')
},
It log on ‘open’ and I see in the network section in the browser console that it is connected and updating every 5 s,
when i curl to the server i am getting response every 5 s:
:ok
event: time
data: {"data":"tick","ttl":"60","published_at":"2014-08-24T16:31:04.091Z","coreid":"mycoreid"}
event: time
data: {"data":"tick","ttl":"60","published_at":"2014-08-24T16:31:09.096Z","coreid":"mycoreid"}
Hi Moors,
Thanks for help, I read the posts, but seems like they are listening to events exact same way as I do. I wonder why I my event ‘time’ never triggers the eventListener.
I am assuming where you have myAccesToken above you are replacing that with the actual hex number that is your access token, right? If myAccesToken is a Javascript variable, then you need to use string concat via the “+” operator.
Your event is being published, I can see it in the public event stream.