<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ejaz Ansari on Medium]]></title>
        <description><![CDATA[Stories by Ejaz Ansari on Medium]]></description>
        <link>https://medium.com/@ejazahmednit?source=rss-8aa0bee28e78------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*X9FYrDt7W6No9vew</url>
            <title>Stories by Ejaz Ansari on Medium</title>
            <link>https://medium.com/@ejazahmednit?source=rss-8aa0bee28e78------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 18:24:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ejazahmednit/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Brainiac vs NodeJsMan(Event Emitter) :]]></title>
            <link>https://medium.com/@ejazahmednit/brainiac-vs-nodejsman-event-emitter-d090a4ea4c37?source=rss-8aa0bee28e78------2</link>
            <guid isPermaLink="false">https://medium.com/p/d090a4ea4c37</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[eventemitter]]></category>
            <dc:creator><![CDATA[Ejaz Ansari]]></dc:creator>
            <pubDate>Sat, 20 May 2023 18:41:54 GMT</pubDate>
            <atom:updated>2023-05-20T18:41:54.361Z</atom:updated>
            <content:encoded><![CDATA[<h3>Brainiac vs NodeJsMan(Event Emitter) :</h3><h3>The Threat Appears:</h3><p>Let me introduce myself, I am Ejaz, dad to a hyper intelligent 5 year (whom I shall refer to as Brainiac, a popular Superman antagonist who is hungry for data).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*4z6KU9BvrxmbzzrW7aIayQ.jpeg" /><figcaption>Brainiac creeps up to NodejsMan to pose a question.</figcaption></figure><p>I am no Superman, but this is the job that I have been handed. What’s the job description you ask? Well, its continuously answering arithmetic questions posed by Brainiac.</p><p>For instance “Baba, what is 65 times 47 ?” . And I am left scratching my head, while on the other hand my development work beckons.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*sUsgwQFLOaseXWlAO6t4tg.jpeg" /><figcaption>NodeJsMan trying to juggle multiple things at a time.</figcaption></figure><h3>An unlikely savior appears:</h3><p>Now, you would be left wondering, what does all of this have to do with javascript? I ‘ll answer that in a bit.</p><p>I have issues with multi-tasking, one could even go ahead and say that my brain is a single threaded application.</p><p>So how I solve the immensely important question posed to me by Brainiac whilst also doing my side job as a developer.</p><p>The answer to this in other multi threaded language paradigms would be to spawn threads for each of the tasks.</p><p>But for frameworks like nodejs, it becomes a problem of blocking one for the other.</p><p>It is at this juncture, that a superhero named <a href="https://nodejs.dev/en/learn/the-nodejs-event-emitter/">Event Emitter</a> jumps in to save the damsel in distress.</p><h3>Solving the problem:</h3><p>What this construct provides us with is the ability to put any task which does not require immediate attention to a separate tick which does not block the transaction path.</p><p>It’s the same solution I use when Brainiac puts forward these tricky questions while I am busy completing an intricate ternary statement.</p><p>My modus operandi is to provide Brainiac with an immediate acknowledgement that his query has been registered by means of an intensely contorted face of mathematical concentration. While I sneak in the actual calculation for processing later. This saves me some time to concentrate on my menial ternary statement.</p><p>This allows my single threaded brain to have a handle on both the things.</p><p>Similarly, in a nodejs application, as soon as a request presents itself, we design the application in such a way that any processor intensive task which does not immediately need to be presented to the caller is emitted as an event.</p><p>Then, an event listener is always listening for such events and immediately latches on to the task of handling the event.</p><p>But wait, the fun doesn’t end here. Nodejs allows us to write multiple listeners to any single event.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*v4dtq3XEbGCCyAY_US9ZQg.png" /></figure><p>Allowing the user to split the event handling to even smaller parts, all of which occur independent of the main transaction path of the request to the server.</p><h3>Aftermath:</h3><p>In a practical example, suppose we have a registered user updating their name.</p><p>We can have three actions to take:</p><p>1. Updating the name in the database (the main transaction)</p><p>2. Sending an email to the user to confirm said change.</p><p>3. Calling an external API to notify other peripheral systems of the change.</p><p>In a more traditional approach, we would do all these things in sequence or at best using parallel promises. But the actions 2 and 3 do not concern the user immediately and are blocking the transaction path unnecessarily.</p><p>With the event emitter paradigm, we can</p><p>1. Update the name in the database, emit the event, respond to the user.</p><p>2. Parallelly two listeners pick up the event outside the main transaction and send an email and call the external API.</p><p>In my case of the underpowered human, I do the following:</p><p>1. Show a thinking face to Brainiac, who glares at my silly face, emit an event to my brain to seek for help.</p><p>2. My brain reads the seeking for help event and immediately calls an external helper API (my wife who has passed on the arithmetical genes to my son).</p><p>3. The response is registered and then my brain is updated with the answer which then is blurted out of my mouth, all the while saving the world from Brainiac’s torment.</p><p>Thus, we see that the Event emitter construct of NodeJs, provides applications to handle various smaller parts of a larger request on different processor ticks.</p><p>There are multiple benefits of this:</p><p>1. Allows the system to respond to the caller much faster with the most important information.</p><p>2. Does not block the transaction path.</p><p>3. Allows us to implement any computation heavy parts of the request separately.</p><p>4. Allows us to records any analytical metrics outside the transaction path.</p><h3>Up Next:</h3><p>In the upcoming weeks, I shall delve deeper into the event emitter pattern and look at more practical applications of the same. We shall look at another world threat from Brainiac and draw analogies with an application like <strong>IMDB</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d090a4ea4c37" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>