The SSE(subscribe a event in Browser) seems not support CORS?

Hi @Dave,

I known you are expert for spark local server. :slight_smile:

After lots of try and testing , i found that the Local server do not support sse with cors in browser ?

below is my code, which got nothing error, the connection will keep connected, but nothing got in browser , after debugging the server found : it will return ‘/n’ from server continued without error.

var evtSource = new EventSource("http://192.168.1.100:8080/v1/events/my-event?access_token=d4074326f6002dAAAAAAAAAAb98ad773d76ad2b1");

evtSource.onmessage = function(e) {
	  alert("EventSource failed.");

      $( "#container3" ).append( "<strong> YES, we now receive the event from serve side ! "  + "</strong> !" );

}
evtSource.onerror = function(e) {
	  alert("EventSource failed.");
};

Heya @yuanetking,

Hmm… There are a few questions here, SSE, CORS, and events in general, so assuming that the following works:

  • You can open that url directly, and see events in that browser window?

I think the latest Spark server package allows cross origin requests from everybody, code is here:

https://github.com/spark/spark-server/blob/master/js/main.js#L45-L59

So I’m guessing the issue is more about specifying the specific event name when binding your eventSource object. Checkout this post for an example of how to bind to a specific eventsource topic:

I hope that helps! :slight_smile:

Thanks,
David

1 Like

Hi @Dave ,

The code of emc2 are using the sse simulator in nodejs which is different with my way which i am calling the cloud server on the browser side directlly.

I just test the way from emc2 it works fine . and maybe the better choice for me . :smile:

Thx @Dave & @emc2

1 Like

Hi ,@Dave,

I found , below url will work:

http://192.168.1.100:8080/v1/events/my-event?access_token=***************************

But below url wont work, my event named “my-event” comes from Spark Core.

http://192.168.1.100:8080/v1/devices/events/my-event?access_token=***************************
http://192.168.1.100:8080/v1/devices/my-core-id/events/my-event?access_token=************************

2 Likes

Hi @yuanetking,

Ahh, thanks for clarifying! I think this makes sense, and might be related to some features I’d like to add to the local cloud the next chance I get. :slight_smile:

Thanks!
David

2 Likes