Saturday, August 2, 2008

FAQ on AJAX Technology

AJAX has become an hot technology with Web2.0 but before starting AJAX Programming developers needs to know some important FAQ on AJAX.

Q) Ajax an Microsoft Technology?
A) Absolutely No, Ajax is an cross browser technology. It is browser dependent and uses JavaScript.

Q)Are Ajax applications easier to develop than traditional web applications?
A) Not necessarily. Ajax applications can involve complex JavaScript code on the client. To develop that complex code efficient and bug-free is not an simple task as it involves pure JavaScript & DHTML coding. But better development tools and frameworks are require to make the Ajax Application development much faster and easier.

Q) Do Ajax applications always deliver a better experience than traditional web applications?
A) Not necessarily. Ajax gives interaction designers more flexibility. However, the more power we have, the more caution we must use in exercising it. We must be careful to use Ajax to enhance the user experience of our applications, not degrade it.

Q) Apart from AJAX any other Technique exist for Dynamic Web Page Update?
A) IFRAMES

Q) Can AJAX access Server side script residing on different domain?
A) NO. Ajax can only access the Script residing on same domain. If you try to access the script from other domain browsers gives an error “Access Denied”

Q) Ajax can also fetch XML content from server side, if this is true how?

A) Using responseXML properties and then by using JavaScript XML DOM to access the attributes and value of XML data.

Q) Is Ajax is Browser Dependent?
A) Yes, Ajax object is part of JavaScript engine inside every browser. So if a new browser comes into market and the JavaScript engine doesn’t have XMLHTTP object then Ajax will not work.

Q) How can i check the progress of my Ajax call?
A) When making Ajax call the readystate has to reach 4 to capture the remote date. So you can insert appropriate messages until the readystate reaches 4.

Q) Do i have to wait endlessly after making Ajax Call?
A) Not always but there are some exceptional cases when the remote server is already busy and is unable to handle your request in that case the JavaScript code will wait until response is fetched from the remote server or there is browser request timed out. You can use the interval to make another call after specific interval.

Q) Do i have to explicitly use Ajax Frameworks for implementing Ajax?
A) Not necessarily, Ajax Frameworks are meant to make the life of programmers simpler. These frameworks acts as an wrapper to the Ajax Code, they provide you with the function call that will do all the Ajax Call and you don’t have to write the Basic Ajax Code.

Q) Can Ajax work with all Web Technologies?
A) Yes, Ajax is browser dependent and if the web technology you are using supports browser then Ajax will work.

Q) Can i make multiple Ajax Call?
A) Yes you can make multiple Ajax call provided you have separate Ajax Object variables created. Making another Ajax Call on the existing Ajax Object will overwrite the previous make call.

Q) Can i do Ajax Connection Pooling Techniques?
A) Yes you can do connection pooling for making Ajax call. Here you will be storing all the Ajax objects in a connection pool so that every individual object will perform their own task.

How AJAX Works

The best way to learn this topic to understand the comparison between the traditional method of calling the Web page from the server and AJAX methodology.


Traditional WebPage Call

  • User interacts with the webpage to perform its operation
  • The request is submitted to the Web Server along with the information user wants
  • Server checks for the details and might require to interact with the Database and finally returns the response to the browser
  • Browser displays the user response details in the form of refreshing the webpage

Traditional WebPage Call

* User has to wait until the page reloads
* User can perform only one action at a time

AJAX come to the rescue:
With AJAX user can fetch the data from the server without refreshing the whole page. During AJAX call following steps are followed

* User interacts with the web page to perform its operation. At this point your webpage internally through JavaScript calls a remote file and the user data is passed through GET/POST method
* The web server receives the request processes it and send the data back to the client browser
* At this point the web page is not reloaded and the browser JavaScript engine is waiting for the response from the web server.
* The web page gets the response from the server and dynamically through JavaScript displays the content fetched from the server

Advantage of using AJAX:

* Make multiple AJAX calls with different request from the same page
* Instant result
* Not to wait for the page to get reload

Ready for going deep into AJAX Programming, if yes than check out the links below.
Bookmark It

What is AJAX?

AJAX stands for Asynchronous JavaScript And XML. It got famous through Gmail etc. It is a part of Web 2.0 Techniques which allows some part or the whole document to dynamically update without refreshing the whole web page. So the time taken to get the response from the server gets reduced as the pages are getting updated in the background through AJAX. Also it gives more interaction on the web page. It is a browser based technology and is independent of any Web Server Programming language.

AJAX Acronym:
A:
‘A’ stands for Asynchronous mode. This technique allows the web page to make request in asynchronous/synchronous mode to the web server. By default in all the AJAX examples it is set to Asynchronous mode. The advantage of making it asynchronous is that is will allow the web page to make multiple calls at a same time which will work in the background and will update the page dynamically as soon the request gets over.

J:
‘J’ stands for JavaScript. AJAX extensively rely on JavaScript Engine for its functioning. So it is essential that the JavaScript should be enabled for AJAX to work.

X:
‘X’ stands for XML. AJAX uses the XMLHttpRequest Object of the JavaScript Engine. With this object it can also handle the request and response messages in XML Format apart fron traditional text format.

Examples of AJAX based websites:
www.gmail.com

Limitations of AJAX:

  • It can only access the page from the domain the original web page is coming from. i.e. demo.html residing at example.com can makes AJAX call to web page residing only at www.example.com.

Headache of AJAX Programming:

  • All the program will have to be written in JavaScript. So you must be an expert in JavaScript to write AJAX Programs.
  • AJAX program should be compatible with different browsers DOM.
  • Lot of request goes to the server. So the server requires huge bandwidth to allocate the request made.
  • Testing on different browsers.
  • Sometimes if your servers fails to respond to the AJAX call then you will have to wait endlessly until you again make the AJAX call.

Before AJAX came into Picture:

  • IFRAME were used to display data of other webpage or other domain in the same page. The page inside the IFRAME can be reload/changed dynamically using Traditional JavaScript Technique.

Ready for going deep into AJAX Programming, if yes than check out the links below.

Bookmark It

Beginning Ajax

Ajax is often mistaken for a programming language, when in reality it is more of a standard or technique used to create better, more interactive web applications. It is used to create more responsive web pages by loading certain areas of a page, instead of an entire page. In this tutorial, and the ones that follow, we will learn to work with it to build dynamic web sites.

Ajax is an acronym that stands for Asynchronous Javascript and XML. It work across a multitude of platforms and is compatible with all of the major browsers. The "asynchronous" portion of its name comes from its ability to request data from the server using the XMLHttpRequest Object and store it in the background without changing the appearance or behavior of the page.

Typically JavaScript is used to make the Ajax function calls. For browsers that do not support XMLHttpRequests, remote scripting can be used.

In this article we will learn the basics of this technique, along with its history. I will make some assumptions. I will assume that you have some knowledge of HTML or XHTML and JavaScript. An understanding of other web technologies, such as XML and CSS would also be helpful. I add CSS in that mix because CSS is also part of the standard that comprises the makeup of Ajax.

It's All in a Name

I'm sure you all know of the mythological figure Ajax? No? How about Ajax the Lesser, who was given the smack down by Poseidon and his trident? Ajax the cleaner? Ah there we go.

Asynchronous techniques can be dated back to the days of DHTML, or even earlier with the src attribute. It could be used to load JavaScript into a page, mimicking some of the AJAX type effects. Remote Scripting, introduced in 1998 by Microsoft, acted as a replacement for these techniques, using Java applets to give the client side the opportunity to communicate via JavaScript. Later on Microsoft would go on to create the XMLHttp Request with its release of Internet Explorer 5.

