With Barbaric Basics I brush up on basic techniques, practices and APIs. Come along and get them refreshed you too. Who knows? You may even learn something new!
Well I see the following pros to taking the certification:
- As a telecommunications engineer turned software developer I always feel like I am missing something, there some knowledge that all computer scientists hold that I never got the chance to study. As such, it is nice to have a curriculum that tells you: look! This is what you need to know to be able to say that you know technology X
- I started doing web development in the late age of jQuery and the early age of MV* frameworks. It was almost jumping from MVVM in XAML to MVVM with knockout.js. Because of that I never did get to program directly against the DOM and every time that I saw some explicit DOM manipulation I started to sweat XD. So it is great to finally know how everything works underneath and sweat no more.
- Last but not least Active Solution not only pays for the exam, but also gives me a bonus for every certification that I get. Freaking awesome right?
Let’s start with Web Workers. I will be using this sample program: iWrite, a vanilla markdown editor to showcase this API (also available on GitHub).
WebWorker from your website and communicate with it back and forth through messages.
To create a new web worker you use the
This will download the file asynchronously and ready the worker to be able to receive messages. The worker will work on its own thread, and in its very own isolated environment which you’ll be able to access through the
self keyword. Next, you will add an event handler to receive messages from the web worker:
Whenever we have some work that needs to be processed by the web worker we will send it a message via
1 2 3 4
When we send a message to the web worker it will start doing work for us, and when it is ready it will itself send a message back to the main executing thread:
1 2 3 4 5 6
That we can handle on the
onmessage event handler:
1 2 3 4 5
If you, for any reason, want to destroy your web worker you can do so using the
And the worker itself can do it by calling its own
You can also handle errors that occur in your web workers by using an
error event handler:
1 2 3 4 5 6 7 8 9 10
Cool right? :)
Some other nifty things that you can do with web workers are:
- Spawn other web workers
- Send ajax requests
- Access the
- Import external scripts via
Web Workers Gotchas
Some things that you need to consider while using web workers are:
- You have no access to the DOM (window, document) since you are not supposed to do DOM manipulation from a web worker
- Since there’s no limit in the amount of workers that you can create, you have to be careful and avoid hogging your host computer
- When sending messages between your web application and working threads you need to be aware that the message itself is copied. So think about how this will impact the performance and memory footprint of your app if you need to send huge messages. Also check this other alternative.
- If you terminate a worker or a worker closes itself, it is gone for ever, no more sending/handling messages.
Want to Know More About Web Workers?
If you want to learn some more about web workers check out these great in-depth articles: