2

I've studied Web and Service Workers and I know that they are intended for different approaches. This thread describes them in more detail. However, what I don't understand is the technical difference between these two. While a Service Worker is meant to be a proxy between a server and a client-side application, a Web Worker can be that too. It has access to XMLHttpRequest so you can use it as proxy too.

What is the technical difference between a Web Worker and a Service Worker?

Community
  • 1
  • 1
user3292653
  • 602
  • 1
  • 7
  • 25
  • correct answer is the unaccepted one by Ali to this question: https://stackoverflow.com/questions/38632723/what-can-service-workers-do-that-web-workers-cannot – user2297550 Nov 21 '18 at 08:53

1 Answers1

5

The key difference between the two is that a Service Worker is intended to intercept network requests that would typically be sent directly to a remote service and handle the event such that the front end client code can continue working even when the network is unavailable. Which is to say provide the basis of an offline mode for a web app. The front end code makes standard fetch() requests as if it were talking to the server that are intercepted by the service worker.

A Web Worker is just a general purpose background thread. The intention here is to run background code such that long running tasks do not block the main event loop and cause a slow UI. Web Workers do not intercept network requests, rather the front end code explicitly sends messages to the Web Worker.

bhspencer
  • 13,086
  • 5
  • 35
  • 44
  • So, a web worker must be called explicit while a service worker is called by every fetch in the client side application? – user3292653 May 14 '17 at 08:31
  • No exactly. To be clear you cannot call a method on a web worker or a service worker directly. You can only send them messages. Both web workers and service workers can receive messages with postMessage(). Where as service workers can intercept network calls and web workers cannot. – bhspencer May 14 '17 at 13:06
  • better, correct answer is the unaccepted one by Ali here: https://stackoverflow.com/questions/38632723/what-can-service-workers-do-that-web-workers-cannot – user2297550 Nov 21 '18 at 08:53