<?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: Shubham</title>
    <description>The latest articles on Forem by Shubham (@shubhamagarwal).</description>
    <link>https://forem.com/shubhamagarwal</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%2F573927%2Fd5d00c66-238d-4f9f-9287-8823fbc23fc0.jpeg</url>
      <title>Forem: Shubham</title>
      <link>https://forem.com/shubhamagarwal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shubhamagarwal"/>
    <language>en</language>
    <item>
      <title>Implement Azure Application Insights with React</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Tue, 17 Aug 2021 13:39:18 +0000</pubDate>
      <link>https://forem.com/shubhamagarwal/implement-azure-application-insights-with-react-3ecl</link>
      <guid>https://forem.com/shubhamagarwal/implement-azure-application-insights-with-react-3ecl</guid>
      <description>&lt;p&gt;When we talk about monitoring/logging option on the UI application side we don't have much option which gives picture perfect dashboard, one way could be passing all the data to the server via some endpoints and creating our own dashboard.&lt;/p&gt;

&lt;p&gt;Today we are going to talk about Application Insights which can be used in applications from web to mobile. &lt;/p&gt;

&lt;p&gt;In web applications like ecommerce we want to capture specific event, page views, track exception, API calls and much more. We can implement Application Insights in the backend code as well so that team can club all the data if required. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's start Configuring AppInsights&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First we need to create the Azure account if you don't have, then create the resource in Azure and have an AppInsights instance so that we can get the &lt;em&gt;instrumentation key&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now install npm packages so that we can start integrating AppInsights.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install @microsoft/applicationinsights-react-js&lt;br&gt;
npm install @microsoft/applicationinsights-web&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's create the file that will setup the instrumentation for us.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// appInsights.js

import { ApplicationInsights } from '@microsoft/applicationinsights-web';
import { ReactPlugin } from '@microsoft/applicationinsights-react-js';
import { createBrowserHistory } from 'history';

const browserHistory = createBrowserHistory({ basename: '' });
const reactPlugin = new ReactPlugin();
const appInsights = new ApplicationInsights({
  config: {
    instrumentationKey: process.env.REACT_APP_APPINSIGHTS_KEY,
    extensions: [reactPlugin],
    extensionConfig: {
      [reactPlugin.identifier]: { history: browserHistory },
    },
  },
});
appInsights.loadAppInsights();
export { reactPlugin, appInsights };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file will setup the connection using the key (instrumentation key) which I have kept in environment file. Let's create one .env file for development.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// .env.development

REACT_APP_APPINSIGHTS_KEY = &amp;lt;paste your instrumentation key here&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;We are done with the initial setup &amp;amp; it's time to track some events.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;trackException&lt;/strong&gt; This event is handy when you want to log exception related to API or if the API itself gets fail.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { appInsights } from 'appInsights';

