Publish. Listening to all messages for a device

I’ve got server side events listening perfectly for a single event like so

eventSource.addEventListener('button-pressed', function (e) {
            var parsedData = JSON.parse(e.data);
            //do stuff here
        }, false);

I can’t get it to work to listen to all events. From the SSE spec setting the onmessage function should do it.

eventSource.onmessage = function (e) {
            var parsedData = JSON.parse(e.data);
            //do stuff here
        };

This never gets called.

Is this a bug on the cloud side? My clientside? Am I missing something?

Hi @kareem613

I was not able to get the onmessage handler to fire on SSE’s either. After trying out a bunch of stuff, I think that this is because the event.type is ‘message’ for that handler and not any of our event types. When I run web-based examples of the onmessage handler, the event.type is always message, which leads me to think that this is browser thing and not an SSE thing.

I do not know how to build a listener for an entire stream, not just one named event type. I strongly suspect that this may be possible, but I have not figured it out either. A work-around would be to publish all your events under one type (such as ‘message’) and then use (say) a JSON as the data with your own private event type field in the data.

I don’t really see this as a big limitation, however. Since you know the names of every event that your core firmware is publishing, you could also just write a handler for each one.

Quick update–I just tried this:

Spark.publish("message",publishString);

And the onmessage handler does fire.

That experiment clarifies what’s happening. That’s for trying that out. I hadn’t thought of that.

Unfortunately I still don’t have a solution.
I’m building an event viewer for my spark tools service to help the community here.

The goal is to show a live stream of events given only a device id to ease development and troubleshooting. This means I actually don’t know the event names. Not my cores.

If you just do a get request from a browser (view source chrome) for deviceid/events I can see all events just fine.

This tells me the server is doing it’s job just fine and this is an event source feature limitation.

Use this end point:

https://api.spark.io/v1/devices/events/?access_token=xxxzzzz

To get events only from your cores

You can also only specify your core id and listen to all streams from that core itself.

Let me find the api call

https://api.spark.io/v1/devices/core_id/events/?access_token=xxxzzzz

That works perfectly with curl and doing raw GET requests.
I can’t seem to figure out how to do it with the javascript eventsource API.

Never worked with doing such stuff but I think of it with just a difference in api url that you will be calling to listen to the events.

Comments from this SO question.

Events sent with no event type define are equivalent to event type “message”.

So it looks like there’s no way to listen to all custom event types without explicitly knowing them.

Seems like a very odd limitation in the event source API.

Maybe there’s a way we can do to get all the events published by the core? Maybe some modification to the API server

Like how we check what functions/variables/status are available for the core

Hi @kareem613

I understand that your use-case is a bit atypical since you are trying to provide a service and don’t know all the events in advance.

If the SSE model doesn’t work for all events, can you fall back to AJAX & XMLHttpRequest style of web programming and make it work like curl?

Yes. Hand rolling a solution would be the only way. That’s a lot of reinvention though. I’d sooner just have users enter the event name then.

Unless the spark team can double up every event with a second that has no type defined. Sounds like a lot of resource overhead though.

Hmm. Perhaps a debug query string parameter?