Component based Approach using Typescript

At the end of September 2016, I started learning Typescript.Being a Javascript Engineer,I can digest the fact that Javascript will be replaced by superset “Typescript” and finally I moved to Typescript.

I used to write javascript and use extensibly some of libraries like jquery and underscore.It have three years journey with Backbone.js with four different projects and the way it is written to use DOM manipulation,I just love it.But as the project size,maintaining codebase becomes an issues and to maintain each and every views life-cycle is an overhead to developers,so most of time i have explicitly write code to maintain life-cycle of a view.Then came Typescript,initially for me,it look like java and I was like “Dude,Am I writing backend code in UI ?”,but as time passed i started liking it and now i prefer typescript more over vanilla javascript.

But why there was a need of migrating towards typescript ?

Answer I found after some days : Components

Ok,Components,but that can be achieved with backbone also,but why typescript?

So,you can still use backbone and write a wrapper over it and extend the base class.Inheritance was missing in vanilla javascript that Typescript filled the gap.

Lets go and have a real use case.As the project size increase it is hard to maintain code-base they are likely chances of code duplication.So there was need of generic things that can be included in file to be use and instrument.For example write a generic view component with strictly typed variables so that the contract is not validated and that component can be used widely to maintain life-cycle of a component.


class BasicView {
/** this is generic component that have basic life cycles of a component

public render(){
}

public preRender(){
}

public destroy(){
}
}

Now,with typescript you can include basic component to every component.


class Component1 extend BasicView{

constructor(options){
super(options);
}

/** over-ride basic view methods in child view

public render(){
/** over-ride render method here in child view */
}
}

With Component base approach,component behave as individual identity that can be plug and plug.Component accepts a set of data and returns a set of data.So writing test cases also become easy with component based approach.

So,if you see frontend end trends for last two decades,the world is moving towards components.It has add-on features as code re-usability easy debugging and neat and clean files.It becomes really very easy to debug and enhance a product feature.The main advantage component based approach gives ,it makes the complete product loosely coupled.So extending it become too easy and migration is smooth.If you look at all recent frontend framework,they exhaustibly use component based approach.

I have now become huge fan of component based design,now any feature that i need to plug in my system,the first thing comes to mind,how i can follow component based approach and make it re-usable at other places.

Modern Frontend Technology Stack- An Overview

As javascript is expediting at an aggressive rate.Every six month new framework come into existence and popularity and it has become very difficult for a front-end developer to keep himself updated on day to day basis.

Now,with new framework and features,everyone want to exhaust those library for better and user-friendly interfaces are in demand.When it comes to development of web app,so many conditions comes into picture.Customers are looking for interactive design and value of product.So,to deliver a seamless running webapp it has become more challenging for a frontend Developers.

In the era of smartphones, web developers are expected to develop UI/UX that is responsive and user-friendly. If the web applications frustrate users, then it is difficult to maintain the customer’s loyalty on your website. Website navigation is another part often neglected by developers. Intuitive navigation creates a better user experience for the website visitor.

Slow web applications are a failure and as a result, customers abscond your website, thus damaging your revenue as well as reputation. Some of the performance issues developers’ faces are Poorly written code, Un-Optimized Databases, Unmanaged Growth of data, Traffic spikes, Poor load distribution, Default configuration, Troublesome third party services, etc.

Security is something the web developers need to consider at every stage of SDLC (software development life cycle). There are many things to consider when it comes to web application security such as denial of service attacks, the safety of user data, database malfunctioning, unauthorized access to restricted parts of the website, etc.

Frameworks are boost performance, offer libraries of coding and extend capabilities, so developers need not do hand-coding web applications from the ground up. Frameworks offer features like models, APIs, snippets of code and other elements to develop dynamic web applications.

In the current scerinio when the market is flooded with so many frontend framework.It become hard to figure out which one will best fit the requirement of the product.So Engineer struggle to learn and implement the best way.Just for example Angular 2.x is much more better than angular 1.x .React native is popular among mobile app developments.Ionic…NativeScript and other else.So now the questions arises do the developers need to master one of them or all at a go .

