<?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>Forem: freakomonk</title>
    <description>The latest articles on Forem by freakomonk (@freakomonk).</description>
    <link>https://forem.com/freakomonk</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%2F19496%2Ffc793ef0-ba5e-4117-bf6e-f7a662c76400.jpg</url>
      <title>Forem: freakomonk</title>
      <link>https://forem.com/freakomonk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/freakomonk"/>
    <language>en</language>
    <item>
      <title>setTimeout vs setImmediate vs Process.nextTick</title>
      <dc:creator>freakomonk</dc:creator>
      <pubDate>Fri, 13 Oct 2023 19:49:31 +0000</pubDate>
      <link>https://forem.com/freakomonk/settimeout-vs-setimmediate-vs-processnexttick-1olg</link>
      <guid>https://forem.com/freakomonk/settimeout-vs-setimmediate-vs-processnexttick-1olg</guid>
      <description>&lt;p&gt;Node provides us the &lt;code&gt;setTimeout&lt;/code&gt;, &lt;code&gt;setImmediate&lt;/code&gt; and &lt;code&gt;Process.nextTick&lt;/code&gt; constructs, but do you know when to use what ? &lt;/p&gt;

&lt;p&gt;To answer that, we need to understand the sequence of Node's event loop. Each stage in a run of event loop is called &lt;code&gt;Phase&lt;/code&gt;. Each of the phases are then executed in a certain order. &lt;/p&gt;

&lt;p&gt;These phases are &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;Timers&lt;/em&gt; - This phase executes the &lt;code&gt;setTimeout&lt;/code&gt; and &lt;code&gt;setInterval&lt;/code&gt; callbacks.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Pending callbacks&lt;/em&gt; - I/O callbacks that have been deferred to this loop run from the last loop run.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Poll&lt;/em&gt; - Looks for new I/O events to be executed. Also executes the callbacks for completed operations.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Check&lt;/em&gt; - Executes the &lt;code&gt;setImmediate&lt;/code&gt; callbacks immediately after the poll phase. Important thing to note is - when the check phase has a &lt;code&gt;setImmediate&lt;/code&gt; to be executed and poll phase is idle, the event loop immediately jumps to check phase without waiting for new I/O events to arrive.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hence, it is fair to assume that &lt;code&gt;setImmediate()&lt;/code&gt; will always execute before &lt;code&gt;setTimeout()&lt;/code&gt;. There is a catch here however as demonstrated by below example&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;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="s1"&gt;Hello&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;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;setImmediate&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="s1"&gt;World&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;span class="c1"&gt;//Output &lt;/span&gt;
&lt;span class="nx"&gt;Hello&lt;/span&gt;
&lt;span class="nx"&gt;World&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the &lt;code&gt;setTimeout&lt;/code&gt; is passed a value 0, the order in which these two timers are executed is not guaranteed to be in sequence. Reason being a &lt;code&gt;setTimeout&lt;/code&gt; with 0ms wait is immediately queued as an I/O event in poll phase, so poll phase might choose to execute &lt;code&gt;setTimeout&lt;/code&gt; or move on to &lt;code&gt;setImmediate&lt;/code&gt; in check phase if poll phase execution time is exhausted. &lt;/p&gt;

&lt;p&gt;But when these two functions are included in a callback of a function, then the &lt;code&gt;setImmediate&lt;/code&gt; is always executed first because the &lt;code&gt;setTimeout&lt;/code&gt; is queued to be executed after the callback is returned. &lt;/p&gt;

&lt;h3&gt;
  
  
  What does &lt;code&gt;Process.nextTick&lt;/code&gt; do ?
&lt;/h3&gt;

&lt;p&gt;The core concept to understand the functioning of &lt;code&gt;Process.nextTick&lt;/code&gt; is the Phase of event loop. As the name suggests, &lt;code&gt;Process.nextTick&lt;/code&gt; executes the code passed to it before the start of next phase. &lt;/p&gt;

&lt;p&gt;Process.nextTick is very useful when you want the code to finish executing before the callback is executed or handle the errors that are important. &lt;/p&gt;

&lt;p&gt;Eg: Consider you are making multiple API calls and the response of the API 'A' is very important for you to check for the user login session. You want to parse this response before other responses so as to decide the user experience. &lt;/p&gt;

&lt;p&gt;What you would do here is place the callback code of API 'A' in a &lt;code&gt;Process.nextTick&lt;/code&gt;, so as soon as the API responds , the code is executed and user can handle that error immediately in same phase before event loop goes to execute the other callbacks.&lt;/p&gt;

