<?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: Node Doctors</title>
    <description>The latest articles on DEV Community by Node Doctors (@nodedoctors).</description>
    <link>https://dev.to/nodedoctors</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%2Forganization%2Fprofile_image%2F6231%2F5e783a27-0c27-4025-9921-d30181489b57.png</url>
      <title>DEV Community: Node Doctors</title>
      <link>https://dev.to/nodedoctors</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nodedoctors"/>
    <language>en</language>
    <item>
      <title>4 FREE Resources To Boost Your Node.js Developer Career ⚡</title>
      <dc:creator>Fabrizio Lallo</dc:creator>
      <pubDate>Sun, 04 Dec 2022 23:00:00 +0000</pubDate>
      <link>https://dev.to/nodedoctors/4-free-resources-to-boost-your-nodejs-developer-career-1bpe</link>
      <guid>https://dev.to/nodedoctors/4-free-resources-to-boost-your-nodejs-developer-career-1bpe</guid>
      <description>&lt;p&gt;Do you want to &lt;strong&gt;speed up&lt;/strong&gt; your &lt;strong&gt;Node.js&lt;/strong&gt; career?&lt;/p&gt;

&lt;p&gt;These are the resources I wish I knew when I started learning Node.js, and guess what? They are completely &lt;strong&gt;FREE&lt;/strong&gt;:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Event Loop 🔁
&lt;/h2&gt;

&lt;p&gt;An animated guide to understanding how Event Loop works under the hood.&lt;/p&gt;

&lt;p&gt;What's better than learning through animations and graphics?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✍🏻 &lt;em&gt;Andrew Hu&lt;/em&gt; - &lt;a href="https://dev.to/nodedoctors/an-animated-guide-to-nodejs-event-loop-3g62"&gt;https://dev.to/nodedoctors/an-animated-guide-to-nodejs-event-loop-3g62&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;✍🏻 &lt;em&gt;Fabrizio Lallo&lt;/em&gt; - &lt;a href="https://dev.to/nodedoctors/animated-nodejs-event-loop-phases-1mcp"&gt;https://dev.to/nodedoctors/animated-nodejs-event-loop-phases-1mcp&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Broken Promises 💔
&lt;/h2&gt;

&lt;p&gt;Using promises correctly is crucial to maintain Node.js performance and writing clean code.&lt;/p&gt;

&lt;p&gt;✍🏻 &lt;em&gt;James Snell &amp;amp; Matteo Collina&lt;/em&gt;- &lt;a href="https://www.youtube.com/watch?v=yRyfr1Qcf34&amp;amp;t=1181s" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=yRyfr1Qcf34&amp;amp;t=1181s&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Async Iterators 🔃
&lt;/h2&gt;

&lt;p&gt;Knowing iteration protocols is essential to write more modern and idiomatic Javascript code.&lt;/p&gt;

&lt;p&gt;✍🏻 &lt;em&gt;Luciano Mammino&lt;/em&gt; - &lt;a href="https://github.com/lmammino/iteration-protocols-workshop" rel="noopener noreferrer"&gt;https://github.com/lmammino/iteration-protocols-workshop&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Streams 🚰
&lt;/h2&gt;

&lt;p&gt;Learning streams allow us to build efficient Node.js applications.&lt;/p&gt;

&lt;p&gt;You can build a video streaming service or ingest and transform terabytes of data, which is in great demand nowadays.&lt;/p&gt;

