Friday, August 12, 2011

Javascript On Server - I !


           Javascript is generally used to build web-applications. One can do whole lot of Front-End Stuff and give his/her application a interactive paradigm . Another aspect, can we do Javascript programming on backend or servers . Is event driven backend possible. Yes, Node.js is answer to all these question.
          In order to execute the JavaScript you intend to run in the backend, it needs to be interpreted and, well, executed. This is what Node.js does, by making use of Google's V8 VM, the same runtime environment for JavaScript that Google Chrome uses. Node.js is pretty good runtime enviornment coupled with usefull libraries. Javascript is pretty powerfull Event driven programming language.
          The “traditional” mode of web servers has always been one of the thread-based model. You launch Apache or any other web server and it starts receiving connections. When it receives a connection, it holds that connection open until it has performed the request for the page or whatever other transaction was sent. If it make take a few microseconds to retrieve a page from disk or write results to a database, the web server is blocking on that input/output operation. (This is referred to as “blocking I/O“.) To scale this type of web server, you need to launch additional copies of the server (referred to as “thread-based” because each copy typically requires another operating system thread).
              In contrast, Node.js uses an event-driven model where the web server accepts the request, spins it off to be handled, and then goes on to service the next web request. When the original request is completed, it gets back in the processing queue and when it reaches the front of the queue the results are sent back (or whatever the next action is). This model is highly efficient and scalable because the web server is basically always accepting requests because it’s not waiting for any read or write operations. (This is referred to as “non-blocking I/O” or “event-driven I/O“.)

Example :
  • You use your web browser to make a request for “/index.html” on a Node.js web server.
  • The Node server accepts your request and calls a function to retrieve that file from disk.
  • While the Node server is waiting for the file to be retrieved, it services the next web request.
  • When the file is retrieved, there is a callback function that is inserted in the Node servers queue.
  • The Node server executes that function which in this case would render the “/index.html” page and send it back to your web browser.
     V8 is constantly pushing the boundaries in being one of the fastest dynamic language interpreters on the planet. I can't think of any other language that is being pushed for speed as aggressively as JavaScript is right now. In addition to that, node's I/O facilities are really light weight, bringing you as close to fully utilizing your system's full I/O capacities as possible. In Nutshell, Event-Driven Nature, Scalable and modular are the few traits by virtue of which Node.Js is much talked about these days.

PS: DOM is browser things and till now has got nothing to do with Node. (Some work going in this direction)



Friday, July 1, 2011

Go(d)ogle! Social War is ON!

                  With all buzz around, and after 2-3 days of twisting and turning with Google plus , i feel like coming back to it again and again. In any case i can't avoid it now. Obvious reasons ! i use google and gmail most of the  time and top black bar will keep reminding me of updates on plus. Pundits over HN ,reddit, techmeme , twitter and other forums discussing  and views are exchanged..as what's future of FB or plus, shortcomings , optimistic details , pessimistic details,etc. 

Advantage Google + :

  • Google owns browser and can do a lot with that in future
  • Data and users Google has(via gmail) is mammoth and more than that of FB
  • Simple and Clean Website/Features (Great work)
  • Mobile market - Anroid coaxed by google

Advantage Facebook:
  • Already a huge population using it.
  • Apps and Games 
  • "I don't know".
              Vic Gundotra ,  Google's social czar and man behind plus  remarked - "We think people communicate in very rich ways," ... "The online tools we have to choose from give us very rigid services." . Vic  highlighted circles featured in plus, which promises to overcome possible privacy issues faced by people on FB. Google seems to have analysed its mistakes from wave and buzz and had provided its user with limited and scope of "options" . With plus unlike wave or buzz its not pushing anything onto its user , rather provide "options"  for same .Although , this could be a curse for noob internet guys and boon specially for geeks. Google has a  Panoply of services, and vision seems to be clear - " to make one stop solution for your internet(may be Search , mail, social, videos ) with one identity and for all devices".

                       Why people in India shifted from Orkut to Facebook?? One big answer to this - "FB was simple , easy, at the same time pretty powerfull when we talk about features ". Another reason be people like change , these were atleast my reasons to switch from Orkut to FB.  We can't judge from the failure of buzz and wave that plus will lie on same side of table . Talking about it globally , Badoo , Bebo , Cyworld ,  FriendFeed , Friendster , Hi5 , ibibo  , Jaiku , Myspace , Netlog , Orkut ,  StudiVZ ,  Tagged , Tribe.net , Tuenti  , Vkontakte , Whispurr are few minniows social networking portals . The main reason of rise of FB was simply the " robustness with simplicity" . Exception of Vkontakte , who's user base was limited to a particular region ,others were no match to FB. Now,   social war in on ! Google capable of taking on FB has arrived with plus and looks promising as of now . 
             Some recent happening around  Facebook - launch of "Awesome" , FB Circles . Are these out of desperation or  was it hunched..Google plus is still in its beta , a long way to go . Facebook already established on social front, will  leave no stone upturned to maintain its monopoly over social sphere. In nutshell , a interesting "social war" ahead , hope better wins.

          [ For those who are unaware, while both sound the same, the service each one provide is very different. You see, Google+ nee Google Plus works as a hub for all the new social-network services of Big G announced like sparks hangout, etc. Meanwhile, Google +1 works like Facebook Like which you can embed on your website and when your Gmail contacts +1 a certain page, you’ll be able to see their little avatar the next time you search Google – of course – when you’re logged in with your Gmail account.
         So far, Google Plus is still on its beta stage and no one knows when will Google release this to the public. Take note that once you are logged in with Plus, you’ll be able to do most of the things you can do on google like checking your email, images, shopping, etc. But of course, its focus are those circles from your friends and how you’ll be able to contact them.
          At this point, pundits aren’t sure what will be the future of this project. In the case of Google Plus One, its still comparable to David compared to the Goliath that was Facebook Like.]