&lt;p&gt;Just to show the comparison between &lt;code&gt;setTimeout()&lt;/code&gt; vs &lt;code&gt;setImmediate()&lt;/code&gt; vs &lt;code&gt;Process.nextTick&lt;/code&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="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="s1"&gt;Hello&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;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;setImmediate&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="s1"&gt;World&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;span class="nx"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;nextTick&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="s1"&gt;Dev.to&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;span class="c1"&gt;//output&lt;/span&gt;
&lt;span class="nx"&gt;Dev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;
&lt;span class="nx"&gt;Hello&lt;/span&gt;
&lt;span class="nx"&gt;World&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>node</category>
      <category>performance</category>
    </item>
    <item>
      <title>Being a good interviewer - How to perform on the other side of interview table</title>
      <dc:creator>freakomonk</dc:creator>
      <pubDate>Thu, 20 May 2021 01:52:49 +0000</pubDate>
      <link>https://forem.com/freakomonk/being-a-good-interviewer-how-to-perform-on-the-other-side-of-interviewing-table-465g</link>
      <guid>https://forem.com/freakomonk/being-a-good-interviewer-how-to-perform-on-the-other-side-of-interviewing-table-465g</guid>
      <description>&lt;p&gt;There have been plenty of resources discussing about giving interviews. It's time to discuss about conducting them.&lt;/p&gt;

&lt;p&gt;Interview is a perfect opportunity for you/your company to make an impression with a potential hire. It serves as a sneak peek into the company's culture. &lt;/p&gt;

&lt;p&gt;Here are a few things an interviewer should keep in mind before going for their next interview&lt;/p&gt;

&lt;h2&gt;
  
  
  Put efforts into the interview
&lt;/h2&gt;

&lt;p&gt;Do you remember how nervous you were when you were interviewing for your current job ? &lt;/p&gt;

&lt;p&gt;That's exactly what the current interviewee will be going through and even more. &lt;/p&gt;

&lt;p&gt;Before everything else, if you are conducting a remote interview - SWITCH ON THE VIDEO. Have an eye contact at the candidate. It eases their nerves. Never switch off the video, candidate will never know if you are paying attention or fixing that production issue.&lt;/p&gt;

&lt;p&gt;So, be empathetic and put efforts into making the candidate comfortable. Take a few minutes explaining about your company, your org, and your team.&lt;/p&gt;

&lt;h2&gt;
  
  
  Show interest in the candidate
&lt;/h2&gt;

&lt;p&gt;Ask about the candidate, about his current roles and responsibilities. Show interest into what they do and take notes on how that can help the role they are interviewing for. This is also always mandatory, but a bonus.&lt;/p&gt;

&lt;p&gt;Make some small talk, crack a joke or two.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prepare for the interview
&lt;/h2&gt;

&lt;p&gt;Prepare to conduct the interview as if you are the candidate. Have a list of questions ready, make sure you know the answer to all of them in depth. If those questions are open ended, you should be aware of all various solutions and their trade-offs. &lt;/p&gt;

&lt;p&gt;Make sure your questions are following universal terminology and ensure the candidate is understanding the question perfectly.&lt;/p&gt;

&lt;p&gt;Always, start the technical discussion stating your current tech stack. Candidate should know that you are technically qualified enough to conduct this interview, not just by the virtue of working for the company.&lt;/p&gt;

&lt;h2&gt;
  
  
  It's not a competition of wits
&lt;/h2&gt;

&lt;p&gt;This is not a contest to decide who knows more tech stuff. If the roles are swapped, I am pretty sure the candidate can ask some pretty challenging questions as well. &lt;/p&gt;

&lt;p&gt;Respect the experience and knowledge of the candidate. Your company needs good engineers more than they need a job. A tech interview should almost always be a discussion. Simulate the real life working scenario. &lt;/p&gt;

&lt;p&gt;When was the last time you were assigned a task without any details and help offered ?&lt;/p&gt;

&lt;p&gt;Ease into the technical discussion and drop hints at regular intervals whenever you see candidate stuck. Ask them to think aloud and assert them whenever they make good choices. &lt;/p&gt;

&lt;p&gt;Every question you ask is a common task for both of you. There is no fun in asking a very tough question that requires a special insight. Like finding median in an infinite stream of integers. One doesn't come to optimal solution for such problems in a short time. If you are asking such questions, be prepared to nudge them to the right approach. &lt;/p&gt;

&lt;p&gt;Avoid asking tricky questions or outputs of a code snippet. It shows absolute lack of preparation on part of the interviewer and is very detached from real world engineering. Be cognisant of the fact that we all use Google and Stack overflow in our every day job for simplest of the things. I have a friend who actually asks candidate to google and observes if they are good with finding solutions.&lt;/p&gt;

&lt;p&gt;A good question is a small technical task which requires candidate to ponder over approach, decide a data structure and design an algorithm for it. &lt;/p&gt;

