<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arun Murugan</title>
    <description>The latest articles on DEV Community by Arun Murugan (@arunmurugan78).</description>
    <link>https://dev.to/arunmurugan78</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F467499%2Ffa41f5ef-6ac6-4adc-beb0-8ec2ff308de4.gif</url>
      <title>DEV Community: Arun Murugan</title>
      <link>https://dev.to/arunmurugan78</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arunmurugan78"/>
    <language>en</language>
    <item>
      <title>Introduction to Web Workers aka Multi-Threading in browser</title>
      <dc:creator>Arun Murugan</dc:creator>
      <pubDate>Mon, 11 Jan 2021 12:51:31 +0000</pubDate>
      <link>https://dev.to/arunmurugan78/introduction-to-web-worker-aka-multi-threading-in-browser-482a</link>
      <guid>https://dev.to/arunmurugan78/introduction-to-web-worker-aka-multi-threading-in-browser-482a</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Web Workers are scripts which run in a separate thread in the background.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;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. (from MDN docs)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Javascript is a single threaded programming language, which means it runs instructions step by step &lt;strong&gt;sequentially&lt;/strong&gt;.&lt;br&gt;
If the code is executing some expensive task which takes a lot of time. Then, no other javascript code can execute until the control comes out of that step. This is because the current thread is busy executing that step and javascript is a &lt;strong&gt;single threaded &lt;/strong&gt; language.&lt;br&gt;
While that step is being executed, no javascript can execute making the UI un-responsive for task requiring javascript, which is obviously a bad user experience.&lt;/p&gt;

&lt;p&gt;We wouldn't have faced this problem if javacsript was multi-threaded. It's not though. What if we can create threads using the web browser? ....Web Workers were born.&lt;/p&gt;

&lt;p&gt;Each Web Worker runs in a separate &lt;strong&gt;OS thread &lt;/strong&gt;and &lt;strong&gt;execution context&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Lets Start Coding!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Check if the Web Workers are supported by the browser&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Create a new Web Worker&lt;/span&gt;
 &lt;span class="c1"&gt;//Pass the URI of the script which has to run as a web worker to the Worker Constructor&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myWorker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;worker.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the worker script (worker.js in this case) can be as simple as a console.log&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Running in a separate thread&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main and the worker threads interact through &lt;strong&gt;Messages&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;self&lt;/strong&gt; in the web worker refers to the Worker Execution Context instance. We'll come back to self later again.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sending Messages
&lt;/h3&gt;

&lt;p&gt;We send message from main thread to worker thread through &lt;strong&gt;postMessage&lt;/strong&gt; method of the worker.&lt;br&gt;
Message from the worker script can be sent to main thread by simply calling postMessage(message) or self.postMessage(message)&lt;/p&gt;
&lt;h3&gt;
  
  
  Listening to Messages
&lt;/h3&gt;

&lt;p&gt;The onmessage method of the worker instance fires when that worker has sent a message to the main thread.&lt;br&gt;
The self.onmessage or onmessage function in the worker script fires when the main thread has sent a message.&lt;br&gt;
The onmessage event listener function accepts a instance of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent"&gt;MessageEvent&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;app.js&lt;/small&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myWorker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;worker.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[Inside Main thread] Sending Message to Worker thread&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;myWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello Web Worker&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;myWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[Inside Main thread] Message sent by Web Worker - &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;small&gt;worker.js&lt;/small&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//onmessage is implicitly self.onmessage&lt;/span&gt;
&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[Inside WebWorker] Message sent by Main thread - &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[Inside WebWorker] Sending Message to Main Thread&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello Main Thread&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;small&gt;Output ( Console logs )&lt;/small&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Inside Main thread] Sending Message to Worker thread
[Inside WebWorker] Message sent by Main thread - Hello Web Worker
[Inside WebWorker] Sending Message to Main Thread
[Inside Main thread] Message sent by Web Worker - Hello Main Thread
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can't access all Browser API inside the web worker script and that's for good. You have access to browser Api like IndexedDB, Web Sockets, inside the web worker. You can't directly manipulate or access DOM. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers"&gt;Complete list of Browser api supported inside web worker&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Web Workers
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Dedicated Web Worker (What we saw earlier)&lt;/li&gt;
&lt;li&gt;Shared Web Worker &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Dedicated Web Worker
&lt;/h3&gt;

&lt;p&gt;The web worker instance can be accessed only by the script created it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Shared Web Worker
&lt;/h3&gt;

&lt;p&gt;The web worker instance can be accessed by any script.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Readings
&lt;/h2&gt;

&lt;p&gt;Learn more about Web Workers from &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers"&gt;MDN docs&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdevelopement</category>
      <category>browser</category>
      <category>web</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
