Web Workers concepts and usage
Worker()
) that runs a named JavaScript file — this file contains the code that will run in the worker thread; workers run in another global context that is different from the current window
. DedicatedWorkerGlobalScope
object (in the case of dedicated workers - workers that are utilized by a single script), or a SharedWorkerGlobalScope
(in the case of shared workers - workers that are shared between multiple scripts).window
object. window
, including WebSockets, and data storage mechanisms like IndexedDB. See Functions and classes available to workers for more details.postMessage()
method, and respond to messages via the onmessage
event handler (the message is contained within the Message
event's data property). The data
is copied rather than shared.hosted within the same origin as the parent page. In addition, workers
may use
XMLHttpRequest
for network I/O, with the exception that the responseXML
and channel
attributes on XMLHttpRequest
always return null
.- Shared workers are workers that can be utilized by multiple scripts running in different windows, IFrames, etc., as long as they are in the same domain as the worker. They are a little more complex than dedicated workers — scripts must communicate via an active port. See
for more details.SharedWorker
- ServiceWorkers essentially act as proxy servers that sit between web applications, the browser, and the network (when available). They are intended, among other things, to enable the creation of effective offline experiences, intercept network requests and take appropriate action based on whether the network is available, and update assets residing on the server. They will also allow access to push notifications and background sync APIs.
- Chrome Workers are a Firefox-only type of worker that you can use if you are developing add-ons and want to use workers in extensions and have access to js-ctypes in your worker. See
for more details.ChromeWorker
- Audio Workers provide the ability for direct scripted audio processing to be done inside a web worker context.
Note: As per the Web workers Spec, worker error events should not bubble (see bug 1188141. This has been implemented in Firefox 42.
Web Worker interfaces
Worker
or SharedWorker
).Worker
.Window
does for normal web content). Different types of worker have scope objects that inherit from this interface and add more specific features.WorkerGlobalScope
and adding some dedicated features.WorkerGlobalScope
and adding some dedicated features.Examples
- Basic dedicated worker example (run dedicated worker).
- Basic shared worker example (run shared worker).
Specifications
See also
- Using Web Workers
- Worker Interface
- SharedWorker interface
- ServiceWorker API
- Functions and classes available to workers
- Advanced concepts and examples
- ChromeWorker: for using workers in privileged/chrome code
Credits
- Source: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
- Published under Open CC Attribution ShareAlike 3.0 licence