I am attempting to use an EventSource
(Server Sent Events) within a Shared Web Worker in Firefox. When I attempt to try and use the EventSource constructor in the message event in the Shared Worker, the Worker throws an error ReferenceError: EventSource is not defined.
I assume this means this object can't be accessed in the Shared Web Workers context but I am able to access the EventSource from within a Shared Web Worker in Google Chrome. I want to make it clear that I can use EventSource
without issue if used directly from script, I just have issues with creating and using it inside of a Shared Web Worker within Firefox.
Looking at the EventSource spec in Section 4 it states:
This constructor must be visible when the script's global object is either a Window object or an object implementing the WorkerUtils interface.
But looking at the Web Workers spec in Section 5 the interface specified there is a WorkerUtils interface.
interface WorkerGlobalScope : EventTarget {
readonly attribute WorkerGlobalScope self;
readonly attribute WorkerLocation location;
void close();
[TreatNonCallableAsNull] attribute Function? onerror;
[TreatNonCallableAsNull] attribute Function? onoffline;
[TreatNonCallableAsNull] attribute Function? ononline;
};
WorkerGlobalScope implements WorkerUtils;
I would have assumed I could access the EventSource using self.EventSource
or just EventSource
from within the Worker based on the information mentioned in both specs. However this doesn't seem to be the case in practice with Firefox.
Is there a reason why I cannot access an EventSource from a Shared Web Worker from Firefox?
I want to use a Shared Web Worker for EventSource (Push Events) because a user could have multiple tabs open from the website in question and I want to avoid having the browser open up multiple EventSource connections to the host.
Is there another approach I should be using instead? Unfortunately Web Sockets isn't an option as I am developing on ASP.net 2.
Thanks for your time.
This bug has being fixed in Firefox Nightly and shipped in Firefox 53:
I've exposed EventSource to dedicated worker and shared worker.
Do other browser engines implement this? As I know, Chrome supports it in dedicated worker, shared worker, and service worker. Safari supports it in dedicated worker. Edge doesn't support it.
Expose EventSource to ServiceWorker: No plan for Service Worker at this moment because there is an open issue https://github.com/w3c/ServiceWorker/issues/947 under discussion.
References
Mozilla Bug 1267903 – EventSource for workers
Firefox Releases: 53 - Workers and Service workers
Future Releases | Be the first to know what's new with upcoming Firefox releases.
Intent to ship: EventSource for Dedicated Worker and Shared Worker - Google Groups
web-platform-tests/eventsource at master · w3c/web-platform-tests
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With