&lt;p&gt;✍🏻 &lt;em&gt;Luciano Mammino&lt;/em&gt; - &lt;a href="https://www.youtube.com/watch?v=yRyfr1Qcf34&amp;amp;t=1181s" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=yRyfr1Qcf34&amp;amp;t=1181s&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;&lt;strong&gt;Let's Connect&lt;/strong&gt;&lt;/em&gt; 🌎 &lt;br&gt;
&lt;em&gt;Follow us on Twitter &lt;a href="https://twitter.com/FabriLallo" rel="noopener noreferrer"&gt;@fabrizio.lallo&lt;/a&gt; and &lt;a href="https://twitter.com/AndrewHu368" rel="noopener noreferrer"&gt;@AndrewHu368&lt;/a&gt; for more Node.js content&lt;/em&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>oop</category>
    </item>
    <item>
      <title>5 Top Node.js Streams Resources You Should Learn to Level Up Your Node.js Skills 🚀💯</title>
      <dc:creator>Fabrizio Lallo</dc:creator>
      <pubDate>Fri, 02 Dec 2022 12:27:31 +0000</pubDate>
      <link>https://dev.to/nodedoctors/5-top-nodejs-streams-resources-you-should-learn-to-level-up-your-nodejs-skills-3d88</link>
      <guid>https://dev.to/nodedoctors/5-top-nodejs-streams-resources-you-should-learn-to-level-up-your-nodejs-skills-3d88</guid>
      <description>&lt;p&gt;If you want to master Node.js, stop screwing around 🛑.&lt;/p&gt;

&lt;p&gt;Learn &lt;strong&gt;Streams.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Streams are the fundamental block of the essential Node.js features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTTP server&lt;/li&gt;
&lt;li&gt;File management&lt;/li&gt;
&lt;li&gt;Encryption and Decryption&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means you can build (TODAY) a video streaming service or ingest and transform terabytes of data just by learning streams.&lt;/p&gt;

&lt;p&gt;You can use these resources and unlock all the Node.js power 🔐🔥.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Streams workshop 📒
&lt;/h2&gt;

&lt;p&gt;Don’t you know anything about theory and practice about streams? This workshop is the gold standard and the perfect place to get started with streams—an ideal combination of theory and practical exercises.&lt;/p&gt;

&lt;p&gt;Author ✍🏻: Luciano Mammino&lt;/p&gt;

&lt;p&gt;Link 🔗: &lt;a href="https://github.com/lmammino/streams-workshop"&gt;https://github.com/lmammino/streams-workshop&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. &lt;strong&gt;Everything you should know about Node.js Streams 📽️&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you are more of a “video guy,” these are fantastic walkthrough about how streams work and how you can use them in your project.&lt;/p&gt;

&lt;p&gt;PLUS 🆕: the first video contains the new stream features introduced in Node.js v18.&lt;/p&gt;

&lt;p&gt;Author ✍🏻: Erik Wendell&lt;/p&gt;

&lt;p&gt;Link 🔗: &lt;a href="https://youtu.be/BdePYKgrMh0"&gt;https://youtu.be/BdePYKgrMh0&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;EXTRA&lt;/em&gt;&lt;/strong&gt;:&lt;br&gt;
Author ✍🏻: Luciano Mammino&lt;/p&gt;

&lt;p&gt;Link 🔗: &lt;a href="https://youtu.be/ldcfYB_mo6Q"&gt;https://youtu.be/ldcfYB_mo6Q&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. &lt;strong&gt;Understanding Streams in Node.js 🔁&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Modern Node.js streams with async iterator.&lt;/p&gt;

&lt;p&gt;Author ✍🏻: Liz Parody&lt;/p&gt;

&lt;p&gt;Link 🔗:  &lt;a href="https://nodesource.com/blog/understanding-streams-in-nodejs/"&gt;https://nodesource.com/blog/understanding-streams-in-nodejs/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. &lt;strong&gt;NodeJS Backpressuring in Streams 🚱&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Backpressure is an important technique to prevent your application from being overwhelmed by data. It is essential to understand and master backpressure.&lt;/p&gt;

&lt;p&gt;Author ✍🏻: Vladimir Topolev&lt;/p&gt;

&lt;p&gt;Link 🔗: &lt;a href="https://enlear.academy/nodejs-backpressuring-in-streams-52638f505e1b"&gt;https://enlear.academy/nodejs-backpressuring-in-streams-52638f505e1b&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. &lt;strong&gt;How To Code A Video Streaming Server in NodeJS 🕹️&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;All Node.js stream knowledge is put into practice to build a real-world example: a video streaming service.&lt;/p&gt;

&lt;p&gt;Author ✍🏻: Abdisalan Mohamud&lt;/p&gt;

