<?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: Subham Dash</title>
    <description>The latest articles on DEV Community by Subham Dash (@subhamdash45).</description>
    <link>https://dev.to/subhamdash45</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%2F977643%2F41bcc679-f0c1-49c9-8e41-3b7acaf8716f.jpg</url>
      <title>DEV Community: Subham Dash</title>
      <link>https://dev.to/subhamdash45</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/subhamdash45"/>
    <language>en</language>
    <item>
      <title>Event Flow: The Magic Behind Bubbling and Capturing! 🌟</title>
      <dc:creator>Subham Dash</dc:creator>
      <pubDate>Sun, 17 Mar 2024 14:44:34 +0000</pubDate>
      <link>https://dev.to/subhamdash45/event-flow-the-magic-behind-bubbling-and-capturing-3b2f</link>
      <guid>https://dev.to/subhamdash45/event-flow-the-magic-behind-bubbling-and-capturing-3b2f</guid>
      <description>&lt;p&gt;Ever wondered how your browser processes those clicks, scrolls, and hovers you make on a webpage? Let's uncover the magic of event flow, where the journey of an event begins!&lt;/p&gt;

&lt;h3&gt;
  
  
  🔍 Event Flow Basics:
&lt;/h3&gt;

&lt;p&gt;Events in the DOM (Document Object Model) follow a fascinating journey through various elements. This journey involves two crucial phases: capturing and bubbling.&lt;/p&gt;

&lt;h3&gt;
  
  
  💫 Capturing Phase:
&lt;/h3&gt;

&lt;p&gt;During the capturing phase, the browser starts from the outermost ancestor element of the target element and traverses down to the target element itself. It's like zooming in from the global scope to the specific element that triggered the event.&lt;/p&gt;

&lt;h3&gt;
  
  
  🚀 Bubbling Phase:
&lt;/h3&gt;

&lt;p&gt;Conversely, the bubbling phase starts at the target element and then bubbles up through its ancestor elements until it reaches the topmost element in the DOM hierarchy. This phase mirrors the event's propagation back to the document root.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;By default, all events you add with addEventListener are in the bubble phase.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3rsydtgpg565w0yifeb1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3rsydtgpg565w0yifeb1.png" alt="event flow image" width="663" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's an example of how it works, which I'll explain below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8" /&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&amp;gt;
    &amp;lt;link rel="stylesheet" href="style.css" /&amp;gt;
    &amp;lt;title&amp;gt;Practice&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Bubbling and Capturing phase&amp;lt;/h1&amp;gt;

    &amp;lt;div&amp;gt;
      &amp;lt;button class="child"&amp;gt;click me&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;

    &amp;lt;script&amp;gt;
      const parent = document.querySelector("div");
      const child = document.querySelector(".child");

      parent.addEventListener("click", function () {
        console.log("clicked parent");
      });

      child.addEventListener("click", function () {
        console.log("clicked child");
      });
    &amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output in the console:
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;clicked child&lt;br&gt;
clicked parent&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  To reverse the order(Capture events first)
&lt;/h3&gt;

&lt;p&gt;The click event of its parent elements must be triggered before the click of the nested element. This phase trickles down from the top of the DOM tree to the target element.  &lt;/p&gt;

&lt;p&gt;Whenever the third argument of addEventListener is set to true, event handlers are automatically triggered in the capturing phase.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8" /&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&amp;gt;
    &amp;lt;link rel="stylesheet" href="style.css" /&amp;gt;
    &amp;lt;title&amp;gt;Practice&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Bubbling and Capturing phase&amp;lt;/h1&amp;gt;

    &amp;lt;div&amp;gt;
      &amp;lt;button class="child"&amp;gt;click me&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;

    &amp;lt;script&amp;gt;
      const parent = document.querySelector("div");
      const child = document.querySelector(".child");

      parent.addEventListener(
        "click",
        function () {
          console.log("clicked parent");
        },
        true
      );

      child.addEventListener("click", function () {
        console.log("clicked child");
      });
    &amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output in the console:
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;clicked parent&lt;br&gt;
clicked child&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  stopPropagation
&lt;/h3&gt;