Then finally, in 2005 (I've skipped a little bit of history to ensure you stay awake), Google popularized Ajax techniques with its Google Suggest in an effort to rule the world. Now today, a short three years later, we are all slaves to the Lord and Master Goog. It commands and we search. All Hail the Goog!

Advantages of Using Ajax

Before we actually learn the techniques, it may help to understand why you would want to and compare the advantages and disadvantages of using Ajax. I would say for the most part that the advantages far outweigh the disadvantages, but you may disagree.

So without further ado, here are the advantages:

-Consistency

Ajax allows for a consistent view in that it does not need to reload the entire page like traditional methods. Instead it can reload specific elements, so the page does not need to flash on and off or reposition itself.

-Fluidity

Instead of loading different pages to display information, you can load the data in the same window, making your web page feel and behave more like a Flash or a desktop application.

-Usability

As stated above, you can make your web pages feel more like an application, allowing for enhanced user experiences and easier controls.

-Bandwidth Friendly

Since Ajax updates portions of the web page instead of the whole thing, and handles data in the background, it typically results in faster page loads. And honestly, even if the page does not actually load faster, it feels that way because the page does not flicker or seem to change much at all.

-Interactivity

Due to the increased speed of the pages loaded (or the illusion of increased speed), visitors to your site are more likely to interact with all of the elements on your site. If you have a rating system on your website or a choice of options, in traditional web sites the user chooses some options and has to wait for the page to reload and store the data before displaying more options. With Ajax, it all happens on the fly. I know when I visit a website and after every decision the page has to reload, I get pretty irked, pretty fast. Yet if just the page section updates, I tend to stay.

That isn't to say that as Ajax gets more popular and we get more used to it, that we won't get impatient with it as well. Consider cooking something in the microwave for five minutes. It seems like a hassle to us now, but back when they were first invented, it was a godsend.

Disadvantages of Using Ajax


Believe it or not, even Ajax has some disadvantages to it. Here are some things to consider when deciding whether or not to use it:

-Security

Ajax has been around for a few years now, but it is really still an infant in terms of technology. And like all new (and old) technologies, it is vulnerable to exploits. Of course if this were to stop you, you may as well give up on the web altogether.

-Accessibility

As we've seen before with new technologies, Ajax can sometimes be lacking when it comes to meeting certain accessibility standards.

-Search Engine Optimization

It seems these days that the Internet is only about two things, one of which I can't mention here in this article. The other of course is SEO. Since Ajax loads content after the page loads sometimes, there aren't always good ways to optimize your site so that spiders can crawl it. That isn't to say you can't, but just be aware of the issues and educate yourself.

-Weirdness

Ajax is an evolving technique and as such certain behaviors can occur that you may not expect. It can also be argued that users who are used to traditional web pages might find a page that acts more like an application more difficult to comprehend.

-Timeliness

Even though Ajax should typically speed up your web page, it ultimately is dependent upon the same hardware as other web technologies and thus is subject to the same problems as normal web pages. If a server is experiencing difficulties, it won't matter what you are programming your site with.

Conclusion

Well that is all the time we have for this article. In our next episode we will begin learning the basics of Ajax and how to work with the XMLHttp Request Object. In the meantime you may wish to brush up on your HTML and JavaScript.

Till then...

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

Monday, July 28, 2008

Tranfer all MySQL databases to another server

I have managed migrate all of my MySQL databases from one server to another. This was a one time only operation, so setting up replication wasn't an option. And restoring a backup on the new server involved more downtime than strictly necessary. I figured the most ideal way would be to dump the active MySQL directly into the new MySQL instance. And that's what I did.


Features
* This often beats setting up replication if you want a quick & dirty solution.

* Extremely fast because it injects the MySQL dump directly into the new MySQL database, hence it does not use additional diskspace or disk IO.

* InnoDB proof.

* By putting everything in one script we can instantly create snapshots with recent data. This allows you to focus on other issues that might pop up during a database migration.

* And yes, you could schedule this script in the crontab and sync to a standby server for emergency failover. But consider replication or clustering instead.

Warning
Be careful, this script will drop any existing database on the receiving side it encounters. USE AT OWN RISK! This is heavy material and you could seriously mess up your system if you don't know what you're doing.


The Script
#!/bin/bash
# where your mysql commands are
CMD_MYSQL="/usr/bin/mysql"
CMD_MYSQLDUMP="/usr/bin/mysqldump"

# login credentials of the submitting side
DB_USER_FROM="root"
DB_PASS_FROM="passxxx"
DB_HOST_FROM="source.example.com"

# login credentials of the receiving side
DB_USER_TO="root"
DB_PASS_TO="passxxx"
DB_HOST_TO="destination.example.com"

# index all databases on the local (submitting) side
echo "database sync"
DATABASES=`echo "SHOW DATABASES;" | ${CMD_MYSQL} -p${DB_PASS_FROM} -u ${DB_USER_FROM} -h ${DB_HOST_FROM}`

# loop over all the databases
for DATABASE in $DATABASES; do
# skip non-databases
if [ "${DATABASE}" != "Database" ] && [ "${DATABASE}" != "information_schema" ]; then
echo "transmitting ${DATABASE}"

# create database on remote (receiving) side if it doesn't exist
echo "CREATE DATABASE IF NOT EXISTS ${DATABASE}" | ${CMD_MYSQL} -p${DB_PASS_TO} -u ${DB_USER_TO} -h ${DB_HOST_TO}

# dump the current database and pipe it directly to the remote (receiving) side to inject it
${CMD_MYSQLDUMP} -Q -B --create-options --delayed-insert --complete-insert --quote-names --add-drop-table -p${DB_PASS_FROM} -u${DB_USER_FROM} -h${DB_HOST_FROM} ${DATABASE} | ${CMD_MYSQL} -p${DB_PASS_TO} -u ${DB_USER_TO} -h ${DB_HOST_TO} ${DATABASE}
fi
done


Save this script somewhere (I usually take the /root/bin dir for this) and Make it executable:


Configure
You need to change following variables to suit your environment:

DB_USER_FROM="root"
DB_PASS_FROM="passxxx"
DB_HOST_FROM="source.example.com"

DB_USER_TO="root"
DB_PASS_TO="passxxx"
DB_HOST_TO="destination।example.com"]

Now run the script (be sure read the warning above).