&lt;p&gt;Link 🔗: &lt;a href="https://youtu.be/ZjBLbXUuyWg"&gt;https://youtu.be/ZjBLbXUuyWg&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you liked the article, follow us on Twitter &lt;a href="https://twitter.com/FabriLallo"&gt;@fabrizio.lallo&lt;/a&gt; and &lt;a href="https://twitter.com/AndrewHu368"&gt;@AndrewHu368&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Credits to Conny Schneider&lt;/em&gt;&lt;br&gt;
&lt;a href="https://unsplash.com/photos/pREq0ns_p_E"&gt;https://unsplash.com/photos/pREq0ns_p_E&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Node.js animated: Event Loop Phases</title>
      <dc:creator>Fabrizio Lallo</dc:creator>
      <pubDate>Sat, 19 Nov 2022 20:52:26 +0000</pubDate>
      <link>https://dev.to/nodedoctors/animated-nodejs-event-loop-phases-1mcp</link>
      <guid>https://dev.to/nodedoctors/animated-nodejs-event-loop-phases-1mcp</guid>
      <description>&lt;p&gt;In the previous article, &lt;a href="https://dev.to/nodedoctors/an-animated-guide-to-nodejs-event-loop-3g62"&gt;Node.js animated: Event Loop&lt;/a&gt;, you explored how Libuv and the event loop enable asynchronous task handling in Node.js' single-threaded environment. We simplified the event loop as a mechanism that pushes callbacks from a &lt;strong&gt;single event queue&lt;/strong&gt; to an empty call stack. In reality, the event loop is composed of multiple phases, each responsible for specific asynchronous tasks.&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%2Fbkc1k3qhlvj0hrts6ud3.gif" 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%2Fbkc1k3qhlvj0hrts6ud3.gif" alt="event_loop_phases" width="1080" height="608"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The event loop phases
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A phase is a FIFO queue of callbacks to execute. When the event loop enters a given phase, it executes callbacks until the queue is exhausted or the maximum number of callbacks is run and moves to the next stage.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The event loop has &lt;strong&gt;6&lt;/strong&gt; phases, and they run in the following order:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Timers&lt;/strong&gt;: executes callbacks scheduled by setTimeout() and setInterval().&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pending callbacks&lt;/strong&gt;: executes I/O callbacks deferred to the next loop iteration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Idle-prepare&lt;/strong&gt;: only used internally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Poll&lt;/strong&gt;: retrieve new I/O events and execute I/O-related callbacks&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check&lt;/strong&gt;: executes callbacks scheduled by setImmediate().&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Close callbacks&lt;/strong&gt;: some close callbacks, e.g., socket.on('close', …).&lt;/li&gt;
&lt;/ul&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%2F332ug2w37qhov5fgvbv4.gif" 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%2F332ug2w37qhov5fgvbv4.gif" alt="event_loop_phases_handled" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The event loop doesn’t constantly keep spinning. If there are no active I/O handlers and the event loop doesn’t need to process any callbacks, the Node.js program automatically exits. On the other hand, the event loop stops on the poll phase to capture incoming requests whenever you create a web server and the phases are empty.&lt;/p&gt;

&lt;h2&gt;
  
  
  Timers and check phase
&lt;/h2&gt;

&lt;p&gt;You are ready to apply your newly acquired knowledge. Still, you can be disappointed when running a &lt;code&gt;setTimeout&lt;/code&gt; after a &lt;code&gt;setImmediate&lt;/code&gt;, or vice versa, because the result is not deterministic.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;setTimeout&lt;/code&gt; schedules a callback after &lt;strong&gt;at least&lt;/strong&gt; the defined milliseconds have elapsed. The CPU could be busy and Libuv is unable to set the task as complete before the event loop processes the timer phase. Consequently, the setImmediate callback could be run in advance, although the timer is the first phase.&lt;/p&gt;

&lt;p&gt;In this example, the CPU is not busy, and the &lt;code&gt;setTimeout&lt;/code&gt; callback is immediately added to its dedicated queue before &lt;code&gt;setImmedate&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/772833707" width="710" height="399"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In this second example, the event loop starts processing the timer phase, but the timer has not expired yet, so setImmediate callback is run in advance.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/772834525" width="710" height="399"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Exploring the poll phase
&lt;/h2&gt;