&lt;p&gt;As its name suggests, the Event.stopPropagation method stops propagation. Any other listener for the same type of event on some ancestor element will not trigger their event listener for the event.&lt;/p&gt;

&lt;h3&gt;
  
  
  👉 Event Bubbling vs. Event Capturing:
&lt;/h3&gt;

&lt;p&gt;While both phases play a role in event propagation, developers often utilize event bubbling due to its intuitive nature. However, event capturing can be handy for specific use cases, such as intercepting events before they reach their target.&lt;/p&gt;

&lt;h3&gt;
  
  
  🛠️ Practical Application:
&lt;/h3&gt;

&lt;p&gt;Imagine building a dynamic dropdown menu or a complex form with multiple interactive elements. Understanding event flow enables us to manage these interactions seamlessly, enhancing user experience and maintaining code maintainability.&lt;/p&gt;

&lt;h3&gt;
  
  
  🎉 Conclusion:
&lt;/h3&gt;

&lt;p&gt;Event flow, with its captivating journey from capturing to bubbling, forms the backbone of interactive web development. Embrace this magic, and unlock endless possibilities in crafting delightful web experiences!&lt;/p&gt;

&lt;p&gt;I have tried to explain all the details of the event flow that I know.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thanks For Reading, Follow Me For More&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>webdev</category>
      <category>frontend</category>
      <category>javascript</category>
      <category>dom</category>
    </item>
    <item>
      <title>WebSockets</title>
      <dc:creator>Subham Dash</dc:creator>
      <pubDate>Fri, 15 Mar 2024 12:04:31 +0000</pubDate>
      <link>https://dev.to/subhamdash45/websockets-3nek</link>
      <guid>https://dev.to/subhamdash45/websockets-3nek</guid>
      <description>&lt;p&gt;A WebSocket is a communication protocol that provides full-duplex communication channels over a single TCP connection. It enables a real-time, event-driven connection between a client and a server.&lt;/p&gt;

&lt;p&gt;"Full duplex" refers to the capability of a communication system to transmit data in both directions simultaneously. In the context of a WebSocket connection, it means that both the client and the server can send and receive data at the same time without having to wait for each other to finish transmitting.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff9i7w40h6thuzn1pz2vc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff9i7w40h6thuzn1pz2vc.png" alt="websocket connection" width="800" height="612"&gt;&lt;/a&gt;&lt;br&gt;
(Image Source: Internet)&lt;/p&gt;

&lt;h3&gt;
  
  
  Best React WebSocket libraries
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Socket.IO: It is a widely used library that provides real-time bidirectional event-based communication between the browser and the server. It offers features like automatic reconnection, fallback options, and support for various transports, making it an excellent choice for building scalable and reliable applications. Socket.IO supports multiple programming languages, including JavaScript, Python, and Java.&lt;/li&gt;
&lt;li&gt;SockJS is a JavaScript library that provides a WebSocket-like object in the browser, even if the server doesn't support WebSockets. It offers a fallback mechanism that uses alternative transport protocols, such as HTTP long-polling, allowing your application to work in environments where websockets are unavailable. SockJS can be used with various backends and programming languages, including Node.js, Java, and Python.&lt;/li&gt;
&lt;li&gt;WS is a simple and lightweight WebSocket implementation for Node.js. It provides a straightforward API for creating WebSocket servers and clients, making it easy to integrate websockets into your Node.js applications. ws offers per-message compression, automatic reconnection, and customizable options for handling incoming and outgoing messages.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  How WebSocket works in simple words
&lt;/h3&gt;

