Printing out Spark events with JS

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"}

How can I log this event with JS?

Some of this might be useful:

This contains a fairly trivial example as well:

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.

Would you mind updating the code in your post with the full Spark/web code? That way it’ll be easier to see where the problem may lie. Thanks!

Thanks:

Here is my backend code:

int temp = 98;
int beans = 50;
long lastMillis = 0;

int brewCoffee(String command);
int cleanMachine(String command);

void setup() {
    Spark.function("brew", brewCoffee);
    Spark.function("clean", cleanMachine);
    Spark.variable("temp", &temp, INT);
    Spark.variable("beans", &beans, INT);

}

void loop() {
    if(millis()-lastMillis > 5000){
        if(beans <= 0){
            beans = 100;
        }
        else {
            beans--;
        }
        Spark.publish("time", "tick");
        lastMillis = millis();
    }


}

int brewCoffee(String command){
    if(command == "coffee")
    {
        return 1;
    }
    else return -1;
}

int cleanMachine(String command){
    return 1;
}

My fronted code:

 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')
    },

I don’t know why my EventListener for time is not triggering. especially because I am getting valid curl response.

Hi @lipenco

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.

@bko yes, my myAccesToken is hardcoded and

 var es = new EventSource('https://api.spark.io/v1/devices/events/spark?access_token=myAccesToken');

is creating post request with 200 response.
I can see my events when i curl in my terminal, but not in my console.

Also onopen event is consoling “open”