OnSwipe redirect code

Saturday, February 7, 2015

AJAX for beginners - what it is and how it evolved

Today's websites or web pages are very complex structures involving several components. It can be daunting for a beginner to understand it all at once. To ease the process let's break things down and see how the various pieces fit together.

At the core of web pages is the HTML. It is the language that browsers understand and it is what web developers use to showcase rich content. Without HTML all our web pages would have been simple text files. Now I assume that you are aware of HTML and how it is used in web pages. Another critical piece that works closely with HTML is CSS, but we can skip that one for now.

Plain HTML web pages are pretty dull. They are not really interactive. What they mainly have apart from text are :
  1. Some formatting information which tells the browser how to show the contents.
  2. Some media content like images or videos.
  3. A bunch of hyperlinks which lead to other similar web pages.
  4. In slightly evolved pages there would be forms which allowed a user to send data back to the servers.

It is the third one which helped form this "web" of pages which we call the Internet. How do these links work? Pretty simple actually. Each link typically points to a web page on a server. Whenever a user clicked on a link, the browser would fetch that page, remove the existing page and show the newly downloaded one. Some times the link could point back to the same page in which case clicking on it would just "refresh" the existing page. We all know about the refresh/reload button in the browsers don't we?

There are a few things to note here.
  1. These basic web pages were dull in the sense that they were not interactive. They did not respond to user actions except for clicks on links, in which case the existing web page goes away and a fresh page is loaded.
  2. When a link was clicked the browser would decide to send a request to the server. The web page itself did not have much control on when the request should be made.
  3. When the browser was making a request in response to a link click, there was pretty much nothing you could do with the web page until the request completed. Every request to the server blocked the entire page and if there were multiple requests that had to be made it had to be sequential, one after the other. A fancy technical word to describe this would be "synchronous".
  4. An obvious observation is that the data that came from server in response to request from a browser was HTML, as in the data and information on how to display that data were sent back from the server all the time.

While the fourth point appears as not such a big problem, the first three are definitely limitations.

To work around the first problem browser developers came up with JavaScript. Browser folks told web developers that they can make their web pages interactive and responsive to user actions by writing code to handle those interactions in this new language called JavaScript and including it in the web pages. Web developers could now write different pieces of code to handle different user actions. The browser provided a few functions (aka APIs) in JavaScript which the web developers could use to make changes to the web page. This API is called DOM (Document Object Model). Thus web pages became interactive and responsive.

The second and third limitations still existed however. The only way to request data from server was through links or in case of evolved pages through forms. Also the web page was one single unit. Whenever something had to change the whole page had to be reloaded. Imagine something like that with your gmail page. How would it be if the page refreshed every time you received a chat message or worse every time one of your contacts logged in or out of chat? That would be disastrous. The fate would be similar of something like a sports website in which the small corner showing the score needs to be updated a lot more frequently than the rest.

To solve this some people thought of iframes as a solution. Iframe is a HTML tag inside which you can put another HTML page. Now with this arrangement the inner page could be reloaded without affecting the outer page. So the sports website could now put the score part in an iframe and refresh it without affecting the rest of the page. Similarly gmail could break up its page into multiple iframes (It actually does this in reality, but it also does a lot more than this).

With iframes the "synchronous" problem was somewhat solved. Multiple requests could now be made in parallel from a single web page. However the method of making the request largely remained the same, using links or forms. It was either not seamless or almost impossible to make requests in any other manner, like say whenever user typed something (like Google search). Now let's see what all capabilities we have on top of the basic web pages.
  1. Web pages can respond to user actions. Some code can be executed on various user actions made on different parts of the page.
  2. This code can modify the web page using DOM.
  3. Browsers are now capable of making multiple requests in parallel without blocking the web page.

The browser folks now combined these abilities thereby allowing web developers to make requests to servers in response to user actions using JavaScript. These requests could be made in parallel without blocking the page i.e. Asynchronously. Now when the response came back the same JavaScript code could add the HTML received in the response to the existing page using DOM.

The developers went a step ahead and said "why receive HTML every time? In most cases the formatting or presentation information is already there in the web page. What is needed is only new data or content (For example, in the sports website example, all that is needed is the updated score. Information on how to show that score is already present in the web page). So instead of fetching HTML every time let's just fetch data to make things much faster and efficient". And back then the most popular way of representing data in a structured manner was XML. So developers started receiving data in XML format from the server and using that to update the web page. XML was so popular that browsers added support for automatically parsing XML data received in response to these requests.