&lt;p&gt;In the case of WebSocket there a handshake will happen between the client and the server and there server tells that I am going to share an HTTP upgrade request. What it meant by is, I am going to upgrade the connect to websocket. Then a connection will open which can help in having bidirectional requests. HTTP is going to be upgraded using method 101(used to change your network protocol)&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;This is a real-time live comment section where all the live comments are broadcast. And in the demo project, I am using socket.io.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We will create a normal HTML file and run the script in the script tag, where we will call io().

&lt;ul&gt;
&lt;li&gt;io(): This is a function provided by the Socket.IO library. It creates a WebSocket connection to the server.&lt;/li&gt;
&lt;li&gt;In the client, we listen to a 'chat message' event and when we receive a message then show it in the UI.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8" /&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge" /&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&amp;gt;
    &amp;lt;title&amp;gt;Document&amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
      body {
        margin: 0;
        padding-bottom: 3rem;
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
          Helvetica, Arial, sans-serif;
      }
      #form {
        background: rgba(0, 0, 0, 0.15);
        padding: 0.25rem;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        display: flex;
        height: 3rem;
        box-sizing: border-box;
        backdrop-filter: blur(10px);
      }
      #input {
        border: none;
        padding: 0 1rem;
        flex-grow: 1;
        border-radius: 2rem;
        margin: 0.25rem;
      }
      #input:focus {
        outline: none;
      }
      #form &amp;gt; button {
        background: #333;
        border: none;
        padding: 0 1rem;
        margin: 0.25rem;
        border-radius: 3px;
        outline: none;
        color: #fff;
      }
      #form &amp;gt; button:hover {
        cursor: pointer;
        background-color: #0f0e0e;
      }
      #messages {
        list-style-type: none;
        margin: 0;
        padding: 0;
      }
      #messages &amp;gt; li {
        padding: 0.5rem 1rem;
      }
      #messages &amp;gt; li:nth-child(odd) {
        background: #efefef;
      }
    &amp;lt;/style&amp;gt;
    &amp;lt;script src="/socket.io/socket.io.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Subham's websocket app&amp;lt;/h1&amp;gt;
    &amp;lt;ul id="messages"&amp;gt;&amp;lt;/ul&amp;gt;
    &amp;lt;div style="position: absolute; bottom: 5px"&amp;gt;
      &amp;lt;body&amp;gt;
        &amp;lt;ul id="messages"&amp;gt;&amp;lt;/ul&amp;gt;
        &amp;lt;form id="form" action=""&amp;gt;
          &amp;lt;input id="input" autocomplete="off" /&amp;gt;&amp;lt;button&amp;gt;Send&amp;lt;/button&amp;gt;
        &amp;lt;/form&amp;gt;
      &amp;lt;/body&amp;gt;
      &amp;lt;script&amp;gt;
        const socket = io();
        const form = document.getElementById("form");
        const input = document.getElementById("input");
        const message = document.querySelector("#messages");

        form.addEventListener("submit", (e) =&amp;gt; {
          e.preventDefault();
          if (input.value) {
            socket.emit("chat message", input.value);
            input.value = "";
          }
        });

        socket.on("chat message", (msg) =&amp;gt; {
          const item = document.createElement("li");
          item.textContent = msg;
          message.appendChild(item);
        });
      &amp;lt;/script&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: I am adding the Backend code for better understanding.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Let us implement a simple Backend that will broadcast the messages/comments
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const { createServer } = require("node:http");
const { join } = require("node:path");
const { Server } = require("socket.io");

const PORT = 3010;
const app = express();
const server = createServer(app);
// The Server class extends the functionality of the provided HTTP server to handle WebSocket connections alongside regular HTTP requests.
const io = new Server(server);

app.get("/", (req, res) =&amp;gt; {
  res.sendFile(join(__dirname, "index.html"));
});

