<?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[TechCret Software - Medium]]></title>
        <description><![CDATA[TechCret Engineering Blog - Medium]]></description>
        <link>https://medium.com/techcret?source=rss----feb77e3748d9---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>TechCret Software - Medium</title>
            <link>https://medium.com/techcret?source=rss----feb77e3748d9---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 13 Nov 2026 18:05:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/techcret" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Centralize Your Logs using Fluentd, Kibana and ElasticSearch]]></title>
            <link>https://medium.com/techcret/centralize-your-logs-using-fluentd-kibana-and-elasticsearch-a9fce8398e06?source=rss----feb77e3748d9---4</link>
            <guid isPermaLink="false">https://medium.com/p/a9fce8398e06</guid>
            <category><![CDATA[logging]]></category>
            <category><![CDATA[fluentd]]></category>
            <category><![CDATA[elk]]></category>
            <category><![CDATA[centralized-logging]]></category>
            <category><![CDATA[td-agent]]></category>
            <dc:creator><![CDATA[Hiten Pratap Singh]]></dc:creator>
            <pubDate>Tue, 30 Jun 2020 15:19:01 GMT</pubDate>
            <atom:updated>2020-06-30T15:19:01.380Z</atom:updated>
            <content:encoded><![CDATA[<p>Today, most of the applications develop using microservice architecture so it become very hard to manage and analyse the logs from these different applications. It also becomes necessary to have some kind of central place to store these logs as logs doesn’t generated from applications alone rather, we have to deal with system logs, server logs and database logs etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*obsViBPCH3DrRb-G2dlCeQ.png" /><figcaption>Fluentd</figcaption></figure><p>So, to deal with this issue, we have to develop some kind of centralised logging system to collect these logs from different sources and then it becomes pretty easy to analyse them.</p><p>Centralized Log Management is an approach where the logs that are generated from sub-systems within the same environment are collected, parsed and stored in a central repository in an organized fashion thereby reducing the overall management effort to identify an issue. There are various open-source as well as paid tools available in the market to accomplish this. Some of the examples are Splunk, LogRythm, LogPacker, Logstash, Fluentd, etc.</p><p>I have already published a blog on how to achieve the said objective using LogStash and it can be read here:</p><p><a href="https://medium.com/hprog99/spring-boot-logs-with-elasticsearch-logstash-and-kibana-elk-c61a378f3cb4">Spring Boot Logs with Elasticsearch, Logstash and Kibana(ELK)</a></p><p>So, let&#39;s start with how to achieve the said objective using Fluentd.</p><p>LogStash is a part of the popular ELK stack provided by elastic while Fluent is a part of Cloud Native Computing Foundation (CNCF). Both are open-source data processing pipeline that can be used for collecting, parsing, and storing logs from different sources. These logs can either be indexed in Elastic search or can be pushed to storage. There are number of plugins available as well to do things more efficiently.</p><ol><li>We must have ElasticSearch and Kibana installed on our system and the guide to install these are here:</li></ol><ul><li><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html">Installing Elasticsearch | Elasticsearch Guide [8.10] | Elastic</a></li><li><a href="https://www.elastic.co/guide/en/kibana/current/install.html">Install Kibana | Kibana Guide [8.10] | Elastic</a></li></ul><p>2. We have to install Fluentd in our system and it doesn’t matter which OS is being used as it’s up and running on most of those.</p><p><a href="https://docs.fluentd.org/v/0.12/quickstart/installation">Installation</a></p><p>3. Next, we have to edit the configuration file for Fluentd. There are following mentioned components used to configure Fluentd.</p><ol><li>source directives determine the input sources.</li><li>match directives determine the output destinations.</li><li>filter directives determine the event processing pipelines. (optional)</li><li>system directives set system wide configuration. (optional)</li><li>label directives group the output and filter for internal<br>routing (optional)</li><li>@include directives include other files. (optional)</li></ol><p>There’s a configuration file named <strong>td-agent.conf</strong> so we will edit this file to configure Fluentd as per our need. This file can be found inside the <strong>/etc/td-agent/</strong> folder.</p><p>I used a sample spring boot application to generate logs so the configuration for that would look like below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79452de2d46d9eefeedef00bc156c651/href">https://medium.com/media/79452de2d46d9eefeedef00bc156c651/href</a></iframe><p>After this step, make sure that Kibana and ElasticSearch is running and then restart the td-agent service. After restarting the service, you can check the logs for td-agent inside the file /var/log/td-agent/td-agent.log and would see something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GyZUzG2dhxF6qdo58bWTUA.png" /><figcaption>Fluentd Logs</figcaption></figure><p>After all these steps, it’s time to open up Kibana dashboard and there you can see the index by the name mentioned in the td-agent config file. After setting up the index in the Kibana then you will start to see your application logs there immediately.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H6LtMFkzM7iixHZ9BND9sw.png" /><figcaption>Kibana Dashboard</figcaption></figure><p>Any kind of source can be used for logs with Fluentd. We just need to make sure to write the format properly as per our logs syntax in the td-agent config file.</p><p>That’s all for this tutorial. Next, I’ll write about comparison between Logstash and Fluentd and some more blogs about how to ships logs from inside of Docker containers. So, stay tuned for more resourceful blogs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a9fce8398e06" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techcret/centralize-your-logs-using-fluentd-kibana-and-elasticsearch-a9fce8398e06">Centralize Your Logs using Fluentd, Kibana and ElasticSearch</a> was originally published in <a href="https://medium.com/techcret">TechCret Software</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Select 2 custom error messages]]></title>
            <link>https://medium.com/techcret/react-select-2-custom-error-messages-fd95392110ad?source=rss----feb77e3748d9---4</link>
            <guid isPermaLink="false">https://medium.com/p/fd95392110ad</guid>
            <category><![CDATA[customized-select]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-select]]></category>
            <dc:creator><![CDATA[Nakul Pant]]></dc:creator>
            <pubDate>Sun, 07 Jun 2020 14:52:56 GMT</pubDate>
            <atom:updated>2020-06-07T14:52:56.192Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/548/0*ftLaZNnjslNEK48k.jpeg" /></figure><p>React Select is a flexible and beautiful Select Input control for ReactJS with multiselect, autocomplete, async and creatable support. React Select is funded by <a href="https://www.thinkmill.com.au/">Thinkmill</a> and <a href="https://atlaskit.atlassian.com/">Atlassian</a>. It represents a whole new approach to developing powerful React.js components that <em>just work</em> out of the box while being extremely customizable.</p><p>So recently I was using react select in one of my projects but stumbled upon an issue of properly displaying error messages next to the select and the library doesn’t support it yet.</p><p>I tried searching for the issue numerous times but no help, after some time when it started to frustrate me I decided to write a wrapper upon it.</p><p>Let&#39;s have a look at the wrapper —</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd73618234b1e16ddd3bce99879cd267/href">https://medium.com/media/fd73618234b1e16ddd3bce99879cd267/href</a></iframe><p>There are a few things that are important regarding our current use case —</p><ol><li>Line number 15 errorMsg as props.</li><li>Line number 34 to 56 which are the CSS property for the error msgs.</li><li>Line number 30 which is for internationalization from <a href="https://react.i18next.com/">https://react.i18next.com/</a> I won’t be covering this plugin in this blog, but for the understanding, this is helpful to use internationalization in a react app.</li><li>Line number 86 to 88 shows the error msg populated by the error message.</li><li>Line number 59 to 61 places the error class on the label of the select component.</li><li>This is just a way I tried using the customization while using react select.</li></ol><p><strong>There can be many better ways. If you do find them please notify us in the comments. Happy to learn.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd95392110ad" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techcret/react-select-2-custom-error-messages-fd95392110ad">React Select 2 custom error messages</a> was originally published in <a href="https://medium.com/techcret">TechCret Software</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Asynchronous javascript: Async /Await]]></title>
            <link>https://medium.com/techcret/asynchronous-javascript-async-await-1af8f57cf8d7?source=rss----feb77e3748d9---4</link>
            <guid isPermaLink="false">https://medium.com/p/1af8f57cf8d7</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[asyncawait]]></category>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[async]]></category>
            <category><![CDATA[await]]></category>
            <dc:creator><![CDATA[Deepak Jha]]></dc:creator>
            <pubDate>Sun, 07 Jun 2020 14:51:56 GMT</pubDate>
            <atom:updated>2020-06-07T14:51:56.648Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*G8lWnstLKnIAEMge.png" /></figure><p>In this blog, I will explain to you how we can simplify the promises in a very simple way, and also this is very easy to understand and use.</p><p>So we already study in the last blog that promise is a big improvement over callbacks but still, promises can be really hard to read and manage especially when we have a long chain of multiple asynchronous events.</p><p>Async and Await is a fine syntactic sugar to make out Asynchronous code to execute like a Synchronous code.</p><h3>Async</h3><p>Async with a function just indicates that this function always returns a promise so when we use the async keyword then it takes the return value and automatically resolves it as a promise.</p><p><strong>Example</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81b56b899c5d68da377007f5f95c1162/href">https://medium.com/media/81b56b899c5d68da377007f5f95c1162/href</a></iframe><h3><strong>Await</strong></h3><p>Async set up a context to use the <strong>await</strong> keyword, the main power of the async function is when we bind it with the await keyword to pause the execution of a function until promise resolve and return its result and this keyword only works inside an async function.</p><p>This is very useful when we need one value after another, like if the second statement dependent on previous statement value, for example, if we need user id before fetching his information;</p><h4>Example</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f97487f6b72c5d896c06711ae54dc394/href">https://medium.com/media/f97487f6b72c5d896c06711ae54dc394/href</a></iframe><p><strong>Few points to remember before using Async Await</strong></p><ol><li>await can not be used in regular function and if we use then end up with an error.</li><li>await will not work in the top-level-code.</li></ol><p><strong>Example</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90e65290ca124b77c2dbd5f94709a69f/href">https://medium.com/media/90e65290ca124b77c2dbd5f94709a69f/href</a></iframe><p>But we can wrap it into an anonymous async function.</p><p><strong>Example</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a2e2fd5ffa2b884f30831968557dd93/href">https://medium.com/media/3a2e2fd5ffa2b884f30831968557dd93/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1af8f57cf8d7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techcret/asynchronous-javascript-async-await-1af8f57cf8d7">Asynchronous javascript: Async /Await</a> was originally published in <a href="https://medium.com/techcret">TechCret Software</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Recovering access to AWS instance if the .pem file is lost]]></title>
            <link>https://medium.com/techcret/recovering-access-to-aws-instance-if-the-pem-file-is-lost-5d963cd429e4?source=rss----feb77e3748d9---4</link>
            <guid isPermaLink="false">https://medium.com/p/5d963cd429e4</guid>
            <category><![CDATA[ssh]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[ssh-keys]]></category>
            <category><![CDATA[aws-ec2]]></category>
            <category><![CDATA[pem]]></category>
            <dc:creator><![CDATA[Nakul Pant]]></dc:creator>
            <pubDate>Sun, 07 Jun 2020 14:51:14 GMT</pubDate>
            <atom:updated>2020-06-07T14:51:14.418Z</atom:updated>
            <content:encoded><![CDATA[<h3>Recovering access to AWS instance if the pem file is lost</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nlvOqcp2oEiVVfMyESWGFQ.png" /></figure><p>So today we are going to cover the scenario of recovering access to an AWS instance if the .pem file for the instance is lost. Some of us might have faced this issue, if not all.</p><p>Suppose we have an instance whose .pem file is lost, destroyed, etc. We know that if an AWS instance is secured with .pem file then we can’t regenerate this .pem file from the AWS again.</p><p>We will be needing a new instance to recover the .pem file for our lost instance. Let’s call this new instance as <strong>helper instance</strong> and lost instance as <strong>recovery instance</strong></p><p>Let’s jump to the steps -</p><ol><li>Every instance has a volume attached to it which has all the user data. So we need to stop the recovery instance. This can be done either from the AWS management console or using AWS SDK through CLI. Make sure you don’t <strong>terminate</strong> your recovery instance.</li><li>Once the recovery instance is stopped. We will detach the volume attached to the recovery instance. Once detached, the volume will be available for use.</li><li>We now need to attach this volume to our helper instance. Since our helper instance already has a root volume (/dev/sda1), our newly attached volume will be secondary (/dev/xvdf1). One thing to keep a note of is our helper instance should be in the same availability zone of the lost instance, else the volume of the lost instance will not be able to attach to the helper instance.</li><li>Mount the new attached volume on the helper instance. We can do it by running this command. <br><strong>mount /dev/xvdf1 /mount</strong><br>This will mount our volume to <strong>/mount</strong> folder.</li><li>Now we need to copy the authorized_keys from our helper instance .ssh folder to the mounted .ssh folder. This will allow the .pem file of the helper instance to log in to the recovery instance.<br><strong>cp ~/.ssh/authorized_keys /mount/home/ubuntu/.ssh/</strong></li><li>Unmount the attached volume from the helper instance.<br><strong>umount /mount</strong></li><li>Detach the volume from the helper instance and reattach it to the recovery instance. Make sure you mount the volume at <strong>/dev/sda1</strong> as the recovery instance does not have any root volume attached to it yet. So this instance has to be mounted at <strong>/dev/sda1</strong>.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Um7KMGvRRe5QoBKZLBmP_Q.png" /></figure><p>8. Use the pem file of helper instance to log in to the recovery instance.</p><p>9. Stop and terminate the helper instance.</p><p>This will let you rest the log in access to the recovery instance.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d963cd429e4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techcret/recovering-access-to-aws-instance-if-the-pem-file-is-lost-5d963cd429e4">Recovering access to AWS instance if the .pem file is lost</a> was originally published in <a href="https://medium.com/techcret">TechCret Software</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Asynchronous Javascript: Promise]]></title>
            <link>https://medium.com/techcret/asynchronous-javascript-promise-61391a7ec934?source=rss----feb77e3748d9---4</link>
            <guid isPermaLink="false">https://medium.com/p/61391a7ec934</guid>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Deepak Jha]]></dc:creator>
            <pubDate>Fri, 29 May 2020 10:26:13 GMT</pubDate>
            <atom:updated>2020-05-29T10:26:13.494Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*t5oPbLaFIa3rPAxt.png" /></figure><p>This is the second part of Asynchronous Javascript in which we will learn about the promise.</p><p>In the previous blog, I will explain to you how challenging it is to manage the order of asynchronous callbacks, and it can lead to callback hell which makes our code hard to read and it is not a good practice to write code. To resolve this problem javascript promise introduced which helps us to organize these callbacks in a much easier way.</p><h3>What is Promise?</h3><p>Javascript promise is an object which represents a successful completion or failure of an asynchronous operation. The promise allows us to write asynchronous code in a more synchronous fashion.</p><p><strong>Example</strong>: if we use the promise to make an Http call to a web server, then we will create a promise object which represents the data that will be returned by the webserver in the future.</p><p>The main advantage of using promise in javascript is better code readability, better handling of the asynchronous operation, and better error handling.</p><h3>Promise States :</h3><p><strong>Pending: </strong>Promise is in the initial state, neither fulfilled nor rejected.</p><p><strong>Fulfilled: </strong>Operation<strong> </strong>successfully completed.</p><p><strong>Rejected: </strong>Operation failed.</p><p>A pending promise will be either fulfilled or rejected if it fulfilled successfully then return data but if rejected then return the reason which is an error.</p><h3>Create and Consumer Promise:</h3><p>A promise constructor is used to create a new Promise object which takes a one callback function as an argument and callback function. There are two parameters in the callback function, resolve and reject.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d6bc00cc7b41158472cebb7cfab83b4/href">https://medium.com/media/2d6bc00cc7b41158472cebb7cfab83b4/href</a></iframe><p>Promises can be consumed by registering functions using <em>.then</em> and <em>.catch</em> methods.</p><ol><li><strong>Then(): </strong>When a promise is either resolved or rejected, then() is invoked. It takes two-parameter. If the promise is <strong>resolved</strong>, the first function is executed and a result is received. If the promise is <strong>rejected</strong>, the second function is executed and an error is displayed on the screen.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*reAqKClNJnWtl4sAKehF9Q.png" /><figcaption>(Promise Resolved)</figcaption></figure><p><strong>2.catch(): </strong>Whenever there is an error or the promise gets rejected during execution time, catch() is invoked.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/329/1*JTqSME4fPZ8SEwfhYAeVrg.png" /></figure><h3><strong>Promise.All()</strong></h3><p>Promise.All() is also a promise, takes an array of promises as an input, and gets resolve when all the promises resolved or any one of the promises gets rejected.</p><p><strong>Example</strong>: If you have 5 promises and you need to know when all the promises get resolved or if you want to perform some operation after all the promises resolved and until then you have to wait.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0f7fe5df3f993c13e423d0cc9698a8d/href">https://medium.com/media/c0f7fe5df3f993c13e423d0cc9698a8d/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=61391a7ec934" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techcret/asynchronous-javascript-promise-61391a7ec934">Asynchronous Javascript: Promise</a> was originally published in <a href="https://medium.com/techcret">TechCret Software</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ES6 Fundamentals: Functional Programming]]></title>
            <link>https://medium.com/techcret/es6-fundamentals-functional-programming-4932a10396cc?source=rss----feb77e3748d9---4</link>
            <guid isPermaLink="false">https://medium.com/p/4932a10396cc</guid>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[arrow-functions]]></category>
            <category><![CDATA[iterators]]></category>
            <category><![CDATA[generator]]></category>
            <dc:creator><![CDATA[Hiten Pratap Singh]]></dc:creator>
            <pubDate>Thu, 21 May 2020 13:07:49 GMT</pubDate>
            <atom:updated>2020-05-21T13:07:49.550Z</atom:updated>
            <content:encoded><![CDATA[<p>JavaScript has always been a functional programming language. ES6 added new features to it like Arrow functions, Iterator etc. We will look into these new features in this blog post in details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bvGvYwG-vEBMUx8VWyPKtA.jpeg" /><figcaption>ES6 Functional Programming</figcaption></figure><h3>Arrow Functions</h3><p>Arrow functions provides us a way to write functions in JavaScript in a concise and compact way. Arrow functions — also called “fat arrow” functions, from CoffeeScript (<a href="http://blogs.msdn.com/b/cdnstudents/archive/2013/09/17/visual-studio-tips-for-javascript-coders-try-coffeescript.aspx?WT.mc_id=16547-DEV-sitepoint-article83">a transcompiled language</a>) — are a more concise syntax for writing function expressions. They utilize a new token, =&gt;, that looks like a fat arrow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8ey16dy3Nw5V9EREiuUalg.png" /><figcaption>Arrow Functions Example</figcaption></figure><h3>Iterators</h3><p>In JavaScript an <strong>iterator</strong> is an object which defines a sequence and potentially a return value upon its termination. Specifically, an iterator is any object which implements the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol">Iterator protocol</a> by having a <strong>next()</strong> method that returns an object with two properties: <strong>value</strong> and <strong>done</strong>. We can get the values from a collection by calling <strong>next()</strong> repeatedly and get to know that it’s the end of the collection when we get true in <strong>done</strong> property.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AQkIytZq9vY0fTZpOjUzeA.png" /><figcaption>Iterator Example</figcaption></figure><h3>for of</h3><p>JavaScript has always had a for in construct that we could use for loop through over an array. for in works by enumerating the properties of an object. It means for in iterate over an array because it works with the available indexes inside an array. Sometimes we just want to iterate over values and not to worry about keys or indexes. It’s where for of comes very handy to accomplish the said task.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0zux52CuBJxU9HLV0TzHpg.png" /><figcaption>for of example</figcaption></figure><h3>Generator Function</h3><p>A Generator Function is a function which generates an Iterator. A Generator Function doesn’t require lots of code but it does require a new keyword which is called yield keyword and a special function syntax. Generator functions are written using the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*">function*</a> syntax. When called, generator functions do not initially execute their code. Instead, they return a special type of iterator, called a <strong>Generator</strong>. When a value is consumed by calling the generator’s next method, the Generator function executes until it encounters the yield keyword. The function can be called as many times as desired, and returns a new Generator each time. Each Generator may only be iterated once.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*juquZwBvKrSB1NSet5ey-A.png" /><figcaption>Generator Function Example</figcaption></figure><p>Well, it marks the end of this post in the ES6 blog series.</p><p>You can find the previous posts here:</p><ol><li><a href="https://medium.com/techcret/es6-fundamentals-introduction-bea5ba018475">ES6 Fundamentals: Introduction</a></li><li><a href="https://medium.com/techcret/es6-fundamentals-variables-and-parameters-1208385aec49">ES6 Fundamentals: Variables and Parameters</a></li><li><a href="https://medium.com/techcret/es6-fundamentals-classes-8a5906ad7d35">ES6 Fundamentals: Classes</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4932a10396cc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techcret/es6-fundamentals-functional-programming-4932a10396cc">ES6 Fundamentals: Functional Programming</a> was originally published in <a href="https://medium.com/techcret">TechCret Software</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Asynchronous Javascript: Callbacks]]></title>
            <link>https://medium.com/techcret/asynchronous-javascript-callbacks-7e308c7d9b04?source=rss----feb77e3748d9---4</link>
            <guid isPermaLink="false">https://medium.com/p/7e308c7d9b04</guid>
            <category><![CDATA[callback-hell]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[callback]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Deepak Jha]]></dc:creator>
            <pubDate>Thu, 21 May 2020 10:17:50 GMT</pubDate>
            <atom:updated>2020-05-21T10:18:06.685Z</atom:updated>
            <content:encoded><![CDATA[<p>In this Asynchronous Javascript series, I will share my knowledge about this topic with you guys.</p><p>Javascript is a single-thread programming language which means that it is not good with multi-tasking. It executes code line by line and if a function depends on the result of another function then it has to wait until other function returns the result. In this era when our computer has multiple cores, it is not a good practice to wait until some other task finish.</p><p>Handling Asynchronous operation in javascript is a very important task in javascript because every application is performing some kind of asynchronous operation and for this javascript provide different ways of handling asynchronous operation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*1qDdkrSq23Z0AhlzA9E5xw.png" /></figure><h4>Callbacks</h4><p>The callback is the oldest and simplest way to handle Asynchronous operation in javascript and a callback function is a function that is passed as a parameter to another function and that will execute later.</p><p>It is not necessary that a callback is always Asynchronous, it can be Synchronous too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/141/1*dHsRCqNUIOTWUu4_mBalXA.png" /></figure><p><strong>Example</strong>: setTimeout() function in javascript, it takes two parameters, first is a callback function and another one is the time duration, callback function, in this case, will be executed after the given time duration and until than normal flow of execution of code continue and there will be no code blocking occur.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/369/1*Jn5uL0dCy0YhGs-6w3Gh7Q.png" /></figure><p>In this example, you must be surprised by the output of the code, the reason for this output is that javascript didn’t execute the code in the order we wanted. Javascript didn’t wait for 5 seconds to execute the setTimeout() function. The reason for this behavior of javascript is that we cant call function one after another and expect that they execute in a synchronous manner. Callbacks are useful when we want to execute code after some other code execution complete.</p><p><strong>Create a Callback</strong></p><p>I created a fetchUser function to simulate a fetching username from the server. This function takes two arguments username and callback function and callback function defines inside setTimeout function In which we passing username as a parameter and after that, we call fetchUser function and pass the userName Parameter and invoke callback function in which we are printing username.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/353/1*HxFVfrxuB0SOPVMhGGhSyg.png" /></figure><p><strong>Callback Hell</strong></p><p>In the above code snippet after fetching user if you want to fetch user address, then user contact, etc. For this, we have to invoke fetchAddress inside fetchUser, then for contact, we have to invoke fetchContact inside fetchAddress. This is called callback hell when we stack methods on each other and it is hard to read and not a good practice to write code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/1*lokgdNipSxzUr0eiJjroGA.png" /></figure><p>In the next blog, I will explain to you how to handle callback hell in javascript.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e308c7d9b04" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techcret/asynchronous-javascript-callbacks-7e308c7d9b04">Asynchronous Javascript: Callbacks</a> was originally published in <a href="https://medium.com/techcret">TechCret Software</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Database aware i18n messages — SpringBoot]]></title>
            <link>https://medium.com/techcret/database-aware-i18n-messages-springboot-5715063094ef?source=rss----feb77e3748d9---4</link>
            <guid isPermaLink="false">https://medium.com/p/5715063094ef</guid>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[i18n]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[spring-boot-2]]></category>
            <dc:creator><![CDATA[Nakul Pant]]></dc:creator>
            <pubDate>Sat, 02 May 2020 02:02:13 GMT</pubDate>
            <atom:updated>2020-05-02T02:02:13.215Z</atom:updated>
            <content:encoded><![CDATA[<h3>Database aware i18n messages — SpringBoot</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*wCS96MhLwKqnVuwI.jpg" /></figure><p>Continuing from my <a href="https://medium.com/techcret/reading-message-properties-file-programmatically-17f2ab4e488">last story</a> where we migrated the messages stored in the properties file to the database, let’s put them to use now.</p><p>In this story, we will set up a Message Source that will pick all the i18n messages from the database.</p><p>Often we keep our i18n messages in a properties file and use the messages from there. But, what if the user wants the control to make changes to the content at runtime, we cannot ask the user to change the properties file manually. In these scenario usually database comes to our help where we can open up a rest API to update the message content.</p><h4>Locale Resolver</h4><p>Let’s walk through the locale resolver first before diving into the message source. LocaleResolver is required to correctly decide which local is currently used in the application. LocaleResolver interface allows for implementations based on a request, session, cookies, etc. The default implementation in spring-boot is AcceptHeaderLocaleResolver. The locale should be passed in the header called “Accept-Language”. We will stick to the default LocalResolver in our sample code. Please read <a href="https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/LocaleResolver.html">LocaleResolver</a> for more details.</p><h4>Message Source</h4><p>MessageSource interface is used for resolving messages, with support for the parameterization and internationalization of the messages. Spring contains two built-in MessageSource implementations: ResourceBundleMessageSource and ReloadableResourceBundleMessageSource. The latter is able to reload message definitions without restarting the Virtual Machine.</p><p>More on message source <a href="https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/MessageSource.html">here</a>.</p><p>Basically every Spring application needs a message source that is responsible for resolving messages based on some parameters. This can be achieved by <em>getMessage</em>() method of the MessageSource which has three variants that can be consulted on the official spring documentation <a href="https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/MessageSource.html">here</a>.</p><p>We need to set up our message source and the strategy to resolve the messages from the database. We do so by declaring a bean named ‘<strong>messageSource’. </strong>The name is specific to the Spring ecosystem and should be used as it is, however, the implementations can differ obviously.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/085211685adcd2ac1ad65cc317f0f174/href">https://medium.com/media/085211685adcd2ac1ad65cc317f0f174/href</a></iframe><p>Our DatabaseAwareMessageSource is marked with @Component(“messageSource”) which marks the class as a spring component bean named ‘<strong><em>messageSource</em></strong><em>’. Remember to stick to the name :P</em></p><p>Our message source class implements AbstractMessageSource which provides us with a method resolveCode(String messageKey, Locale locale) that needs to be overridden. Our main logic to retrieve the messages based on locales from the database will be implemented here. This method returns a MessageFormat.</p><p>Here I am getting the current tenant in the current request with the help of a utility class TenantContext which obviously am not going to cover here :P</p><p>MessageRepository helps us to find the message content by the message key and locale passed along with the tenantId we fetched above.</p><p>Next, we create and return an instance of MessageFormat which takes in the content based on the message key and the locale.</p><p>That is all for <em>messageSource</em> configuration.</p><p>Next, let&#39;s use this message source. The usage of getting the messages from messageSource is the same.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b608e59189a7682a9647325d21a19ab/href">https://medium.com/media/7b608e59189a7682a9647325d21a19ab/href</a></iframe><p>Let&#39;s say we have a message in the database with the above key and value. This message accepts an argument and is replaced by {0} as the first argument, which is resolved by using getMessage() from the messageSource bean. The database record looks like this in our database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08e983ff276e5bc8b1108c3247304306/href">https://medium.com/media/08e983ff276e5bc8b1108c3247304306/href</a></iframe><p>Usage —</p><pre><strong>throw new </strong>UserNotFoundException(<strong>messageSource</strong>.getMessage(<strong>&quot;not.found.admin&quot;</strong>, <strong>new </strong>String[]{adminCO.getUniqueId()}, Locale.<em>getDefault</em>()))</pre><p>results in throwing an exception with the message that is provided from the database. The <em>getMessage() method </em>takes the message key as the first argument, an array of string objects that will be resolved as placeholders in the message content, and a locale. The placeholder arguments can be accessed based on the index, meaning {0} will be replaced by the element at the 0th index, and {2} will be replaced by the element at 2nd index of the array.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5715063094ef" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techcret/database-aware-i18n-messages-springboot-5715063094ef">Database aware i18n messages — SpringBoot</a> was originally published in <a href="https://medium.com/techcret">TechCret Software</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ES6 Fundamentals: Classes]]></title>
            <link>https://medium.com/techcret/es6-fundamentals-classes-8a5906ad7d35?source=rss----feb77e3748d9---4</link>
            <guid isPermaLink="false">https://medium.com/p/8a5906ad7d35</guid>
            <category><![CDATA[es6-classes]]></category>
            <category><![CDATA[ecmascript-6]]></category>
            <category><![CDATA[es6-js]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Hiten Pratap Singh]]></dc:creator>
            <pubDate>Sat, 02 May 2020 01:51:42 GMT</pubDate>
            <atom:updated>2020-05-02T01:51:42.826Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post of the ES6 Fundamentals blog series, we will learn about the Classes in ES6.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ycz876_PksWq-88nxunLAQ.jpeg" /></figure><p>Actually, the concept of Classes is familiar to many programmers from many programming languages like Java, C++ and C# etc and the goal in the JavaScript is the same.</p><p>The class keyword creates a class definition and a class definition is a blueprint we can use for creating objects. Creating an object from a class is called instantiating an object form a class.</p><p>Well behind the scene, an ES6 class definition is creating a constructor function and manipulating a prototype object. That’s something, we have been doing with JavaScript for years.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LldOHgXGraLsHcvoQDoqzw.png" /><figcaption>Prototype based Classes in JavaScript</figcaption></figure><p>This constructor function and prototype setup works but the syntax is hideous because the doesn’t reflect what we want to do in a straightforward manner.</p><p>Well, this is where the class keyword in ES6 steps in and you can see the exact same code using the class keyword below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FI7FIbak089xFqzaLM4Dog.png" /><figcaption>class keyword-based code in ES6</figcaption></figure><h3>Defining a Class</h3><p>JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript’s existing prototype-based inheritance. The class syntax does not introduce a new object-oriented inheritance model to JavaScript.</p><p>Classes are in fact “special functions”, and just as you can define function expressions and function declarations. The class syntax has two components: class expressions and class declarations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RKd-o5JzfzDUeyjmeWTVuw.png" /><figcaption>Class Declaration</figcaption></figure><h3>Constructor</h3><p>Managing state consistently usually requires some initialization logic for an object. We can implement this logic by proving a constructor. Just like the other member of a class, it’s a function that automatically invoked when you execute new keyword with a class name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6U8CeUtx5YJnv87y3FwxIg.png" /><figcaption>Constructor in an ES6 Class</figcaption></figure><h3>Getters and Setters</h3><p>Once we have some data inside an object, we might want to protect that data. It’s called Encapsulation with getters and setters. Getters and Setters are created using the get and set keywords. They effectively bind a property of an object to a function. So, using getters and setters, we will be writing and reading data using these functions not directly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LH50fPgFAQqWIaO9lnfkxA.png" /><figcaption>Getters and Setters in an ES6 Class</figcaption></figure><h3>Inheritance</h3><p>Inheritance is one of the three pillars of Object-Oriented Programming i.e. Polymorphism, Encapsulation and Inheritance. ES6 gives us an easy syntax to specify Inheritance relationship. Inheritance is a way of saying that a class inherits from another class, and due to Inheritance that class will have all of the methods and state defined by the other class. Inheritance is achieved in ES6 by extends keyword.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-fjHtLnTtWSfd8ZcC-j3aA.png" /><figcaption>Inheritance in ES6</figcaption></figure><h3>super</h3><p>There are times when we want to use Inheritance in a certain way where we will want to interact with our super class on a specific way. Infact there may come scenarios where we must want to interact with our super class and in those scenarios, super keyword comes very handy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Pv0YIROSB7UDh2svJfxShg.png" /><figcaption>super keyword in ES6</figcaption></figure><p>Well, it marks the end of this post in the ES6 blog series.</p><p>You can find the previous posts here:</p><ol><li><a href="https://medium.com/techcret/es6-fundamentals-introduction-bea5ba018475">ES6 Fundamentals: Introduction</a></li><li><a href="https://medium.com/techcret/es6-fundamentals-variables-and-parameters-1208385aec49">ES6 Fundamentals: Variables and Parameters</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a5906ad7d35" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techcret/es6-fundamentals-classes-8a5906ad7d35">ES6 Fundamentals: Classes</a> was originally published in <a href="https://medium.com/techcret">TechCret Software</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reading message.properties file programmatically.]]></title>
            <link>https://medium.com/techcret/reading-message-properties-file-programmatically-17f2ab4e488?source=rss----feb77e3748d9---4</link>
            <guid isPermaLink="false">https://medium.com/p/17f2ab4e488</guid>
            <category><![CDATA[application-properties]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[initializing-bean]]></category>
            <category><![CDATA[spring-boot-2]]></category>
            <category><![CDATA[spring]]></category>
            <dc:creator><![CDATA[Nakul Pant]]></dc:creator>
            <pubDate>Thu, 30 Apr 2020 18:16:43 GMT</pubDate>
            <atom:updated>2020-04-30T18:17:23.041Z</atom:updated>
            <content:encoded><![CDATA[<h3>Reading message.properties(i18n) file programmatically</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*yAdqiYfwG1hzub-X.jpg" /><figcaption>In this blog, we will see how can we access the properties file in Spring Boot programmatically.</figcaption></figure><p>Some time back I was working on a use case to get the localized messages from the database instead of message.properties. Since the application was a multi-tenant application we needed to provide the message customization feature to our users as well.</p><p>Sounds good? But the main issue was where to store the default messages. As it would have been hectic to every time create the entries in the database for every tenant. The problem became more hectic when we started developing the application. So to overcome this problem we used the properties file to contain default messages for each category like validations, API responses, etc.</p><p>This properties file then will be loaded at the booting up of the application and will create the messages with their keys into the databases. I will cover internationalized messages from the database in a separate story.</p><p>So let’s start with a basic example of how to get this done.</p><p>So here’s our properties file that had the validation messages.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/056a816b18f67d4f11c60baae8a59010/href">https://medium.com/media/056a816b18f67d4f11c60baae8a59010/href</a></iframe><p>So these messages were the default messages that will be created for every tenant in the database. Next, we create a database schema to hold these records in the database. We are using MongoDB for the database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6ea286e64cf6dd364f16d40cd866bae/href">https://medium.com/media/f6ea286e64cf6dd364f16d40cd866bae/href</a></iframe><p>Here <em>locale</em> will store the language code, the <em>key</em> will store the message key from the properties file, the <em>content</em> will store the message content from the properties file. <em>tenantId</em> stores the unique key of the tenant for which the message record is created. For simplification, I have categorized the message types to be of types of <strong>VALIDATION, API,</strong> etc stored in <em>messageType.</em></p><p><strong>@UniqueMessageKey(message = “validation.message.key.exists”) </strong>is a custom validator to check that duplicate message keys don’t get created for every tenant. You can read more about Custom Validators in my previous blog <a href="https://medium.com/techcret/custom-validation-across-properties-in-a-class-springboot-ae5be0ddece">here</a>.</p><p>Now we will need a service class that processes our properties file and stores them in the database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c388c7c48ca9cc4400d5574de88944b4/href">https://medium.com/media/c388c7c48ca9cc4400d5574de88944b4/href</a></iframe><p>Our validation service class implements the InitializingBean which will call afterPropertiesSet() overridden method after the initialization. The <em>properties </em>field is a hashmap which will store the key values from the properties file and will be used for other processing. <em>MessageRepository </em>is our repository class for<em> the Message </em>model<em>.</em></p><p>Next, we load the <em>validation.properties </em>as a classpath resource in the <em>afterPropertiesSet</em>() method. We will use to load our resource as properties instance.<br><strong>PropertiesLoaderUtils.loadProperties()</strong> is a convenient method that loads a resource for programmatically accessing the content as key-value. We iterate on this properties instance and store the key-value based message in our defined <em>properties</em> map.</p><p>Now we create a method in this service class that will be used to create the database records of these messages for all tenants.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/545f5c5661372f0114b93a3c9c149b07/href">https://medium.com/media/545f5c5661372f0114b93a3c9c149b07/href</a></iframe><p>In this <em>createValidationMessagesForAllTenants()</em> method, we iterate over the properties map and create database records for the messages per tenants. For the sake of simplicity, we are using default locale in this example.</p><p>Looks good so far. But what if we have to add/modify a new property? Do we have to create a new record every time? Let&#39;s add one more method which will update the records in the database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed747cf8b3b55c66f56f161607c55263/href">https://medium.com/media/ed747cf8b3b55c66f56f161607c55263/href</a></iframe><p>This <em>updateValidationMessagesForAllTenants() </em>will check if a record exists by the message key, if not then this will create the new message record for the tenants.</p><p>Now you must be wondering that we have created these two methods but we aren’t using these methods anywhere. So let’s create a service that will be bootstrapping these messages on application startup and updating the records if changed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2fa2d002d21dd3a01962f51a69eea0f6/href">https://medium.com/media/2fa2d002d21dd3a01962f51a69eea0f6/href</a></iframe><p>Our <em>MessageBootstrap.java </em>is a service class that contains a method <em>createData() </em>that can be called in your application-level bootstrap which caters to the application level bootstrapping of data. We have a boolean flag <em>isUpdatingMessagesAllowed </em>that can be turned on or off to allow updating of messages.<em> </em>This can be achieved in various ways but for simplicity, I have taken it in the method itself.</p><p>In our next story, we will see how to configure and use these database aware localized messages in our application.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17f2ab4e488" width="1" height="1" alt=""><hr><p><a href="https://medium.com/techcret/reading-message-properties-file-programmatically-17f2ab4e488">Reading message.properties file programmatically.</a> was originally published in <a href="https://medium.com/techcret">TechCret Software</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>