&lt;p&gt;One of the brilliant questions I was asked a few years ago is " A Television manufacture firm want to calculate the number of black spots on their defective screens. Compute a solution to do this". This has no mention of a data structure or an algorithm. This will let candidate think over the problem agnostically. It's not nearly as much fun when you frame the same question as "Calculate the number of connected components in a forest".&lt;/p&gt;

&lt;h2&gt;
  
  
  Create an experience
&lt;/h2&gt;

&lt;p&gt;It's the interviewer's responsibility to create a great experience to the interviewee. Even if you feel the candidate is not correct for the role, go ahead and make their time worthwhile. You never know who is going to be referred by the candidate next. &lt;/p&gt;

&lt;p&gt;Make sure you discuss solutions with the candidate. They should leave the interview in a content state that they have actually learnt something in the last hour. This also helps as a proof that you know the answers, it's important you sell yourself as a qualified interviewer. A good interviewing experience is a very good indicator for a potential hire.&lt;/p&gt;

&lt;p&gt;My current company, Fanatics, has an amazing interviewing culture and I so often listen from the new joinees that a major factor of their decision to join is the interview experience. Even I joined this company because I had a great interview with my current manager. The interview felt like I am part of the team already and we are brainstorming together.&lt;/p&gt;

&lt;p&gt;I have seen plenty of times in my network where a candidate refused to join the company even after a good offer because they had bad interviewing experience. Do not be like this company.&lt;/p&gt;

&lt;p&gt;It's as much learning opportunity for the interviewer as for the candidate, make sure you acknowledge if you have learnt anything. &lt;/p&gt;

&lt;p&gt;This is all I had in mind, will make sure to expand this with further ideas. Do let me know what you think of these points. Happy to discuss.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>JSON Web Tokens (JWTs) 101</title>
      <dc:creator>freakomonk</dc:creator>
      <pubDate>Sun, 16 May 2021 13:27:12 +0000</pubDate>
      <link>https://forem.com/freakomonk/json-web-tokens-jwts-e05</link>
      <guid>https://forem.com/freakomonk/json-web-tokens-jwts-e05</guid>
      <description>&lt;h1&gt;
  
  
  JSON Web Tokens (JWT)
&lt;/h1&gt;

&lt;p&gt;To understand the concept of JWT, we need to understand the authentication and authorisation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Authentication&lt;/li&gt;
&lt;li&gt;Authorisation&lt;/li&gt;
&lt;li&gt;JWTs&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Authentication
&lt;/h2&gt;

&lt;p&gt;Authentication is a process in which a user is determined if he is exactly who he is claiming to be. For eg. a user "John Smith" requesting for his FB messages needs to be authenticated by the FB server that he is indeed John Smith, not someone impersonating him. &lt;/p&gt;

&lt;p&gt;Username, password, OTP are common ways of authenticating a user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Authorisation
&lt;/h2&gt;

&lt;p&gt;John, say, needs to access messages of a group he is part of. Now server needs to verify if John is indeed an admin of the group and he is authorised to view the group's inbox. &lt;/p&gt;

&lt;p&gt;Authorisation is the process of checking for the access privilege of the user. Server typically does this by verifying the user id against the roles mapped to it.&lt;/p&gt;

&lt;h2&gt;
  
  
  JSON Web Tokens
&lt;/h2&gt;

&lt;p&gt;JWTs are used to handle both authentication and authorisation. Let us see how JWTs work before we dive into how it is used to ensure authentication and authorisation.&lt;/p&gt;

&lt;p&gt;Like the name says, JWTs are JSON format based tokens used in the web. JWTs usually look like &lt;code&gt;aaaaaaaa.bbbbbbb.ccccccc&lt;/code&gt;. Notice the &lt;code&gt;.&lt;/code&gt; delimiter between sections. These three sections in a JWT are &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Header&lt;/li&gt;
&lt;li&gt;Payload&lt;/li&gt;
&lt;li&gt;Signature
respectively. Each section is in JSON format as specified above.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Header
&lt;/h3&gt;

&lt;p&gt;A JWT header consists of two parts. The type of the token &lt;code&gt;typ&lt;/code&gt; and the algorithm used to sign this token &lt;code&gt;alg&lt;/code&gt;. This &lt;code&gt;alg&lt;/code&gt; section contains an identifier for a hashing algorithm such as &lt;code&gt;HMAC&lt;/code&gt;, &lt;code&gt;SHA256&lt;/code&gt; or &lt;code&gt;RSA&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A typical header section looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"typ"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"JWT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"alg"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"SHA256"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Payload
&lt;/h3&gt;

&lt;p&gt;JWT's payload contains claims related to the token. Claims are basically statements about a specific section of data. Examples of claims in a JWT as &lt;code&gt;issuer_name&lt;/code&gt;, &lt;code&gt;creation_time&lt;/code&gt;, &lt;code&gt;expiration_time&lt;/code&gt; etc. &lt;/p&gt;

&lt;p&gt;The claims are represented by either reserved keywords or custom keywords. The advtange of a reserved keyword is for universal interoperability. As in, any receiver can make a reserved claim mandatory and sender would know what keyword would use without explicit agreement.&lt;/p&gt;

&lt;p&gt;Examples of reserved claims are &lt;code&gt;iss&lt;/code&gt; for issuer, &lt;code&gt;exp&lt;/code&gt; for expiry date, &lt;code&gt;sub&lt;/code&gt; for subject. A JWT can contain both reserved claims and custom claims.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"iss"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"server_identifier"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"sub"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"login_auth"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"exp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1621177091"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;unix&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;epoch&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;standardization&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"user_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"IN876TY34"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;user_id&lt;/code&gt; is a custom claim.&lt;/p&gt;

&lt;p&gt;Note: Both header and payload are Base64Url encoded.&lt;/p&gt;

&lt;h3&gt;
  
  
  Signature
&lt;/h3&gt;

&lt;p&gt;Signature is what provides security to a JWT and prevents tampering of the data. Anyone can see the contents of a JWT payload if it is not encrypted, but any tampering will make the token invalid because tampering of the data changes the signature.&lt;/p&gt;

&lt;p&gt;A signature of a JWT is created with the algorithm specified in the header section along with a secret, encoded sections of header and payload. Since signature is created based on the encoded payload, any changes in the payload will invalidate the signature.&lt;/p&gt;

&lt;p&gt;Though attacker has access to encoded header, payload and algorithm from the JWT, he won't know the secret.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;SHA&lt;/span&gt;&lt;span class="mi"&gt;256&lt;/span&gt;&lt;span class="err"&gt;(&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;base&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="err"&gt;UrlEncode(header)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"."&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;base&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="err"&gt;UrlEncode(payload),&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;secret)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A JWT after creating these sections look like &lt;br&gt;
&lt;code&gt;eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InNoYXNoYW5na2EiLCJwYXNzd29yZCI6IjEyMzQ1IiwiaWF0IjoxNTM2MjgwMjM1LCJleHAiOjE1MzYyODAyNjV9.iplar3jWiW8rh1gU1H6pYaPu6-njCfflrP8GLbx9Imw&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now that we have created a JWT, let us look into how it is used in authentication and authorisation. &lt;/p&gt;
&lt;h2&gt;
  
  
  How to use JWTs ?
&lt;/h2&gt;

&lt;p&gt;JWTs are created during the authentication flow. Once the user logs in with username/password details, a JWT is generated by the server. This JWT is stored by client , usually in local storage, and is sent to server for each subsequent request as part of &lt;code&gt;Authorisation&lt;/code&gt; header.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;'Authorisation': 'Bearer &amp;lt;jwt&amp;gt;'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the payload section of JWT, data related to the authentication and authorisation can be stored which is used by the server to authenticate the user and also verify the user's authorisation for various API access etc. &lt;/p&gt;
&lt;h4&gt;
  
  
  Example of creating JWT using Node.js
&lt;/h4&gt;

&lt;p&gt;We use a npm package&lt;br&gt;
&lt;code&gt;npm install jsonwebtoken&lt;/code&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;jwt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jsonwebtoken&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jwtSecretKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;JWT_SECRET_KEY_STORED_AS_ENVIRONMENT_VARIABLE&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;time&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;iss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;server_identifier&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sub&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;login_auth&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;exp&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1621177091&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;IN876TY34&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sign&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;jwtSecretKey&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A JWT is not to be confused with a cookie. More discussion into this comparison will be subject of next post. A JWT is usually used to store and transmit authorisation data. &lt;/p&gt;

&lt;p&gt;One downside of using a JWT is all the data is stored in it as claims. Since it is passed to server from client for every request, there is lot of data being transmitted over the network everytime and this only gets worse as more and more data is stored in a JWT. &lt;/p&gt;

</description>
      <category>jwt</category>
      <category>webdev</category>
      <category>authorisation</category>
      <category>authentication</category>
    </item>
    <item>
      <title>To be a Full Stack Engineer in 2020</title>
      <dc:creator>freakomonk</dc:creator>
      <pubDate>Sun, 19 Jul 2020 14:47:15 +0000</pubDate>
      <link>https://forem.com/freakomonk/to-be-a-full-stack-engineer-in-2020-3n2d</link>
      <guid>https://forem.com/freakomonk/to-be-a-full-stack-engineer-in-2020-3n2d</guid>
      <description>&lt;p&gt;This is in follow up to the blog post I have written last year about being a Full Stack Engineer in &lt;a href="%5Bhttps://dev.to/freakomonk/to-be-a-full-stack-engineer-in-2019-97m%5D(https://dev.to/freakomonk/to-be-a-full-stack-engineer-in-2019-97m)"&gt;2019&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I have since joined an amazing &lt;a href="//www.fanatics.com"&gt;company&lt;/a&gt; and picked up a few more skills on being a full stack engineer. I have tried to be as concise as possible, yet exhaustive is the skills to be learnt. &lt;/p&gt;

&lt;p&gt;Starting from the front-end. &lt;/p&gt;

&lt;h2&gt;
  
  
  HTML, CSS 
&lt;/h2&gt;

&lt;p&gt;Well, nothing can be done on web without a basic understanding of HTML and CSS. Developers have long moved on from writing actual HTML, CSS with advent of UI libraries but still one should learn the basic building blocks of web. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Mozilla Developer Network is the best resource out there for anything related to web (mostly!). &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML"&gt;https://developer.mozilla.org/en-US/docs/Web/HTML&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/html/"&gt;https://www.w3schools.com/html/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Freecodecamp offers might be the best learning roadmap for HTML, CSS out there is: &lt;a href="https://www.freecodecamp.org/learn/"&gt;https://www.freecodecamp.org/learn/&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Javascript
&lt;/h2&gt;

&lt;p&gt;Javascript is probably the most important skill a web developer or a full stack engineer can have just because of the varied applications of the language. It can be used on the browser and also server-side. &lt;/p&gt;

&lt;p&gt;Freecodecamp &lt;a href="https://www.freecodecamp.org/learn/"&gt;track&lt;/a&gt; also covers Javascript, but my favorite way of learning JS would be to read &lt;a class="comment-mentioned-user" href="https://dev.to/getify"&gt;@getify&lt;/a&gt;
 's "You don't know JS" &lt;a href="https://github.com/getify/You-Dont-Know-JS"&gt;series&lt;/a&gt;. He even recently launches "You don't know JS yet" &lt;a href="https://github.com/getify/You-Dont-Know-JS"&gt;series&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  React
&lt;/h2&gt;

&lt;p&gt;Next we dive into the UI libraries that one must learn. There is a still debate on which is more popular React or Angular,  but since more and more companies are adopting React, let's go with it. &lt;/p&gt;

&lt;p&gt;Kent C Dodds has an excellent &lt;a href="https://egghead.io/courses/the-beginner-s-guide-to-react"&gt;video tutorial&lt;/a&gt; for React Beginners on egghead.io&lt;/p&gt;

&lt;p&gt;Also, its recommended to go through the &lt;a href="https://reactjs.org"&gt;Official docs&lt;/a&gt; for more advanced topics.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redux/Mobx/Context/Recoil
&lt;/h2&gt;

&lt;p&gt;State management is a major problem when designing component based web applications. Each one of Redux/Mobx/Context/Recoil solves the problem in their own  way and having an idea on atleast one of them is imperative. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Redux&lt;/strong&gt; : &lt;a href="https://redux.js.org/tutorials/essentials/part-1-overview-concepts"&gt;Getting Started with Redux&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Mobx&lt;/strong&gt;: &lt;a href="https://mobx.js.org/getting-started"&gt;Intro to Mobx&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Context&lt;/strong&gt;: This is natively supported state management in React - &lt;a href="https://reactjs.org/docs/context.html"&gt;What is React Context&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Recoil&lt;/strong&gt;: &lt;a href="https://recoiljs.org/"&gt;What is Recoil&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  REST
&lt;/h2&gt;

&lt;p&gt;We make tons of API calls daily and a majority of them are powered by REST. It only makes sense to understand the basic principles behind REST and the corresponding HTTP error codes. &lt;br&gt;
&lt;a href="https://dzone.com/articles/introduction-to-rest-api-restful-web-services"&gt;Introduction to RESTful APIs&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  GraphQL
&lt;/h2&gt;

&lt;p&gt;GraphQL is a latest contender for REST but has its own applications. Knowing when to use REST vs GraphQL is important for optimising the application performance. &lt;br&gt;
&lt;a href="https://graphql.org/learn/"&gt;Learn GraphQL&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.howtographql.com/"&gt;How to GraphQL&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Node.js is the server side runtime for JS which enables you to build APIs and host them using servers. Having to code in same language on both browser and server speeds up the developer velocity. &lt;br&gt;
&lt;a href="https://nodejs.org/en/docs/guides/"&gt;Intro to Node.js&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Golang/Java
&lt;/h2&gt;