My approach,I write modular code.I write an layer of code to use any framework.Suppose right now for an application i am using backbone .I have written wrappers over backbone as base components.My code interacts with base components.So What is the advantage ? In near future If i wish to replace my framework with angular 2.x / React Native .So i have to make changes in Base Wrapper and my whole application logic and code will remain the same.This is the need of current front-end stack.You can change the framework and use the underlying features with minimal efforts.

Configure UI Layer on apache2

What is Apache2 ?

Apache is the most popular web server on the internet.It is used to serve more than half of all active websites.So lets configure it for our local development.

Installing Apache2 or httpd on Ubuntu or Debian

Open Terminal and run the following commands
    $ sudo apt-get update
    $ sudo apt-get install apache2

Assuming your workspace is in /home/user/workspace/UI directory
    $ cd /var/www
    $ sudo ln -s /home/user/workspace/UI UI
The second command creates a symbolic link to your repository.

Now, we will configure our apache server to serve static contents so that 
we can run our code locally.

Configure Apache2

$ cd /etc/apache2/conf-available

Create a file fqdn.conf inside conf-available directory containing following text:
ServerName localhost

and then again create a symbolic link to fqdn.conf file
$   cd /etc/apache2/conf-enabled
$   sudo ln -s ../conf-available/fqdn.conf fqdn.conf

Add UI workspace files and ProxyPass settings to apache2 server 
Before that let us take a backup of our setting file 000-default.conf.
Backup file name is 000-default.conf.bak

$   cd /etc/apache2/sites-available
$   sudo cp 000-default.conf 000-default.conf.bak

Now modify file 000-default.conf as the following:

Change DocumentRoot to:
DocumentRoot /var/www/UI

Add the following lines towards the end of the file, just before closing </VirtualHost> tag

        ProxyRequests Off
        <Proxy *>
                Order deny,allow
                Allow from all
        </Proxy>

        ProxyPass /api http://IPAddress
        ProxyPassReverse / http://IPAddress

        <Location />
                Order allow,deny
                Allow from all
        </Location>     

After making above changes,
Enable proxy module and restart apache2 server.


    $   sudo a2enmod proxy
    $   sudo a2enmod proxy_http
    $   sudo service apache2 restart
Open up your favorite browser and type localhost
This is all about configuring UI on your local Machine for development.
The same UI can be configured and run on node server.
 


Parameters to measure the peformance of a web App ( Frontend Applications)

I personally thing that anyone comparing to two front end Applications built with two different stack.Performance of any app depends upon various parameters and most importantly it depends upon the Project Requirement.
As far as my experience with building application,I will try to list some of these parameters.
a) Quality of the test/benchmark code : Writing any application/module/function but writing a test case for these are comparatively more tougher.
b) Application Type : The main stack is choose on the basis of application type ,whether is data driven, small application or SPAs .The choice of framework all depends upon on requirement of the product.
c) Server load and Server performance : whether the server is able to handle multiple request .it all depends upon optimizing the server to its best .
d) Quality of Internet Connection at end user and client Side
e) Memory usage by the client Browser and type of Browser used. Resource utilization (CPU/memory/network)
f)Dom Manipulation : DOM manipulation should be very less and it is not good for performance.
g)Garbage Collection by the Browser.

In a nutshell ,no library or framework is good or bad .All we need to learn its pros and cons and best way to exploit it to get our purpose done,be in Backbone, Angular ,Ember or React.

My Experience with Angular Js

I started working on angular late 2015 and I think I have written my share of Code in Angular Js.
In the year 2015, angular and React was popular. Fast.Fast Development and less code base
for a larger application become a requirement .performance was also a factor.The last project was in Backbone and Due to its bad design we have serious issues like sync calls
and large number of HTTP calls .So we decided to move to Angular .
After going through few blogs and articles we decided to use Angular + D3 + NVD3 and
for Development and RequireJs for Dependency and Grunt for Task Manger.The best Tool
was Chrome Dev Tool which helped us throughout our development process .

