I'm developing the backend server for a turn based game using App Harbor and thus far it seems perfectly suited to my needs. I would really like to run a background process to process turn data etc and I was wondering if someone could clarify what the difference between 'Web workers' and 'Background workers' are? I have fairly limited experiance with web development, but as far as I can tell 'Web workers' are for interacting with AJAX pages (which I don't need) and 'Background workers' allow you to run a console application in the background (which I do need!)
Thanks!
What is a Service Worker? Unlike Web Workers, Service Workers have a dedicated role of being a proxy between the network and the browser and/or cache. Similar to Web Workers, they are registered in the primary JavaScript file as a dedicated worker.
Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface.
How many web workers can run concurrently JavaScript? A web worker is a JavaScript program running on a different thread, in parallel with main thread. The browser creates one thread per tab. The main thread can spawn an unlimited number of web workers, until the user's system resources are fully consumed.
Web Workers help in making use of multi-core processors efficiently, otherwise, JavaScript Application ends up running on the single main thread. If you put this code into the browser console in Developer Tools, you will see that the page becomes unresponsive which is due to the code blocking the main thread.
Web workers is connected with the request from browser and can get and send data to the request on browser.
Background Workers are independent threads that are not connected with any request and can not send data to the browser alone*.
[*] Only through a web worker.
In general 'Web worker' is the web application, or the ASP.NET site/service that handles requests from user. 'Background worker' is simply a scheduled task. It runs in background, on specified intervals, and executes some code.
The difference is that a Web worker does some work when a new request comes to the application. A request means someone loads a page/calls a web service on ASP.NET. While the background worker is being started on some interval, and doesn't need a user to load a page to be executed. It is mostly used for processing long-running tasks. The usual workflow is - the web worker receives a request from the user, and queues some data to be processed. On next run, the background worker gets the data and processes it, and stores the data somewhere(usually in database). Then the web worker, on next request from user, checks the database and if the result is there - shows it to the user.
This way the user doesn't need to wait for the asp.net page to process the data and return a result immediately.
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