&lt;p&gt;There are certain limitations to what a Nodejs application can achieve and so for such use-cases we use another OO language like Golang or Java. Java is the most popular one but Golang is fast rising&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Java&lt;/strong&gt;: &lt;a href="https://www.w3schools.com/java/java_intro.asp"&gt;Java Intro&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Golang&lt;/strong&gt;: &lt;a href="https://tour.golang.org/welcome/1"&gt;A Tour of Go&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Databases
&lt;/h2&gt;

&lt;p&gt;There are two types of Databases, SQL and NoSQL. The differences between them both should be learnt and only then we can decide when to use which type of Database. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SQL&lt;/strong&gt;: There are several popular SQL databases. We have Oracle, MySQL etc, but I will go with &lt;a href="https://www.postgresql.org/docs/online-resources/"&gt;Postgres&lt;/a&gt; simply because of its rise and performance.&lt;br&gt;
&lt;strong&gt;NoSQL&lt;/strong&gt;: NoSQL databases are used when there is not many inter dependencies among your tables ( putting it very very simply, you should go read the differences ). Both &lt;a href="https://university.mongodb.com/"&gt;MongoDB&lt;/a&gt; and &lt;a href="https://cassandra.apache.org/doc/latest/getting_started/index.html"&gt;Cassandra&lt;/a&gt; are good candidates.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cache
&lt;/h2&gt;

&lt;p&gt;More often that not, you end up using Cache to store data that is needed frequently from the database. &lt;/p&gt;

&lt;p&gt;Again, noting down the popular ones: &lt;a href="https://university.redislabs.com/"&gt;Redis&lt;/a&gt; &amp;amp; &lt;a href="https://memcached.org/"&gt;Memcached&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Apart from this, a Full stack engineer should know the basics of Cloud ( Azure, AWS or Google Cloud) and &lt;a href="https://web.dev/learn/"&gt;Web design&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are a few options I deliberately skipped from this list such as Typescript and Deno etc to not confuse new engineers entering into the realm.&lt;/p&gt;

&lt;p&gt;Let me know if you see anything amiss or you want to know about any particular tech.&lt;/p&gt;

</description>
      <category>react</category>
      <category>node</category>
      <category>graphql</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Delete an element from a nested array in JS</title>
      <dc:creator>freakomonk</dc:creator>
      <pubDate>Sun, 17 May 2020 01:12:14 +0000</pubDate>
      <link>https://forem.com/freakomonk/delete-an-element-from-a-nested-array-in-js-333m</link>
      <guid>https://forem.com/freakomonk/delete-an-element-from-a-nested-array-in-js-333m</guid>
      <description>&lt;p&gt;&lt;code&gt;Problem Statement:&lt;/code&gt; Delete an element from a nested array in Javascript&lt;/p&gt;

&lt;p&gt;Let's discuss. &lt;/p&gt;

&lt;p&gt;Deleting an element from a regular array is easy. &lt;/p&gt;

&lt;p&gt;Option 1  (when you have a predicate) :&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="cm"&gt;/*predicate*/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Option 2 (when you have an index)&lt;/p&gt;

&lt;p&gt;Either use filter with an additional &lt;code&gt;index&lt;/code&gt; argument or make use of splice&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;indexToBeDeleted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, how would you delete an element from a nested array in an object? Say this is your object structure&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;title&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 World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Article 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;references&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                    &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;www.example.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Smith&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;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;www.example2.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Kent Dodds&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;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;www.example3.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Kyle Smith&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;span class="p"&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;This is a typical example we all see everyday. Say the second reference for the first post is no more valid and it has to be deleted for it to be saved to DB. &lt;/p&gt;

&lt;p&gt;Usually, the location of a nested element in an object is specified by its &lt;code&gt;path&lt;/code&gt;. Here, for our second reference , the &lt;code&gt;path&lt;/code&gt; is &lt;code&gt;articles[0].references[1]&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;How to delete this ? &lt;/p&gt;

&lt;p&gt;The native &lt;code&gt;delete&lt;/code&gt;option in JS is not valid for arrays as it leaves behind an ugly &lt;code&gt;undefined&lt;/code&gt; in the array, that is if you can traverse to that nested array. Also, when given a path you will have to write your own logic to traverse through the object. &lt;/p&gt;

&lt;p&gt;Another good alternative is to use &lt;code&gt;lodash&lt;/code&gt;. It comes with &lt;code&gt;set&lt;/code&gt;&amp;amp; &lt;code&gt;get&lt;/code&gt; methods which are just perfect for this usecase when used in conjunction. &lt;/p&gt;