This approach of making requests and updating parts of web pages became very popular and was much widely used. Since this was predominantly used to make requests over HTTP (The protocol of the web) to fetch XML data this technology was called "XMLHttpRequest" (Internet explorer called it something different, it probably still does).

What all does this XHR offer?
  1. Making requests "Asynchronously"
  2. Making requests from "JavaScript"
  3. Making requests for "XML"

Put together it forms "AJAX - Asynchronous JavaScript and XML".

Notes :
  1. Several things have been simplified here as this is aimed at people new to web development and Ajax in particular. This should nevertheless give a good starting point.
  2. References to XML should be read as " something other than HTML". Nowadays XML has been largely replaced by JSON because of several advantages.
  3. XMLHttpRequest does not necessarily mean making asynchronous request. It supports making synchronous requests also. But I don't know if there is a good reason to do it. Making a sync xhr will block the entire page until that request is complete. Really don't do that.

Friday, December 12, 2014

As citizens, we are the system

This post is in response a comment on quora ( http://www.quora.com/What-are-the-biggest-misconceptions-that-Indians-have-about-India/answer/Prashant-Bhattacharji/comment/2765540) and a couple more comments from the same user in that same thread.

===================================================

Dear Abhijeet Choudhury,

A lot of what you have said here is true, about the politicians, thugs and the plight of poor people in India. However I think there are some misconceptions. First of all here is my inference about who you are based on your comment and quora profile (Ignore if my inference is wrong ) :

1) You are an IT engineer (mostly at TCS) from a middle class or upper middle class family who has been in an urban place for most part of your life, there by lacking touch with the ground reality, especially what happens in rural and semi-urban places.
2) You probably had been to USA for work and were impressed by the much efficient system and people there
3) You probably follow Hollywood and US news/comedy etc and have a very positive view of USA

Now the misconceptions

1) USA is definitely advanced than India in several aspects. But remember that
 - They got independence centuries before we did. Their situation was also pretty bad even till the first half of 20th century. The poor people like the mine labourers, construction workers suffered very badly. (Read these : Truck system   Sixteen Tons ) It was mainly during and after the second world war that USA super charged and surged ahead, primarily by accepting people from across the world and creating an ecosystem where innovation thrived. (That is something we definitely have to learn)
 - their population and population density was and is much lesser than that of India
 - they sort of (not entirely) had a clean slate to start with while we had a much older history/legacy to deal with. The older it is the more time it takes to change.

None of these are reasons for us to be complacent with the current state of affairs. Singapore is a perfect example of how development can be achieved in short time also. I am merely saying that a comparison of current India and current USA is not fair. We are definitely progressing. May not be at rate which is impressive to you, may not be at the level the west is, nevertheless we are moving forward and there is ample proof of this. Just look around you and compare the 90s with the current. Has there been no positive change at all? After 67 years of independence we are definitely ahead of where USA was after 67 years of their independence. This is of course because we have leapfrogged in various fields. And that is precisely why we can catch up with the developed world. Let us be positive and give ourselves some more time. Because of our huge mass and very old history, the inertia is very high. Change will be slow but it will happen. It already has started (and no this is not a reference to Modi coming to power).

2) You "almost all common people in USA have a lavish life" - Who are these common people you are referring to? The middle class and upper middle? Your USA counterparts? Fact check : These people are not "almost all" by any statistic. It is true that the urban middle class people are having a decent life in the USA. But so is the urban middle class in India. We sure have more friction when we deal with certain archaic govt institutions but on a day-to-day basis the urban middle class has a fairly smooth life (except may be traffic woes..!). The poor and jobless, even in USA, have a lot of problems. The number of homeless people is increasing like anything. The wealth distribution is very skewed there too. Well qualified people are spending nights in buses because they can afford an overnight bus journey but cannot afford to rent a home. (Read : The dark side of Silicon Valley - Telegraph). I also belong to this same urban middle class category and my life is pretty smooth. May not be lavish but definitely not struggling. So is the case with almost all the people in my circles. You cannot compare the picture of a poor farmer or a construction worker in India with the picture of an IT guy in USA. The blue collar workers suffer in USA too (Read : 'Being homeless is better than working for Amazon')