&lt;p&gt;You are now wondering how I can test the phases processing order. The following code snippet will help you prove the order of the phases.&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%2Frjla2set11e20tt5k9v9.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%2Frjla2set11e20tt5k9v9.png" alt="poll_phase" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once Node.js starts the program, the event loop processes all the queues and blocks on the poll phase waiting for incoming requests. When someone makes a GET HTTP request to the server, the event loop goes through these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The event loop pushes the request handler on the call stack.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;setImmediate&lt;/code&gt; callback is added in the &lt;strong&gt;check&lt;/strong&gt; phase.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;setTimeout&lt;/code&gt; callback is scheduled in the &lt;strong&gt;timers&lt;/strong&gt; phase.&lt;/li&gt;
&lt;li&gt;The event loop moves to the check phase. The &lt;code&gt;setImmediate&lt;/code&gt; callback is popped off from the queue and pushed on the call stack for execution.&lt;/li&gt;
&lt;li&gt;The event loop does not move any callbacks from empty queues.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;setTimeout&lt;/code&gt; callback is popped off from the timer queue and pushed on the call stack for processing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/772860074" width="710" height="399"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The code is available at:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/fabrilallo/animated-nodejs/tree/main/02-event-loop-phases" rel="noopener noreferrer"&gt;Animated Node.js-02-event-loop-phases repository&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Now, you understand the event loop and its phases. You can also tell the difference between &lt;code&gt;setTimeout&lt;/code&gt; and &lt;code&gt;setImmediate&lt;/code&gt; and expect a non-deterministic behavior when running them in sequence. In addition, you know that event loop blocks on the poll phase waiting for new I/O requests on a Node.js web server.&lt;/p&gt;

&lt;p&gt;But where are promises handled in Node.js?&lt;/p&gt;

&lt;p&gt;You will explore it in more detail in the following articles.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/" rel="noopener noreferrer"&gt;Node.js official documentation: The Node.js Event Loop, Timers, and process.nextTick()&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/libuv/libuv/blob/v1.x/src/unix/core.c#L384" rel="noopener noreferrer"&gt;Libuv event loop source code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;If you liked the article, follow us on Twitter &lt;a href="https://twitter.com/FabriLallo" rel="noopener noreferrer"&gt;@fabrizio.lallo&lt;/a&gt; and &lt;a href="https://twitter.com/AndrewHu368" rel="noopener noreferrer"&gt;@AndrewHu368&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>Node.js animated: Event Loop</title>
      <dc:creator>Andrew Hu</dc:creator>
      <pubDate>Sat, 05 Nov 2022 18:15:13 +0000</pubDate>
      <link>https://dev.to/nodedoctors/an-animated-guide-to-nodejs-event-loop-3g62</link>
      <guid>https://dev.to/nodedoctors/an-animated-guide-to-nodejs-event-loop-3g62</guid>
      <description>&lt;p&gt;We have all heard about JavaScript and Node.js being single-threaded, but what does it mean in practical terms?&lt;/p&gt;

&lt;p&gt;It means that JavaScript can do one thing at a time. For example, we cannot simultaneously multiply and sum numbers. We usually do operations in sequence. We add and then multiply or vice versa. Modern computers are fast, and the result of two or more consecutive tasks seems to be computed simultaneously, but there are exceptions.&lt;/p&gt;

&lt;p&gt;We all have tried to scrape data from that slow website or waited more than thirty seconds before getting the result of a database query. Do we want to block our single thread from executing more tasks because of a slow database query? Luckily, Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.&lt;/p&gt;

&lt;p&gt;What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fj7r4s8bxdbv73bv93vit.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fj7r4s8bxdbv73bv93vit.png" alt="Code sample to showcase the event loop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The V8 JavaScript engine manages a call stack, an essential piece that tracks which part of our program is running. Whenever we invoke a JavaScript function, it gets pushed to the call stack. Once the function reaches its end or a &lt;code&gt;return&lt;/code&gt; statement, it is popped off the stack.&lt;/p&gt;

&lt;p&gt;In our example, the line of code &lt;code&gt;console.log('Starting Node.js')&lt;/code&gt; is added to the call stack and prints &lt;code&gt;Starting Node.js&lt;/code&gt; to the console. By doing so, it reaches the end of the &lt;code&gt;log&lt;/code&gt; function and is removed from the call stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fm2ja8tymo646or3emo07.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fm2ja8tymo646or3emo07.gif" alt="Function invocation on Node.js call stack"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following line of code is a database query. These tasks are immediately popped off because they may take a long time. They are passed to Libuv, which asynchronously handles them in the background. At the same time, Node.js can keep running other code without blocking its single thread.&lt;/p&gt;

&lt;p&gt;In the future, Node.js will know what to do with the query because we have associated a callback function with instructions to handle the task result or error. In our case, it is a simple &lt;code&gt;console.log&lt;/code&gt;, but it could be complex business logic or data processing in production applications.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fl2vxqbfq6r7e55up4bnz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fl2vxqbfq6r7e55up4bnz.gif" alt="Libuv handles I/O ops"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While Libuv handles the query in the background, our JavaScript is not blocked and can continue with &lt;code&gt;console.log(”Before query result”)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F5dnrdqoql3sp9rw0gt1x.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F5dnrdqoql3sp9rw0gt1x.gif" alt="Processing I/O while Node.js runs our code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the query is done, its callback is pushed to the I/O Event Queue to be run shortly*&lt;em&gt;.&lt;/em&gt;* The event loop connects the queue with the call stack. It checks if the latter is empty and moves the first queue item for execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fpp9n3grfwgcaqgi30t4e.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fpp9n3grfwgcaqgi30t4e.gif" alt="The event loop checks for an empty call stack"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code is available at &lt;a href="https://github.com/fabrilallo/event-loop-1" rel="noopener noreferrer"&gt;https://github.com/fabrilallo/event-loop-1&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pop quiz on the event loop
&lt;/h2&gt;

&lt;p&gt;Try to figure out what the following code prints on the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Flu4v4a7ryrp347glwrf1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Flu4v4a7ryrp347glwrf1.png" alt="A more complicated code sample to showcase the event loop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2F1sV8nGA" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimgur.com%2F1sV8nGA" alt="An animated guide for the event loop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The event loop, the delegation, and the asynchronous processing mechanism are Node.js's secret ingredients to process thousands of connections, read/write gigantic files, handling timers while working on other parts of our code.&lt;/p&gt;

&lt;p&gt;In the article, we saw the vital role of Libuv and its ability to handle numerous potentially long-running tasks. At the same time, we went through the event loop and its role as a bridge/connector between callbacks of asynchronous operations in the I/O event queue and the call stack. In the following &lt;a href="https://dev.to/nodedoctors/animated-nodejs-event-loop-phases-1mcp"&gt;articles&lt;/a&gt;, we will explore in greater detail how &lt;strong&gt;timers&lt;/strong&gt;, &lt;strong&gt;I/O&lt;/strong&gt;, &lt;strong&gt;promises&lt;/strong&gt;, and &lt;strong&gt;ticks&lt;/strong&gt; are handled by the different phases of the event loop.&lt;/p&gt;

&lt;p&gt;If you liked the article, follow us on Twitter &lt;a class="mentioned-user" href="https://dev.to/fabriziolallo"&gt;@fabriziolallo&lt;/a&gt;  and &lt;a class="mentioned-user" href="https://dev.to/andrewhu368"&gt;@andrewhu368&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is Node.js?</title>
      <dc:creator>Fabrizio Lallo</dc:creator>
      <pubDate>Sun, 30 Oct 2022 21:38:22 +0000</pubDate>
      <link>https://dev.to/nodedoctors/what-is-nodejs-3if2</link>
      <guid>https://dev.to/nodedoctors/what-is-nodejs-3if2</guid>
      <description>&lt;p&gt;JavaScript had the reputation of being a toy language. A tool to add interactivity to web pages. For a long time, its use cases were limited to the browser, but the creation of Node.js completely changed its role in the modern web development world.&lt;/p&gt;