As we were new to Angular and Product need to be delivered in less than 3 months (90 days ) we were little bit scared.Angular as a steep Learning Curve. The greatest pain is setting the project and once project is setup development usually becomes fast.
As I was from jquery+Backbone background, initially I find it difficult to adjust to two way data binding and few lines of code doing miracles and inbuilt directives support.Later I become fan of Angular .As the project was huge, I keep performance issues also in mind.
Performance really matters a lot as users never waits for more than 5 seconds.if you are slow you will loose the business.
My experience in building a product with Angular stack was awesome and I enjoyed it that I was able to deliver and ultra fast application with more features, custom directives.custom filters and so on.
In my application we have lots of charting libraries ,but High-charts were not supported by Angular so ,I moved to NVD3 and D3 for more user interaction.Initially due to less support I faced certain issues with NVD3 but Later I found hacks and tricks to tackle with it.

With Angular my current application become 2x times faster than previous with more efficiently managing data at front-end side.

Comparison between Apache and Nginx

Both Apache and Nginx are popular and are used in wide variety of applications .
Most of the time it become an important decision to choose between Nginx and Apache for their application .Apache is more popular than Nginx because it covers more use cases and it has been around much longer, hence more people know about it .

Say for example :
With Apache, you just need to run/maintain/configure a single software that handles everything you might need like static files (css/js/images), PHP , Python, etc. It supports a lot of configuration options that cater to various use cases but is a little resource heavy (i.e. uses more CPU/RAM).

Nginx, on the other hand, just serves static files and also allows you to load balance (transfer your incoming request to one among a set of servers, depending on load), and some other stuff too. But it is much lighter on resources. Moreover, you would have to use something else to handle your dynamic content (PHP (programming language)/Python). Thus, you would have to run/maintain/configure multiple pieces of software.

Apache makes copies (process forks) of itself for every request, be it PHP or a css file. Each running version eats up more RAM. So you have a very low limit on the number of things it can do at one time.

Nginx on the other hand uses a fixed amount of RAM because it is event driven. So you can use your RAM for other things, like PHP

So from my side a piece of advice ,Use Apache if you just want to manage one thing and don’t mind resource usage and use nginx if you have lots of static content or are resource constrained or you like to use “the right tool for the right job”!

Why nginx is good at serving static files?

It’s not any better/worse at serving static files than Apache. However, the big difference is that nginx can serve a lot more users without using up additional resources.

Apache makes copies (process forks) of itself for every request, be it PHP or a css file. Each running version eats up more RAM. So you have a very low limit on the number of things it can do at one time.

Nginx on the other hand uses a fixed amount of RAM because it is event driven. So you can use your RAM for other things, like PHP.

Why nginx + apache?

nginx is configured to serve the static (js, css, img) files. So you have a fixed amount of ram handling all the regular files. No need to use up a fat copy of apache to serve something simple.

apache+mod_php, can just focus on creating dynamic HTML. Which means more effective use of your precious RAM.

For more details please go through these links :

Apache vs nginx – WikiVS
Nginx Vs Apache in AWS – Updated
Web Server Performance Comparison
Nginx overtakes Apache as the server of choice for the top 1000 trafficked sites
Apache vs nginx {performance comparison}
Apache2 vs Nginx for PHP application

I use both on a few projects. Apache for the main web server and nginx as a caching server only sitting in front of Apache.I use Nginx for static content and caching.
Apache remains better for production as it provides an important range of handful plugins.

web scraping and bot development using PHP

Web scraping is a computer science technique for extracting information and data from web sites. In data mining research scraping and analysing of information is discussed. Practically web scraping is necessary if you want to develop a web application where you want to show customised information from various websites.  For this you’ve to first scrap data from the sites and then apply some logic to filter the information.

Practically you can use different languages to write the program that will automatically search and collect the information. But if you’re PHP experts and want to use PHP for this kind of stuff here I would like to recommend a book web-bots/spider using PHP/Curl

What is web scraping?
I am going to expalin this with the help of an example .Suppose you want to develop an application which collects contents from various websites and thus shows a customised user
specific contents ,for that we use web scrapers .

Say for example ,Suppose you want to develop a web application where you want to show classified information about current news from different newspaper. So that people who are interested on a particular news can get all the newspaper link in your classified category.

How it works ?
The application will retrieve all the news from different websites, then classify and categorised information based on interest, like politics, sports, entertainment etc. The above book and the library(Library ) will help you to make this kind of application.
web-crawlwring

We can use this for any kind of application like Android and mobile applications.On that case your scraping script should be installed in a server where it automatically will collect and classify information and in the smart phone application you just retrieved the data from your server.