// This line listens for the 'connection' event emitted by clients when they establish a WebSocket connection to the server. When a client connects to the server, this event is triggered.
io.on("connection", (socket) =&amp;gt; {
  console.log("Connection established");
  //  This line sets up an event listener on the socket object for the 'chat message' event. When the client sends a 'chat message' event to the server, this listener is triggered.
  socket.on("chat message", (msg) =&amp;gt; {
    console.log("received message", msg);
    io.emit("chat message", msg);
  });

  socket.on("disconnect", () =&amp;gt; {
    console.log("User disconnected!");
  });
});

server.listen(PORT, () =&amp;gt; {
  console.log("server running at http://localhost:3010");
}); 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here in the demo screenshot, I have opened 2 tabs and from 1st tab, I commented "Hi" and from another, I commented "hello"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxu1swsm5vahkvjqzu2bj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxu1swsm5vahkvjqzu2bj.png" alt="websocket implementation screenshot" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Challenges to consider while implementing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Resource usage&lt;/li&gt;
&lt;li&gt;Connection limits&lt;/li&gt;
&lt;li&gt;Load balancing&lt;/li&gt;
&lt;li&gt;Authentication&lt;/li&gt;
&lt;li&gt;Firewall/Proxy&lt;/li&gt;
&lt;li&gt;Scaling&lt;/li&gt;
&lt;li&gt;Testing/Debugging&lt;/li&gt;
&lt;li&gt;Resource cleanup&lt;/li&gt;
&lt;li&gt;Error handling&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Applications of WebSockets
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Chat Applications&lt;/li&gt;
&lt;li&gt;Live Feeds and Notifications&lt;/li&gt;
&lt;li&gt;Online Gaming&lt;/li&gt;
&lt;li&gt;Financial Trading Platforms&lt;/li&gt;
&lt;li&gt;Real-Time Analytics and Monitoring&lt;/li&gt;
&lt;li&gt;Live Streaming and Broadcasting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I have tried to explain all the details of SSE that I know.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thanks For Reading, Follow Me For More&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>html</category>
      <category>express</category>
    </item>
    <item>
      <title>Server Sent Events(SSE)</title>
      <dc:creator>Subham Dash</dc:creator>
      <pubDate>Wed, 06 Mar 2024 17:27:07 +0000</pubDate>
      <link>https://dev.to/subhamdash45/server-sent-eventssse-6i0</link>
      <guid>https://dev.to/subhamdash45/server-sent-eventssse-6i0</guid>
      <description>&lt;p&gt;A server-sent event is when a web page automatically gets updates from a server. Traditionally, a web page has to send a request to the server to receive new data, then the server will send the response to the client. With server-sent events, a server can send new data to a web page at any time, by pushing messages to the web page.&lt;/p&gt;

&lt;p&gt;If we are implementing SSE, we should keep the following two things in mind:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A long-lived unidirectional communication exists (the communication happens between the server and the client)&lt;/li&gt;
&lt;li&gt;An HTTP connection only&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo0b1kxfkv09df3xyihgm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo0b1kxfkv09df3xyihgm.png" alt="A screen showing how unidirectional communication is happening form server to client" width="800" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Communication flow between Server and Client using Server Sent Events. (Image Source: PubNub)&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;An HTTP request has been made. As long as we do not explicitly close this connection by doing some actions, such as changing the tab, this request will never terminate or close.&lt;/p&gt;

