SlideShare a Scribd company logo
Web Workers
Yakusik Maksim
What is this?
A technology that allows to execute the script in a different context, which does not
block the UI.
What types of workers are exist?
Web Workers
Shared Web Workers
Do you know about Service Workers?
That’s a bit different. Service Workers works in a different context, just like the
worker has the same limitations (without access to the DOM). However, this
provides many different api to control web pages.
https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API
https://developer.mozilla.org/ru/docs/Web/API/Service_Worker_API
How to design a Worker?
var scriptUrl = "/worker.js";
var worker = new Worker(scriptUrl);
var sharedWorker = new SharedWorker(scriptUrl);
// также можно без отдельного скрипта
var blob = new Blob([code], {type:
'application/javascript'});
worker = new Worker(URL.createObjectURL(blob));
How to send / receive messages to / from Worker
worker.postMessage({hello: 'world'});
worker.onmessage = m => m.data... ;
sharedWorker.port.postMessage({hello: 'world'});
sharedWorker.port.onmessage = m => m.data... ;
How to get a message inside Worker
// in Worker
self.onmessage = function (message) {
message.data ...
};
// in SharedWorker
self.onconnect = function(connect) {
var port = connect.source;
port.onmessage = function(message) {
message.data ...
};
};
https://github.com/MaximYakusik/worker-example/tree/feature/send-hello
How to initiate the integration of Workers
worker.postMessage({ port: sharedWorker.port },
[sharedWorker.port]);
How to kill worker :)
/*Call in the main thread*/
worker.terminate()
Other options:
- Via the browser api. (in chrome: // inspect)
- Close all pages that use it.
- Ask Shared Worker to close himself.
// on worker
self.onconnect = function(connect) {
var port = connect.source;
port.onmessage = function(message) {
switch (message.data.command) {
...
case 'Kill yourself':
port.postMessage('I'm die :(');
self.close();
break;
}
};
};
Closing Shared Worker
//on main
sharedWorker.port
.postMessage({command: 'Kill
yourself'});
https://github.com/MaximYakusik/worker-example/tree/feature/worker-must-die
What could I use it for?
For resource-intensive tasks such as packing and unpacking of archives.
https://gildas-lormeau.github.io/zip.js/
In which browsers can I use it?
https://caniuse.com/#feat=webworkers,sharedworkers
Thank you for attention!

More Related Content

PDF
Building Isomorphic Apps (JSConf.Asia 2014)
PDF
General Assembly Workshop: Advanced JavaScript
PPT
HTML5 Multithreading
PDF
Node, express & sails
PDF
Lesson 09
PDF
Integrating Browserify with Sprockets
PPTX
Node.js with WebMatrix
PPT
You Know WebOS
Building Isomorphic Apps (JSConf.Asia 2014)
General Assembly Workshop: Advanced JavaScript
HTML5 Multithreading
Node, express & sails
Lesson 09
Integrating Browserify with Sprockets
Node.js with WebMatrix
You Know WebOS

What's hot (20)

PPTX
Angular Lazy Loading and Resolve (Route Resolver)
PPTX
HTML5 Web Workers-unleashed
PDF
Grunt.js and Yeoman, Continous Integration
KEY
An Introduction to webOS
PPTX
Workshop Intro: FrontEnd General Overview
PDF
Deep dive into Vue.js
PPTX
ASP.NET MVC and ajax
PPTX
Ajax assignment help
PDF
Phantom js quick start
PDF
Intro to Sails.js
PPTX
A New Vue for Web Development
PPTX
AngularJS - The Next Big Thing?
PDF
React.js and Flux in details
ODP
AngularJS
PPTX
Vue 2.0 + Vuex Router & Vuex at Vue.js
PPTX
Modern Applications With Asp.net Core 5 and Vue JS 3
PDF
Vue JS Intro
PDF
Browserify
PPT
JavaScript on Rails 튜토리얼
Angular Lazy Loading and Resolve (Route Resolver)
HTML5 Web Workers-unleashed
Grunt.js and Yeoman, Continous Integration
An Introduction to webOS
Workshop Intro: FrontEnd General Overview
Deep dive into Vue.js
ASP.NET MVC and ajax
Ajax assignment help
Phantom js quick start
Intro to Sails.js
A New Vue for Web Development
AngularJS - The Next Big Thing?
React.js and Flux in details
AngularJS
Vue 2.0 + Vuex Router & Vuex at Vue.js
Modern Applications With Asp.net Core 5 and Vue JS 3
Vue JS Intro
Browserify
JavaScript on Rails 튜토리얼
Ad

Similar to Web Workers (20)

PDF
Web workers
PDF
Web workers
PDF
The Happy Path: Migration Strategies for Node.js
PPT
(In)Security Implication in the JS Universe
PPTX
Mean stack Magics
PPTX
Java script Basic
PPTX
Html web workers
PDF
Service Worker 201 (en)
PDF
Javascript - Ebook (A Quick Guide)
PPTX
VisualWeb - Building a NodeJS Server Meshwork and Full-Javascript Stack Frame...
PPTX
Ajax presentation
PPTX
Front End Development | Introduction
PDF
Introduction to Node.js
PDF
Beginning MEAN Stack
PDF
Wso2 product release webinar introducing jaggery
PDF
Intro to node.js - Ran Mizrahi (27/8/2014)
PDF
Intro to node.js - Ran Mizrahi (28/8/14)
PDF
Full Stack React Workshop [CSSC x GDSC]
PPT
PDF
NodeJS @ ACS
Web workers
Web workers
The Happy Path: Migration Strategies for Node.js
(In)Security Implication in the JS Universe
Mean stack Magics
Java script Basic
Html web workers
Service Worker 201 (en)
Javascript - Ebook (A Quick Guide)
VisualWeb - Building a NodeJS Server Meshwork and Full-Javascript Stack Frame...
Ajax presentation
Front End Development | Introduction
Introduction to Node.js
Beginning MEAN Stack
Wso2 product release webinar introducing jaggery
Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (28/8/14)
Full Stack React Workshop [CSSC x GDSC]
NodeJS @ ACS
Ad

Recently uploaded (20)

PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Nekopoi APK 2025 free lastest update
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
assetexplorer- product-overview - presentation
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Digital Systems & Binary Numbers (comprehensive )
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Computer Software and OS of computer science of grade 11.pptx
Nekopoi APK 2025 free lastest update
Designing Intelligence for the Shop Floor.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PTS Company Brochure 2025 (1).pdf.......
assetexplorer- product-overview - presentation
Why Generative AI is the Future of Content, Code & Creativity?
wealthsignaloriginal-com-DS-text-... (1).pdf
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
CHAPTER 2 - PM Management and IT Context
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx

Web Workers

  • 2. What is this? A technology that allows to execute the script in a different context, which does not block the UI. What types of workers are exist? Web Workers Shared Web Workers
  • 3. Do you know about Service Workers? That’s a bit different. Service Workers works in a different context, just like the worker has the same limitations (without access to the DOM). However, this provides many different api to control web pages. https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API https://developer.mozilla.org/ru/docs/Web/API/Service_Worker_API
  • 4. How to design a Worker? var scriptUrl = "/worker.js"; var worker = new Worker(scriptUrl); var sharedWorker = new SharedWorker(scriptUrl); // также можно без отдельного скрипта var blob = new Blob([code], {type: 'application/javascript'}); worker = new Worker(URL.createObjectURL(blob));
  • 5. How to send / receive messages to / from Worker worker.postMessage({hello: 'world'}); worker.onmessage = m => m.data... ; sharedWorker.port.postMessage({hello: 'world'}); sharedWorker.port.onmessage = m => m.data... ;
  • 6. How to get a message inside Worker // in Worker self.onmessage = function (message) { message.data ... }; // in SharedWorker self.onconnect = function(connect) { var port = connect.source; port.onmessage = function(message) { message.data ... }; }; https://github.com/MaximYakusik/worker-example/tree/feature/send-hello
  • 7. How to initiate the integration of Workers worker.postMessage({ port: sharedWorker.port }, [sharedWorker.port]);
  • 8. How to kill worker :) /*Call in the main thread*/ worker.terminate() Other options: - Via the browser api. (in chrome: // inspect) - Close all pages that use it. - Ask Shared Worker to close himself.
  • 9. // on worker self.onconnect = function(connect) { var port = connect.source; port.onmessage = function(message) { switch (message.data.command) { ... case 'Kill yourself': port.postMessage('I'm die :('); self.close(); break; } }; }; Closing Shared Worker //on main sharedWorker.port .postMessage({command: 'Kill yourself'}); https://github.com/MaximYakusik/worker-example/tree/feature/worker-must-die
  • 10. What could I use it for? For resource-intensive tasks such as packing and unpacking of archives. https://gildas-lormeau.github.io/zip.js/
  • 11. In which browsers can I use it? https://caniuse.com/#feat=webworkers,sharedworkers
  • 12. Thank you for attention!