PS: One personal reason why i would prefere G+ , is that it allows me to keep my name "only" Vishal unlike FB where i have to use palindrome of my first name i don't use second name :D

Wednesday, April 20, 2011

Cross Origin Resource Sharing - CORs

              With advancement of web , Ajax is playing an important role in most of website or web application . One of common restrictions is same -origin policy within browsers due to but obvious security reasons. What we mean by [violating same origin] or cross site request  - "Cross-site HTTP requests are HTTP requests for resources from a different domain ,different port, different sceheme than the domain of the resource making the request.  For instance, a resource loaded from Domain X (http://vvishal.com) such as an HTML web page, makes a request for a resource on Domain Y (http://vvishal.me)". 
Ref : https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript


             Web Developers had been trying work around for this by various means setting up proxies , server side proxies .  At this point of time community of developer outcried a native way of doing cross domain Ajax Request . Cross Origin Resource Sharing - CORS evolved at this point of time .Internet Explorer 8+, Firefox 3.5+, Safari 4+, and Chrome supports CORS , Opera till 10.63 was not suppoting CORS.


             In Simple terms , the basic idea of CORS is to modify HTTP headers from client and server side to enable communication between two. This makes server and client know about each other and recognize the needs , Success , Fail etc. Header plays an important role the handshake between client and server and the informations needed to be exchanged.


Read More:
http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/
http://arunranga.com/examples/access-control/
https://developer.mozilla.org/En/HTTP_access_control







Sunday, December 5, 2010

Host your Website - Google app engine

      Google app engine provides environment to host your web-application . Essential steps involved in hosting a website -
    1. Buy a domain name with no hosting space to pay for.(ex- GoDaddy)
      Buying a domain is simple we can go to any website and buy a domain .
Ex- GoDaddy , here you can get discount also some coupon are available on net.
    2 .Upload and configure your web application(website here) on Google apps
      Web Hosting ! this is where google app engine comes to rescue . Follow few of below simple steps to upload/deploy your website on app engine.
  1. Create app engine account, install python and download app engine sdk.  
  2. On your machine create a folder  eg. MyWebsite (c:\MyWebsite).
  3. Make a folder named static inside that eg. (C:\MyWebsite\static)
  4. keep all your files to be uploaded inside "Static".
  5. In "MyWebsite" we need to add a file app.yaml
Code to be written inside app.yaml
application: [Name of application , which you create on google app engine eg. Website]
version: 1
runtime: python
api_version: 1
handlers:
- url: (.*)/
  static_files: static\1/index.html
  upload: static/index.html
- url: /
  static_dir: static

Folder hire achy looks like this in genral case
- MyWebsite/
   -Static/
       -index.html
       -css/
       -JS/
       -images/
       -favicon.ico
   -app.yaml

      Once done with this on client side(i.e Your machine) , log in to google app engine and create simple application (eg Website). Now, we need to deploy our code on app engine . Open command line and type in simple command --appcfg.py --email=example@gmail.com update c:\MyWebsite/
Done ! now your website is hosted on google app engine and can be accessesed directly at 
url- http://[Your-Application name eg. Website].appspot.com/
    3. Management of Domain, Domain Mapping (as google app dosen't allow you to map to naked domain eg example.com, you may map to sub-domain though eg- test.example.com )
      Application is up and running but generally we want it to map to our domain eg. example.com .Since app engine dosen't allows that so we have to follow few workarounds to make it work as we wish. 

Domain Setting
Domain Verification-
   - Go to Google App engine Dashboard-> Your Application[eg. Website here]
   - Administration->Application Settings-> Add Domain 
   - Enter your Domain Name and follow registration process and Get domain registered.
   - Go To Google Apps for your domain name 
   - Login to dashboard
-  Go to Domain setting and check for domain registration etc. 





URL Mapping
- Browse to Service Setting, go to your app eg-Website here 
- Add new URL for your google app
- Here we can't map to naked domain like example.com
So , as matter of practice we map it to www subdomain eg- http://[www].example.com
(as www.example.com is also a subdomain of example.com)


 
After this our website will be available at http://www.example.com and [your application name eg. website].appspot.com . Content available at subdomain http://www.example.com will not be available at naked domain http://example.com .

To make content available at http://example.com we need to redirect it to http://www.example.com. For this , we can use meta refresh but this is not a good practice . One of alternate solution is using header redirection. Generally, at domain provider we are given a small space of web hosting ,there we have to put a index.php file with following code inside it
header ("Location: http://www.example.com");
This solves our problem and our website gets redirected properly.So now the app/website is available at - 
- http://[Your application name].appspot.com
- http://www.example.com
- http://example.com
This is a simple way of keeping your domain name and web hosting a separate . Incured with flexibilities of Google App Engine, your website will be 99.9% up as claimed by Google.

Cheers!