&lt;p&gt;Node.js allows us to run JavaScript outside the browser, and out of nowhere, a limited language became the favorite companion for thousands of developers working on small and big projects.&lt;/p&gt;

&lt;p&gt;Its vibrant community created many tools that power many applications on servers, desktops, and embedded systems.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js is commonly used with Express or Fastify to power complex backend services.&lt;/li&gt;
&lt;li&gt;Electron package Node.js and Chrome to create cross-platform desktop applications. It is used in production to power programmers’ favorite editor VSCode and messaging applications like Microsoft Teams.&lt;/li&gt;
&lt;li&gt;Embedded systems can leverage low.js to develop applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Node.js?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Before Node.js
&lt;/h3&gt;

&lt;p&gt;Contrary to popular belief, Node wasn’t invented to allow JavaScript outside the browser. The latter was a positive outcome. It was developed to create more performant web servers.&lt;/p&gt;

&lt;p&gt;Ten years ago, handling multiple user connections involved the creation of a new thread, an independent unit of execution within the web server process. This methodology implied two problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you perform a database query, it blocks the entire thread from executing other operations until it receives the result.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j7F2UlE0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kjd6aumd2jb16gnzn34a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j7F2UlE0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kjd6aumd2jb16gnzn34a.png" alt="blocking execution" width="880" height="522"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Handling concurrent connections with associated threads requires high memory consumption and increased difficulty in managing synchronization between them.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Node.js way
&lt;/h3&gt;

&lt;p&gt;JavaScript and Node.js solve the problem of threads' high memory usage and synchronization by using a single thread paired with an event loop mechanism. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gOwyK6km--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lo7tgkn71flt5dadb0zj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gOwyK6km--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lo7tgkn71flt5dadb0zj.png" alt="non-blocking execution" width="880" height="801"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the code snippet above, Node.js can handle concurrent connections and run other pieces of code because the database query execution is delegated to the event loop. The database operation has a callback function which is run when the event loop notifies the single thread that the query is completed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BHOi4FCg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mj5xy47mwvliwnq2o6o1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BHOi4FCg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mj5xy47mwvliwnq2o6o1.png" alt="single thread running the query" width="880" height="1511"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Node.js internal overview
&lt;/h2&gt;

&lt;p&gt;To understand Node.js, we need to understand V8.&lt;/p&gt;

&lt;p&gt;V8 is a C++ program that runs JavaScript code and converts it into instructions understandable by the computer microprocessor. V8 is included in Google Chrome and other browsers to run our websites JavaScript and enable our rich web experience.&lt;/p&gt;

&lt;p&gt;The V8 engine is not limited to modern browsers but can also be embedded into any C++ program and extended with more functionalities. Not by chance, Node.js is a C++ program that integrates the V8 engine and other libraries to enrich JavaScript with new features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;libuv, the C++ library responsible for the event loop, TCP and UDP connection sockets, DNS resolution, file system operations, data encryption, etc.&lt;/li&gt;
&lt;li&gt;C++ bindings are responsible for invoking C++ code when running Node.js-specific JavaScript code.&lt;/li&gt;
&lt;li&gt;The JavaScript core API.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--axyCAFpG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n4z7ypdgiul0y3ddvt1p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--axyCAFpG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n4z7ypdgiul0y3ddvt1p.png" alt="node.js internals overview" width="880" height="568"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Node.js clever design and event loop has empowered thousands of developers and products to create web servers capable of handling thousands of concurrent connections without the hassle of threads’ high memory consumption and synchronization. Node.js event loop capabilities are not limited to database queries and concurrent connections, but they are also used in production to process large files, encrypt/decrypt data, perform network requests, etc.&lt;/p&gt;

&lt;p&gt;You will explore in more detail the event loop in the following article.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you liked the article, follow us on Twitter &lt;a href="https://twitter.com/FabriLallo"&gt;@fabrizio.lallo&lt;/a&gt; and &lt;a href="https://twitter.com/AndrewHu368"&gt;@AndrewHu368&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