3) You talk about loan to poor people. Here are some things you should know :
 - Govt provides "interest free" agriculture loans to farmers. This facility is being used by many farmers (albeit being misused also).
 - Co-operative societies provide loan to the poor at 3% interest rate (which is less than the interest your bank gives on your SB account)
 - Micro finance institutions exist through out the country (and several other neighbouring countries) which are helping workers and entrepreneurs at the rural, grass-root levels. These are people who sell fruits and vegetables on hand pulled carts, people who put up small mobile recharge shops, etc.
 - Govt runs the rural employment guarantee scheme which is analogous to the "Unemployment compensation" in USA. Again, this is also being misused but this has indeed prevented a lot of migration of poor people from rural to urban places there by reducing the "slums" that we see in all cities.

There are other several steps being taken by the govt to help uplift the poor. Sure the execution is not the best and efficient. There are a lot of leaks. But it is not summing up to a zero. There is a positive effect and we are progressing. It can definitely be a lot better, but that won't happen with negative attitude. We have to keep a positive outlook and be ready to get our hands dirty. Otherwise we will just be like those people sitting in a comfort chair in front of tv watching Sachin get out and then say "Arre square cut maarna tha yaar..!".

4) In your another comment you talk about traffic violations. You say people do it because they see politicians and other big people doing it and also because police extort money from people. I fail to recognize the correlation. If a politician is breaking traffic rules, what is stopping you from following the same? I see so many people jump signals while I always make a point to stop when it is a red. People behind me honk like crazy, some switch lanes and point fingers at me when they overtake me and jump the signal. I still don't budge. It is not just me. I have seen several other people do the same. If you are genuinely interested in creating a lawful ecosystem, then start following the rules. You cannot say that I won't do it until every one else starts doing it. That mentality won't get us anywhere. As Gandhiji said "Be the change that you wish to see in the world".

That brings me to my final and most important point : In all your posts you are just pointing fingers at others : other people, the system, the politicians, the police and so on. You compare all of these in India and USA. You declare that all of this is better in USA. But you fail to recognize what contributed to making all of this better in USA. Did it magically happen? Of course not. The people of the country toiled hard to reach there. People like you and me.

There was a time in USA when child labourers in coal mines was a common place. There was a time when farmers in USA paid lawyers with grains and nuts because they did not have money. There was a time in USA when racial discrimination was so strong that the African-American folks were as afraid of just talking as were the shudras in India when untouchability was at its height. The USA too had a lot of shortcomings. But they worked to overcome all of that and fullfil their dreams, albeit in their own ways. There was several altercations and fights. People had to fight for their rights, their safety, for development. They still do. (Remember "Occupy Wallstreet", Ferguson shooting?). It only through such fights that we weed out the bad. Not by complaining.

You might say "What could a regular corporate employee like me who works 9 to 6 for 5 days a week do in this matter?". The simple answer is that there is not easy way out. If we want to bring about a change we should be ready to face hardships. To start with pledge to abide by rules everywhere you interface with government. Getting your driving license? Do not go through an agent or pay any bribe to the RTO officer. Purchased a piece of land? Do not under-quote the value to save tax and pay a bribe to the sub-registrar. Is there a pot hole in the road in your locality? Go notify that to your ward corporator. If it doesn't happen in one complaint, do it many times. You sure will be spending quite a bit of time, at least initially. Please don't think of it as a waste of time. That would just be a short sighted view. Eventually they will give in and do the work. There are a lot of things that each and every one of us can do as citizens to improve the situation. Complaining is definitely not one of them. And pessimistic attitude definitely doesn't help one bit.

Not until too long ago the Americans looked up to Europe like you look up to the USA, picturing Europe to be that much more developed and sophisticated place. That in no way reduced their national pride. Everything they did, everything they said was about USA and people of USA. Majority of the people aiming to be the best cared about being the best in USA. They did not look for a stamp of approval from some other country. Their growth and development period was characterized by indigenous innovation and not by simply importing ideas and technologies from the already developed world. That is what we should be doing too. Be proud about our nation while accepting our shortcomings and working towards overcoming them.

All said and done, please do note that India is not at all a failed democracy. Compared to a host of oppressive regimes around the world we have freedom of speech to a vast degree. Except for a few isolated incidents like the one you mention (two mumbai girls being arrested) we have a lot of freedom of speech and it is improving. The 66A of IT Act (under which the girls were arrested) has been challenged in the court and the court has already opined that the legislation is very vague and ripe for misuse. The hearing is still on and soon we will see the legislation being revamped to be meaningful, if not scrapped altogether (Read : Sec 66A of IT act lacks guidelines, arrests made over social media posts prone to abuse: SC).

Things will change. They will improve. They are just waiting for each of one of us to do our part. Let's not shy away from confronting the problems and work on solving them instead of just complaining.