&lt;p&gt;Every time there is new data, the server passes it on a regular interval over the same HTTP network, which is open.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Connection should be live&lt;/li&gt;
&lt;li&gt;The format in which data comes is event-stream&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;We will understand the working of Server-Sent Events using an example. Suppose, we have a stock price listing platform where our stock prices are to be updated continuously. Now for this, we only need a unidirectional connection i.e. server to client, where our server would send the new prices to our clients whenever they are updated on our server.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We will create a normal HTML file and run the script in the script tag, where we will call (/see) and render the latest stock prices whenever the server updates them.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;meta charset="UTF-8"&amp;gt;
  &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
  &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
  &amp;lt;title&amp;gt;SSE Test App&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;h1&amp;gt;In SSE App&amp;lt;/h1&amp;gt;
  &amp;lt;section style="height: 100px; width: 300px; background-color: antiquewhite;"&amp;gt;
  &amp;lt;ul style="list-style-type:disc"&amp;gt;
    &amp;lt;li&amp;gt; Stock1 Price : &amp;lt;span id="first-element"&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;li &amp;gt;Stock2 Price : &amp;lt;span id="second-element"&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;/ul&amp;gt;
  &amp;lt;div id="time"&amp;gt;&amp;lt;/div&amp;gt;

  &amp;lt;/section&amp;gt;

  &amp;lt;script&amp;gt;
    const eventSource = new EventSource('/sse')
    eventSource.onmessage = (event)=&amp;gt;{
      const resObject = JSON.parse(event.data)
      const firstElement = document.getElementById('first-element')
      firstElement.innerText = `${resObject.stock1Rate}`
      const secondElement = document.getElementById('second-element')
      secondElement.innerText = `${resObject.stock2Rate}`
      const timeElement = document.getElementById('time')
      timeElement.innerText = `${resObject.currentTime}`
    }
  &amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: I am adding the Backend code for better understanding.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Let us implement a simple Backend API to serve us stock prices at an interval of 5 seconds.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const {join} = require('node:path')

const PORT = 3010
const app = express()

app.use(express.static("public"));

app.get("/sse",(req, res)=&amp;gt;{
  res.setHeader('Content-Type', 'text/event-stream');
  res.setHeader('Connection','keep-live');
  res.setHeader('Cache-Control', 'no-cache');

  const intervalId = setInterval(() =&amp;gt; {
    const stock1Rate = Math.floor(Math.random() * 30000);
    const stock2Rate = Math.floor(Math.random() * 40000);
    const currentTime = new Date().toLocaleTimeString()
    res.write(`data: ${JSON.stringify({currentTime,stock1Rate, stock2Rate})} \n\n`)
  }, 5000);

  req.on('close', ()=&amp;gt;{
    clearInterval(intervalId)
  })

})

app.get("/", (req, res) =&amp;gt; {
  res.sendFile(join(__filename, "/index.html"));
});

