Interpreting EventStream via Javascript

Hi. So, I’m all set up with getting events from my Electron as an event stream, i.e. by entering this URL into my browser:

https://api.particle.io/v1/devices/events?access_token={access_token}

This works fine, and the page updates with events as they occur.

What I’m trying to do now is interpret those incoming events with JavaScript, and process and display them in some useful way on a webpage, e.g.

var source = new EventSource("https://api.particle.io/v1/devices/event?access_token={access_token}");
 
source.onmessage = function(event) {
    // Do something useful with event.data
};

But when I do this, no events ever seem to come through, even when triggering them manually. Some further reading seems to imply this might be a problem with the domains of the webpage and the event source not matching. (Cross-origin URLs)

Does anyone know of a way to get around this? Either through Particle configuration or JavaScript fiddling.

Alternatively, it would be sufficient for the project (albeit less preferable) to just query the Particle API for the last few events that have occurred. I’d be interested in how to do this as well.

I’m running on Chrome.

Thanks.

Why not use the JS SDK?

And there currently is no way to request past events.

2 Likes

Bingo. I didn’t even know that was there.

As always “Read the gosh-darned manual”.

Thanks.

1 Like