const properties = {userId : personId}
try {
}
catch(err) {
 appInsights.trackException({ error: err, properties });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;pass the data in properties object which you want to capture along with the error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;trackPageView&lt;/strong&gt; In a device or webpage app, page view telemetry is sent by default when each screen or page is loaded. But you can change that to track page views at additional or different times.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;appInsights.trackPageView("tab1");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So idea is to create a common method and just need to import where ever required and call different methods provided by Azure AppInsights.&lt;/p&gt;

&lt;p&gt;For other methods please checkout this link&lt;br&gt;
&lt;a href="https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics"&gt;https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>azure</category>
      <category>insights</category>
      <category>hooks</category>
    </item>
    <item>
      <title>Upload files to Azure Blob Storage with React</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Fri, 04 Jun 2021 08:27:15 +0000</pubDate>
      <link>https://forem.com/shubhamagarwal/upload-to-azure-blob-storage-with-react-3760</link>
      <guid>https://forem.com/shubhamagarwal/upload-to-azure-blob-storage-with-react-3760</guid>
      <description>&lt;p&gt;We can directly upload large files (zip,.mp4, .mp3, .pdf, or any other type) to blob storage via browser with a Shared Access Signature (SAS) token generated from account.&lt;/p&gt;

&lt;p&gt;In this example we have used CRA to integrate blob storage code.&lt;/p&gt;

&lt;p&gt;We will cover :-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Generating SAS token, Container Name, Storage Name&lt;/li&gt;
&lt;li&gt;Uploading file to Blob&lt;/li&gt;
&lt;li&gt;Getting the list of all files uploaded to blob&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Let's Start :-&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's assume you have your account setup with Azure and have all necessary permission to create container.&lt;/p&gt;

&lt;p&gt;Once you have CRA ready, we will need to use &lt;em&gt;@azure/storage-blob&lt;/em&gt; package to upload files. &lt;/p&gt;

&lt;p&gt;Now we will need to set up storage account name and container name. Container name is something where all your files resides. I would recommend you to follow this link to set up blob level configuration.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.microsoft.com/en-us/azure/developer/javascript/tutorial/browser-file-upload-azure-storage-blob" rel="noopener noreferrer"&gt;https://docs.microsoft.com/en-us/azure/developer/javascript/tutorial/browser-file-upload-azure-storage-blob&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you are done with the setup we can start some coding. let's say below are the configuration values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sasToken = process.env.storagesastoken || "sv=2020-02-10&amp;amp;ss=bfqt&amp;amp;srt=sco&amp;amp;sp=rwdlacuptfx&amp;amp;se=2021-05-28T16:49:40Z&amp;amp;st=2021-05-24T08:49:40Z&amp;amp;spr=https&amp;amp;sig=Ce0EinaxCMsiqnNfo9wCRr8%3D"; 
const containerName = `importfiles`;
const storageAccountName = process.env.storageresourcename || "storagename"; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;while creating the token we need to set start date and the end date with other permission like read, write, delete.&lt;/p&gt;

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

&lt;p&gt;Let's create the file and put the code to choose the file from the folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const DisplayForm = () =&amp;gt; (
    &amp;lt;div&amp;gt;
      &amp;lt;input type="file" onChange={onFileChange} key={inputKey || ''} /&amp;gt;
      &amp;lt;button type="submit" onClick={onFileUpload}&amp;gt;
        Upload!
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are all set and we have everything to upload files over the blob storage, so let's see the code for blob Upload.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const uploadFileToBlob = async (file) =&amp;gt; {
  if (!file) return [];

  // get BlobService = notice `?` is pulled out of sasToken - if created in Azure portal
  const blobService = new BlobServiceClient(
    `https://${storageAccountName}.blob.core.windows.net/?${sasToken}`
  );
  // get Container - full public read access
  const containerClient = blobService.getContainerClient(containerName);

  // upload file
  await createBlobInContainer(containerClient, file);

  // get list of blobs in container
  return getBlobsInContainer(containerClient);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once we pass the file object as argument to the above function it will call createBlobInContainer function. In this function along with file we can also pass the metadata like UserName, UserId etc so that we can trigger some azure function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;await blobClient.setMetadata({UserName : 'shubham'});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another use case can be updating the file path in the DB, in that case we will require metadata to map the files.&lt;/p&gt;

&lt;p&gt;Once we are able to upload the files in blob it executes getBlobsInContainer function which will return all the files in your storage account. This function is optional and depends on you.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const getBlobsInContainer = async (containerClient) =&amp;gt; {
  const returnedBlobUrls = [];
  for await (const blob of containerClient.listBlobsFlat()) {
    // if image is public, just construct URL
    returnedBlobUrls.push(
      `https://${storageAccountName}.blob.core.windows.net/${containerName}/${blob.name}`
    );
  }

  return returnedBlobUrls;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;This is one way of uploading file to blob and the code which I have put here is the basic one, we can extend our code and put some checks for error handling, getting the progress of files.&lt;/p&gt;

&lt;p&gt;We can use Redux with middlewares in case of complex architecture. &lt;/p&gt;

&lt;p&gt;The final code is on &lt;a href="https://github.com/shubhamagarwal/Blob-Storage" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to Handle Multiple Environments in a React App</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sun, 14 Feb 2021 15:11:09 +0000</pubDate>
      <link>https://forem.com/shubhamagarwal/how-to-handle-multiple-environments-in-a-react-app-2fob</link>
      <guid>https://forem.com/shubhamagarwal/how-to-handle-multiple-environments-in-a-react-app-2fob</guid>
      <description>&lt;h1&gt;Use Case&lt;/h1&gt;

&lt;p&gt;While developing a web application most of the developers use Create React App. This means that once you start creating a build, it gets created for a specific environment and continues to exist in the same environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    "test": "node scripts/test.js"
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Developers can only use two environments by default.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Development — NODE_ENV=development when developing the application locally. Uses .env.development by default, if available.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Production — NODE_ENV=production when building the application for deployment. Uses .env.production by default, if available.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Imagine my project has four environments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Development&lt;/li&gt;
&lt;li&gt;Testing&lt;/li&gt;
&lt;li&gt;Staging&lt;/li&gt;
&lt;li&gt;Production&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Suppose you have different API URL for each environment or APPINSIGHTS_KEY which can used for the analytics. You can add the configuration in the .env file. Below are the files which we can used for different environments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.env.development&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;REACT_APP_BASE_API_URL = 'https://learning.int.org/'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.env.qa&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;REACT_APP_BASE_API_URL = 'https://learning.qa.org/'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.env.candidate&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;REACT_APP_BASE_API_URL = 'https://learning.candidate.org/'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.env.production&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;REACT_APP_BASE_API_URL = 'https://learning.production.org/'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: Prefix “REACT_APP_” is required for creating custom variables in React.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we need to change the build script in package.json file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "start-js": "run-s react:start lint",
    "react:start": "react-scripts start",
    "start:development": "env-cmd -f .env.development npm run-script start-js",
    "build:integration": "env-cmd -f .env.integration npm run-script build",
    "build:qa": "env-cmd -f .env.qa npm run-script build",
    "build:candidate": "env-cmd -f .env.candidate npm run-script build",
    "build:production": "env-cmd -f .env.production npm run-script build",
    "build": "set \"GENERATE_SOURCEMAP=false\" &amp;amp;&amp;amp; rimraf ./build &amp;amp;&amp;amp; react-scripts build",
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Access the variables in-app&lt;/em&gt; : &lt;strong&gt;process.env.REACT_APP_BASE_API_URL&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Note: Though we have added four different environments and when you build the app NODE_ENV will be production. To distinguish the builds, you can add REACT_APP_ENV and specify your environment there.&lt;/p&gt;

&lt;p&gt;Hope this article solves some of the problems that occur on day to day basis.&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>react</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