app.listen(PORT,()=&amp;gt;{
  console.log(`App is connected to ${PORT}`)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the UI the stock prices will change after each 5sec -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjkct2svlseanqz9teq9w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjkct2svlseanqz9teq9w.png" alt="stock price listing application" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see how the response is received in the browser-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpxwuks5z9ue90l633479.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpxwuks5z9ue90l633479.png" alt="response received from server" width="800" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges that we may face while implementing
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Browser compatibility&lt;/li&gt;
&lt;li&gt;Connection limit&lt;/li&gt;
&lt;li&gt;Connection timeout&lt;/li&gt;
&lt;li&gt;Background tab behavior&lt;/li&gt;
&lt;li&gt;Resource utilization&lt;/li&gt;
&lt;li&gt;Load balancer&lt;/li&gt;
&lt;li&gt;Sticky connection&lt;/li&gt;
&lt;li&gt;Proxy/Firewall&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I have tried to explain all the details of SSE that I know.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thanks For Reading, Follow Me For More&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>express</category>
      <category>webdev</category>
    </item>
    <item>
      <title>A description of how browser rendering works</title>
      <dc:creator>Subham Dash</dc:creator>
      <pubDate>Wed, 23 Nov 2022 13:26:48 +0000</pubDate>
      <link>https://dev.to/subhamdash45/how-does-the-browser-actually-render-a-website-e04</link>
      <guid>https://dev.to/subhamdash45/how-does-the-browser-actually-render-a-website-e04</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The Browser render Story (3 Part Series)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The process of rendering HTML&lt;/li&gt;
&lt;li&gt;The process of rendering CSS&lt;/li&gt;
&lt;li&gt;The Process of rendering JS&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Overview&lt;/li&gt;
&lt;li&gt;From raw bytes of HTML to DOM&lt;/li&gt;
&lt;li&gt;From raw bytes of CSS to CSSOM&lt;/li&gt;
&lt;li&gt;Laying out the render tree&lt;/li&gt;
&lt;li&gt;The Process of rendering JS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This article explains how your browser converts HTML, CSS, and JavaScript into a working website that you can interact with.&lt;/p&gt;

&lt;p&gt;Let’s get started.&lt;/p&gt;

&lt;h3&gt;
  
  
  Overview &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;A web browser is a piece of software that loads files from a remote server (or perhaps a local disk) and displays them to you allowing for user interaction.&lt;/p&gt;

&lt;p&gt;However, within a browser, there’s a piece of software that figures out what to display to you based on the files it receives. This is called the browser engine.&lt;/p&gt;

&lt;p&gt;The browser engine is a core software component of every major browser, and different browser manufacturers call their engines by different names. The browser engine for Firefox is called Gecko, and Chrome’s is called Blink, which happens to be a fork of WebKit.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The process of rendering HTML&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;When you write some HTML, CSS, and JS, and attempt to open the HTML file in your browser, the browser reads the raw bytes of HTML from your hard disk (or network).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;- The browser reads the raw bytes of data, and not the actual characters of code you have written.&lt;/p&gt;

&lt;p&gt;The browser receives the bytes of data, but it can’t really do anything with it; the raw bytes of data must be converted to a form it understands. This is the first step.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa513gvfh43ckvik63539.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa513gvfh43ckvik63539.jpg" alt="Image description" width="730" height="587"&gt;&lt;/a&gt;&lt;br&gt;
(credit: internet)&lt;/p&gt;
&lt;h2&gt;
  
  
  From raw bytes of HTML to DOM  &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;First, the raw bytes of data are converted into characters. This conversion is done based on the character encoding of the HTML file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcuxxmo5qnd30c6ohk1d7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcuxxmo5qnd30c6ohk1d7.png" alt="Image description" width="800" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Characters are great, but they aren’t the final result. These characters are further parsed into something called tokens.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg7hiecax8uq1oqefo6ol.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg7hiecax8uq1oqefo6ol.png" alt="Image description" width="800" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Without this tokenization process, the bunch of characters will just result in a bunch of meaningless text, i.e., HTML code — and that doesn’t produce an actual website.&lt;/p&gt;

&lt;p&gt;When you save a file with the .html extension, you signal to the browser engine to interpret the file as an HTML document. The way the browser interprets this file is by first parsing it. In the parsing process, and particularly during tokenization, every start and end HTML tag in the file is accounted for.&lt;/p&gt;

&lt;p&gt;The parser understands each string in angle brackets (e.g. - &lt;code&gt;&amp;lt;html&amp;gt;, &amp;lt;p&amp;gt;&lt;/code&gt;) and understands the set of rules that apply to each of them. For example, a token that represents an anchor tag will have different properties from one that represents a paragraph token.&lt;/p&gt;

&lt;p&gt;Conceptually, you may see a token as some sort of data structure that contains information about a certain HTML tag. Essentially, an HTML file is broken down into small units of parsing called tokens. This is how the browser begins to understand what you’ve written.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz9pd0invycufrtmqvvjm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz9pd0invycufrtmqvvjm.png" alt="Image description" width="800" height="147"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tokens are great, but they are also not our final result. After the tokenization is done, the tokens are then converted into nodes. You may think of nodes as distinct objects with specific properties. In fact, a better way to explain this is to see a node as a separate entity within the document object tree.&lt;br&gt;
Nodes are great, but they still aren’t the final results.&lt;/p&gt;

&lt;p&gt;Now, here’s the final bit. Upon creating these nodes, the nodes are then linked in a tree data structure known as the DOM. The DOM establishes the parent-child relationships, adjacent sibling relationships, etc. The relationship between every node is established in this DOM object.&lt;/p&gt;

&lt;p&gt;We don’t open the CSS or JS file in the browser to view a webpage. We open the HTML file, most times in the form index.html. This is exactly why you do so: the browser must go through transforming the raw bytes of HTML data into the DOM before anything can happen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvnylx3k1p46fmmm0hq8f.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvnylx3k1p46fmmm0hq8f.jpg" alt="Image description" width="730" height="374"&gt;&lt;/a&gt;&lt;br&gt;
(credit: internet)&lt;/p&gt;

&lt;p&gt;Depending on how large the HTML file is, the DOM construction process may take some time. No matter how small, it does take some time, regardless of the file size.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftf3rvhaxfz85bfdu9e0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftf3rvhaxfz85bfdu9e0h.png" alt="Image description" width="800" height="189"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  2. The process of rendering CSS&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;A typical HTML file with some CSS will have the stylesheet linked as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;

&amp;lt;head&amp;gt;
  &amp;lt;link rel="stylesheet" href="style.css"&amp;gt;
  &amp;lt;title&amp;gt;Document&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;

&amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the browser receives the raw bytes of data and kicks off the DOM construction process, it will also make a request to fetch the &lt;code&gt;main.css&lt;/code&gt; stylesheet linked. As soon the browser begins to parse the HTML, upon finding a &lt;code&gt;link&lt;/code&gt; tag to a CSS file, it simultaneously makes a request to fetch that.&lt;/p&gt;

&lt;h2&gt;
  
  
  From raw bytes of CSS to CSSOM  &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;As you can see, a similar process occurs when the browser receives raw CSS bytes.&lt;/p&gt;

&lt;p&gt;This is achieved by converting raw bytes of data into characters, then tokenizing them. In addition, nodes are formed, and then a tree structure is formed.&lt;/p&gt;

&lt;p&gt;DOM stands for Document Object Model. Most people know what it is. In the same way, CSS has a tree structure called CSS Object Model (CSSOM).&lt;/p&gt;

&lt;p&gt;Both HTML and CSS cannot be accessed by the browser by raw bytes. This has to be converted to a form it recognizes — and that happens to be these tree structures.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F53midncdlo6qjccuhffu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F53midncdlo6qjccuhffu.png" alt="Image description" width="800" height="195"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CSS has something called the "cascade". The cascade is how the browser determines what styles are applied to an element. Because styles affecting an element may come from a parent element (i.e., via inheritance), or have been set on the element themselves, the CSSOM tree structure becomes important.&lt;/p&gt;

&lt;p&gt;All well and good. The browser has the DOM and CSSOM objects. Can we have something rendered to the screen now?&lt;/p&gt;

&lt;h2&gt;
  
  
  The render tree
&lt;/h2&gt;

&lt;p&gt;What we have right now are two independent tree structures that don’t seem to have a common goal.&lt;/p&gt;

&lt;p&gt;The DOM and CSSOM tree structures are two independent structures. The DOM contains all the information about the page’s HTML element’s relationships, while the CSSOM contains information on how the elements are styled.&lt;/p&gt;

&lt;p&gt;OK, the browser now combines the DOM and CSSOM trees into something called a render tree.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fey7m6hp7ltnfsdz6jn3p.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fey7m6hp7ltnfsdz6jn3p.jpg" alt="Image description" width="730" height="320"&gt;&lt;/a&gt;&lt;br&gt;
(credit: internet)&lt;br&gt;
The render tree contains information on all visible DOM content on the page and all the required CSSOM information for the different nodes. Note that if an element has been hidden by CSS (e.g., by using display; none), the node will not be represented in the render tree.&lt;/p&gt;

&lt;p&gt;The hidden element will be present in the DOM but not the render tree. This is because the render tree combines information from both the DOM and the CSSOM, so it knows not to include a hidden element in the tree.&lt;/p&gt;

&lt;p&gt;With the render tree constructed, the browser moves on to the next step: layout!&lt;/p&gt;
&lt;h2&gt;
  
  
  Laying out the render tree&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Well, first, the browser has to calculate the exact size and position of each object on the page. It’s like passing on the content and style information of all elements to be rendered on the page to a talented mathematician. This mathematician then figures out the exact position.&lt;/p&gt;

&lt;p&gt;This layout step (which you’ll sometimes hear called the “reflow” step) takes into consideration the content and style received from the DOM and CSSOM and does all the necessary layout computing.&lt;/p&gt;

&lt;p&gt;With the information about the exact positions of each element now computed, all that is left is to “paint” the elements to the screen. The browser now “paints” the individual node to the screen. Finally, the elements are now rendered to the screen!&lt;/p&gt;
&lt;h2&gt;
  
  
  3.The Process of rendering JS&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;You can remove and add elements from the DOM tree, and you may modify the CSSOM properties of an element via JavaScript, and this JS code written in &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;Well, one of the most important things to remember is that whenever the browser encounters a script tag, the DOM construction is paused! The entire DOM construction process is halted until the script finishes executing.&lt;/p&gt;

&lt;p&gt;This is because JavaScript can alter both the DOM and CSSOM. Because the browser isn’t sure what this particular JavaScript will do, it takes precautions by halting the entire DOM construction altogether.&lt;/p&gt;

&lt;p&gt;Best practice is always to put script tag right to the end, just before the closing of body tag. So that the UI will be shown to the user as a first priority.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Demo&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="style.css"&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
    &amp;lt;p id="paragraph"&amp;gt;How Browser Rendering Works&amp;lt;/p&amp;gt;
    &amp;lt;div&amp;gt;&amp;lt;img src="https://xyz.jpg"&amp;gt;
    &amp;lt;script&amp;gt;
        let header = document.getElementById("paragraph");

        console.log("paragraph is: ", paragraph);
    &amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here in the above scenario you'll get the proper output in the cosole as- &lt;code&gt;paragraph is: &amp;lt;p id="paragraph"&amp;gt;How Browser Rendering Works&amp;lt;/p&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But if you put script tag in the head or at the starting of the body then you will get &lt;code&gt;paragraph is: null&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Why? Pretty simple.&lt;/p&gt;

&lt;p&gt;While the HTML parser was in the process of constructing the DOM, a script tag was found. At this time, the body tag and all its content had not been parsed. The DOM construction is halted until the script’s execution is complete.&lt;/p&gt;

&lt;p&gt;By the time the script attempted to access a DOM node with an id of header, it didn’t exist because the DOM had not finished parsing the document!&lt;/p&gt;

&lt;p&gt;So, what happens when the parser encounters a script tag but the CSSOM isn’t ready yet?&lt;/p&gt;

&lt;p&gt;Well, the answer turns out to be simple: the Javascript execution will be halted until the CSSOM is ready.&lt;/p&gt;

&lt;h2&gt;
  
  
  The async attribute
&lt;/h2&gt;

&lt;p&gt;By default, every script is a parser blocker! The DOM construction will always be halted.&lt;/p&gt;

&lt;p&gt;There’s a way to change this default behavior though.&lt;/p&gt;

&lt;p&gt;If you add the async keyword to the script tag, the DOM construction will not be halted. The DOM construction will be continued, and the script will be executed when it is done downloading and ready.&lt;/p&gt;

&lt;p&gt;Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Demo&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="style.css"&amp;gt;
    &amp;lt;script src="index.js" async&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
    &amp;lt;p id="paragraph"&amp;gt;How Browser Rendering Works&amp;lt;/p&amp;gt;
    &amp;lt;div&amp;gt;&amp;lt;img src="https://xyz.jpg"&amp;gt;
&amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>frontend</category>
      <category>webdev</category>
      <category>community</category>
    </item>
  </channel>
</rss>