&lt;p&gt;We want to delete the element at &lt;code&gt;articles[0].references[1]&lt;/code&gt; path from &lt;code&gt;blog&lt;/code&gt; object . &lt;br&gt;
Trick is to devide the above path into two parts &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;parentArrayPath : &lt;code&gt;articles[0].references&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;childIndex : 1
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="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;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lodash&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;span class="nx"&gt;OR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lodash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// depending on where you are using this library&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;refs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;parentArrayPath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;refs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;childIndex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;parentArrayPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;refs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now, &lt;code&gt;blog&lt;/code&gt; object will look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;title&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 World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Article 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;references&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                    &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;www.example.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Smith&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;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;www.example3.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Kyle Smith&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;span class="p"&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;Do let me know if you have a better alternative. Happy to discuss.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Dynamic queries in GraphQL</title>
      <dc:creator>freakomonk</dc:creator>
      <pubDate>Mon, 11 Nov 2019 12:13:03 +0000</pubDate>
      <link>https://forem.com/freakomonk/dynamic-queries-in-graphql-1pbb</link>
      <guid>https://forem.com/freakomonk/dynamic-queries-in-graphql-1pbb</guid>
      <description>&lt;p&gt;A simple GraphQL query looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query myQuery() {
    user {
        age
        firstName
        lastName
        dob
       }
     } 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Above query returns response of the format&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data {
   user {
       age: 23,
       firstName: "John",
       lastName: "Smith",
       dob: "01-01-1970"
   }
 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Well, this is pretty standard stuff. GraphQL endpoint returns the exact fields are requested. &lt;/p&gt;

&lt;p&gt;Now, what if I want to query different fields every time ? For eg, I want to query &lt;code&gt;{ age, firstName }&lt;/code&gt; first time and then &lt;code&gt;{ age, dob }&lt;/code&gt; .  I wouldn't want to fetch &lt;code&gt;firstName&lt;/code&gt; in second case since I won't be needing it. This is a solid approach especially when an API call is involved with certain fields.&lt;/p&gt;

&lt;p&gt;Usecases for this would be when&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There is a checklist on the UI containing which data elements need to be fetched.&lt;/li&gt;
&lt;li&gt;User selects his authorisation criteria and we want to fetch just that.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One way to do this would be to generate queries depending on the fields specified. But this gets pretty messy as soon as we introduce nested jsons.&lt;/p&gt;

&lt;p&gt;To solve this, GraphQL has a very handy feature called &lt;a href="%5Bhttps://medium.com/open-graphql/graphql-directives-3dec6106c384%5D(https://medium.com/open-graphql/graphql-directives-3dec6106c384)%5D(https://www.apollographql.com/docs/graphql-tools/schema-directives/)"&gt;directives&lt;/a&gt;. Very specifically, &lt;code&gt;@include&lt;/code&gt; directive solves the problem of dynamic querying.&lt;/p&gt;

&lt;p&gt;Now, we will rewrite the above query using &lt;code&gt;@include&lt;/code&gt; directive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query myQuery($age: Boolean!, $firstName: Boolean!, $lastName: Boolean!, $dob: Boolean!) {
       user {
       age @include(if: $age)
       firstName @include(if: $firstName)
       lastName @include(if: $lastName)
       dob @include(if: $dob)
       }
}

variables: 
 {
  age: true,
  firstName: true,
  lastName: false,
  dob: false
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The response of the above query would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data {
   user {
       age: 23,
       firstName: "John"
   }
 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It returns just &lt;code&gt;age&lt;/code&gt; and &lt;code&gt;firstName&lt;/code&gt; since only those were set to &lt;code&gt;true&lt;/code&gt; in the variables json. Another good thing about this &lt;code&gt;@include&lt;/code&gt; directive is, GraphQL engine do not even execute the corresponding resolvers thus gaining us performance. Manipulating these variables json is very simple and the query will not need be mutated for every request. To get the values of &lt;code&gt;dob&lt;/code&gt; instead of &lt;code&gt;firstName&lt;/code&gt;, we just need to change &lt;code&gt;firstName: false&lt;/code&gt; and &lt;code&gt;dob: true&lt;/code&gt; in variables json.&lt;/p&gt;

&lt;p&gt;Downside of using this directive is the queries turn out to be much more verbose and the need to repeat the variables at multiple places. But as explained &lt;a href="https://github.com/apollographql/apollo-server/issues/3080#issuecomment-515705883"&gt;here&lt;/a&gt;, it is an inherent part of GraphQL type safety.&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Microservices vs Batch programs</title>
      <dc:creator>freakomonk</dc:creator>
      <pubDate>Mon, 21 Oct 2019 06:20:38 +0000</pubDate>
      <link>https://forem.com/freakomonk/microservices-vs-batch-programs-31na</link>
      <guid>https://forem.com/freakomonk/microservices-vs-batch-programs-31na</guid>
      <description>&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Microservice&lt;/th&gt;
&lt;th&gt;Batch program&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Typically built to serve one usecase&lt;/td&gt;
&lt;td&gt;Supports multiple tasks called jobs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;APIs are built into a microservice&lt;/td&gt;
&lt;td&gt;Batch should never contain APIs since the availability is not guaranteed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hosted by a server framework eg., Netty, Django&lt;/td&gt;
&lt;td&gt;Not hosted but executed periodically by a scheduler eg., cronjob&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Have very high availability requirements&lt;/td&gt;
&lt;td&gt;No requirement of availability. Executed when required&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Interoperability with other microservices and batches might be desired&lt;/td&gt;
&lt;td&gt;Should be interoperable with other microservices and batches&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;High resource intensive tasks are usually not handled by microservices to enable high availability&lt;/td&gt;
&lt;td&gt;Designed for high resource intensive tasks&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>microservices</category>
      <category>batch</category>
      <category>software</category>
      <category>design</category>
    </item>
    <item>
      <title>To be a Full Stack Engineer in 2019</title>
      <dc:creator>freakomonk</dc:creator>
      <pubDate>Tue, 03 Sep 2019 17:07:11 +0000</pubDate>
      <link>https://forem.com/freakomonk/to-be-a-full-stack-engineer-in-2019-97m</link>
      <guid>https://forem.com/freakomonk/to-be-a-full-stack-engineer-in-2019-97m</guid>
      <description>&lt;p&gt;Hi All&lt;/p&gt;

&lt;p&gt;I will list down a few resources to help become a Full Stack Engineer in 2019. &lt;/p&gt;

&lt;p&gt;I am here including the most popular tech stack of React, Redux, Node, GraphQL along with a few resources for system design.&lt;/p&gt;

&lt;p&gt;In order to understand React, Redux, or Node in detail, its recommended to understand a little bit about Javascript. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Javascript&lt;/strong&gt;: Picking up JS depends on your experience with it, but a very good starting point would be to start with fundamentals. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/getify/You-Dont-Know-JS"&gt;https://github.com/getify/You-Dont-Know-JS&lt;/a&gt; is a great place to start learning Javascript. It starts with the very basics and digs deeper into advanced concepts. In addition, it also covers the internals of JS compiler.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;React&lt;/strong&gt;: Official React docs do a pretty impressive job of explaining this library. &lt;a href="https://reactjs.org/"&gt;https://reactjs.org/&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tutorial included in the official site (&lt;a href="https://reactjs.org/tutorial/tutorial.html"&gt;https://reactjs.org/tutorial/tutorial.html&lt;/a&gt;) is more than sufficient to get hold of all basic stuff. To top that, Kent C. Dodds' course on egghead is an amazing course to cover the breadth of React. 
&lt;a href="https://egghead.io/courses/the-beginner-s-guide-to-react"&gt;https://egghead.io/courses/the-beginner-s-guide-to-react&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Redux&lt;/strong&gt;: Before starting with Redux or for any library for that matter, its useful to understand the need behind it. Reasons to use Redux are nicely explained in this article: &lt;a href="https://blog.logrocket.com/why-use-redux-reasons-with-clear-examples-d21bffd5835/"&gt;https://blog.logrocket.com/why-use-redux-reasons-with-clear-examples-d21bffd5835/&lt;/a&gt; .&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To learn more about it, official docs are a good source &lt;a href="https://redux.js.org/introduction/getting-started"&gt;https://redux.js.org/introduction/getting-started&lt;/a&gt; .&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Node&lt;/strong&gt;: Node is the most popular JS framework to build server-side application and it has a very un-intuitive single threaded architecture. Node architecture can be studied from this doc: &lt;a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/"&gt;https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Proper understanding of the Node architecture is needed to build scalable and asynchronous applications. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;GraphQL&lt;/strong&gt;: GraphQL is the latest tech on rise and it makes sense to learn it. GraphQL helps in building optimized data communication APIs for web applications. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://graphql.org/learn/"&gt;https://graphql.org/learn/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.howtographql.com/"&gt;https://www.howtographql.com/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Design&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is an amazing series where Google explains the browser architecture : &lt;a href="https://developers.google.com/web/updates/2018/09/inside-browser-part1"&gt;https://developers.google.com/web/updates/2018/09/inside-browser-part1&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.interviewbit.com/courses/system-design/"&gt;https://www.interviewbit.com/courses/system-design/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://leetcode.com/"&gt;https://leetcode.com/&lt;/a&gt; is also a good place to practice coding in JS.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>node</category>
      <category>react</category>
      <category>redux</category>
      <category>graphql</category>
    </item>
  </channel>
</rss>
