Archive for March, 2006

Why Ajax is conceptually better

Ajax has been around for quite some time now (it is a bit more than a year ago that Jesse James Garret coined the term) and it has quite some lovers, and just as many haters. First of all let’s distinguish between two different things that are often confused when we talk about Ajax. What many think to be Ajax are relly two independant things:
  • Asynchronous Communication: allows fetching and sending data asynchronously, in the background.
  • DOM-Manipulation: which allows all those fancy flash-like effects on webpages, such as zooming images, floating windows, but also really basic stuff, like swapping text in boxes.
If we want to split hairs there is a third thing that makes up those Web 2.0 applications: a logic layer, which takes care of the execution of the applicationMany of the haters argue that by using these techniques the programmers make nice applications but make things more difficult, adding complexity to the client, which before Web 2.0 was a viewer, creating portability issues and last but not least excluding all those visitors that do not use a supported browser. But let’s go a bit deeper into the things involved, when building a web service. First of all we’re able to identify three different parts:
  1. The Server side application: this may be as complex as you want but for our purpose we’ll just assume that it gets some input from the user, processes it, updates its internal state and returns some data to the user.
  2. The data: the input and output data of our service.
  3. The User Interface: this is where the user gets to see the data in all its beauty (or uglyness as it often happens).
Now in the “old” way of doing things, let’s just call it Web1, parts 2 and 3 where clustered together. With the current development in the Web 2.0 we tend to divide the layers 2 and 3 again by completely dividing the layout from the data. The idea is to download a relatively small engine that takes care of what happens on the page, and fetches asynchronously the data it needs from the server, often using data encapsulation methods such as JSON or XML. This is actually one step forward, but in the meantime a return to the roots of the Web: we now serve “pure data” or content, without all the formatting clutter that scrambled our data back in Web 1. The reduction of redundancy is a nice side effect, not having to download the layout on every refresh. The fact that we serve pure data using standard formats and standard protocols allows us to create non-human interaction between a web service and a client, the data is available for further elaboration and is independant of its actual representation in the User Interface. And here we are, welcome to the Semantic Web.
Humans are capable of using the Web to, say, find the Swedish word for “car”, renew a library book, or find the cheapest DVD and buy it. But if you asked a computer to do the same thing, it wouldn’t know where to start. That is because web pages are designed to be read by people, not machines. The Semantic Web is a project aimed to make web pages understandable by computers, so that they can search websites and perform actions in a standardized way. The potential benefits are that computers can harness the enormous network of information and services on the Web. Your computer could, for example, automatically find the nearest manicurist to where you live and book an appointment for you that fits in with your schedule. A lot of the things that could be done with the Semantic Web could also be done without it, and indeed already are done in some cases. But the Semantic Web provides a standard which makes such services far easier to implement. [From Wikipedia]
The potential of the Semantic Web is just unimaginably huge, to say it with The Hitchhikers Guide to the Galaxy’s words: it’s big. You just won’t believe how vastly hugely mindboggingly big it is. And there are already some examples: Remarkably we can find almost every big player of the Web 2.0 movement in the list, because it many services already rely for their internal workings on XML or JSON, so it becomes really easy to access them, even without the original “Web Frontend”.
  • Share/Bookmark

Site design using Prototype

The internet is full of tutorials explaining those little tricks about AJAX, how to handle XHRequests and all that low level stuff, but nobody tells you how to design the entire application, nobody gives you the overview on how all these things should work together. What good is knowing all those fragments if the developer is unable to put them together to a real use? We have libraries to abstract from the Browser dependant things like actually doing XMLHTTPRequests, and we should concentrate on higher level design to give our clients (or visitors) good and usefull applications.

General web site design have evolved over the years so knowing design decisions that modern designers make can be important web page design knowledge; when creating a site it would probably be a mistake to let web design become anything other thanhigh priority.

This is why in this tutorial I’ll try to give you an idea on how the actual design works. I’ll be heavily relying on my last tutorial for the layout of the final application, but we’ll go much more into detail about the decisions we take. Some parts may be applicable to non-Web-2.0 Web design and some even come in handy when designing completely non-web related applications

User Requirements

This step is as important as the coding itself, if I’d start right with the code without first clarifying where I want to go, I’d be wasting time! So there are three important questions that I have to answer:

  1. What? What do I (or my boss) want the application to be, what functionality is a must, what is an optional, what are “nice to have” features?
  2. Why? Does what I have found that I want to implement fit into the context or would it fit better if I’d change it a bit?
  3. When? Do I have to implement everything right now, or may I add some new features one after another, possibly by releasing it before everything is implemented? But much more important: In what order will I have to implement the functionality?

There is not really a point that is more important than the others, take your time documenting and analysing the problems that you expect and fix some priorities, it will greatly speed up the implementation later.

HP0-505 exam provides perfect knowledge about all kinds of HP technology fundamentals. HP0-678 exam has another name to be known as Implementing HP Enterprise Virtual Array Solutions. NS0-111, latest Network Appliance Storage Associate Exam certification is very advantageous for the data and networking administrators to get more knowledge and expertise. RCDD, Registered Communications Distribution Designer) certification is one of the standard certification exams. The main objective of S10-200 exams is to develop technical skills and knowledge how to manage the storage infrastructure effectively. CISA is also known as Certified Information Systems Auditor whose value and standard is well-acknowledged globally.

What & Why

We plan to make a community site so the obvious things for a community is that it has to be attractive, easy to use and most important “feel alive”, nobody really wants to be part of a dead community where some casual visitor posts a new message to the forum every few days. To archieve the live feeling we use some little tricks like the regular updates of the list of users that are currently online. Remember that a community is not made up by two or three moderators posting some news from time to time, and filling the Forum with Junk messages nobody cares about.

When

It is important to understand that we’re not delivering a half finished product, it is fully functional, we plan to add more features in a second (and maybe third) step
  1. Release
    1. Main engine: the basic framework which will allow us to load and execute single modules at a later time.
    2. Display some static content.
    3. News System
    4. User Registration and simple profiles.
  2. Release
    1. Advanced profiles
    2. Private messaging
    3. more to come…

This list is off course fairly superficial and does not cover the details of the implementation, it is an abstract planning step, that helps us get a general overview

Under the hood

As mentioned before we don’t want to reinvent the wheel for the millionth time again so we’ll use some libraries:

  1. Prototype: probably the most flexible and intuitive AJAX Library on the internet, really lightweight and easy to use.
  2. Behaviour: helps us to keep our pages downgradable for browser without JavaScript support (important also for indexing by search engine).
  3. script.aculo.us: what would a Web-2.0 application be without nice effects?

This is what we’ll use for the client side. For the server side we’ll use PHP which is nice enough for our purpose, and is a reasonable tradeoff between control, speed and abstractness. For client-server communication we’ll use JSON which allows us to directly encapsulate the data into a JavaScript object, thus making the interpretation of the data much easier, in this tutorial we’ll use the JSON-PHP Library.

The Behaviour Library allows us to bind functionality right to the DOM-Elements without having to add onclick, onload, on[whatever] handlers right in the HTML, it allows us to define some CSS Classes, associate them with certain actions and then the library will take care of the rest for us, fully automagically.

Another main problem with AJAX pages are the “one-page-application” (applications that run entirely on one page) that have some problems with Bookmarking and the famous back button, I decided that the community page will be implemented as a one page application because it makes the whole process much easier, everything runs in one context and we don’t have to care aabout the rest. But we also want to make our pages bookmarkable so we use the hash ending of the URL to identify which page we are currently on.

Basic setup

Ok, so it’s time for us to gather all our libraries and put them into our Project Folder. Our project directory structure looks like this:

The Javascript folder will contain our three libraries (prototype, behaviour and script.aculo.us), the lib directory will contain all our PHP libraries (just the JSON-PHP for now) and static will contain our non dynamic content, such as some welcome textes and help pages.

Implementation

Now we’ll finally getting to see the actual code, it is important that we go one step after another, starting from the basic page layout and then building the functionality on top of that, it will keep us motivated throughout the process to implement a little thing and then test it to see if it works as we’d expected. The base Page is just a Page with the basic content, possibly a welcome message that tells where the user landed and what this is all about. We’ll use a pretty standard Layout for our application, the only requirement for AJAX is that we assign some ID’s to the main parts so that we can easily reference them later. Now we can insert links as we would normally have done, browsers without JavaScript will see a classic page that is completely served by our PHP-Scripts on the server side. For example the first thing we’ll do is display some static, non generated content, like the Welcome page normal links would look like this:

<a href="BasePage.php?Page/About">About</a>

The PHP page would take the query string Page/About and would then serve the new page. Now we want to add the AJAX Magic, and this is magic indeed: we’ll use behaviour to catch the request before it is being sent and use AJAX methods from there on to download only the updated regions of the page. First we have to add a Class to all our internal links:

<a href="BasePage.php?Page/About" class="link">About</a>
this allows us to distinguish the links we want to catch from the links to outside resources, for which AJAX is not applicable anyway.
 
var rules = { 
  'a.link' : function(el){
    // We don't want to have garbled links...
    if(el.onclick)
      return;
    Engine.log("Applying rule to " + el.toString());
    var target = el.href.substring(el.href.indexOf('?') + 1);
    el.href = "#"+target;
    el.onclick = function(event){
      var targ;
      if (!event) var event = window.event;
      if (event.target) targ = event.target;
      else if (event.srcElement) targ = event.srcElement;
      if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
      Engine.log("Clicked on internal Link " + targ.toString());
      Engine.loadPage(targ.href);
      return false;
    }
  }
};
Behaviour.register(rules);

By doing this we have defined how the user interacts with the AJAX Engine, every element that fires up a certain event will have a class, and behaviour will then take care of the hooking up. Off course the loadPage example isn’t a really complex one but it’s the most common interaction on our page, and it shows the important parts.

See what’s going on

To test and see if all is going as we designed it we will have some way to get feedback from what we do on our Page. Using the alert() function is trivial, too trivial for us and it is really annoying too. Better would be some sort of textfield where our debugging messages are displayed without disturbing the site interaction (alert() opens a popup and focuses it…). We’ll use a popup that can be closed without interupting the application and it won’t try to get the focus for every message.

debug: true,
console: null,
log: function(message){
  if(!Engine.debug)
    return;
  if(!Engine.console){
    // Log window does not exist, try to open it.
    consoleWnd = window.open('','Debug Information','width=350,height=250,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1');             consoleWnd.document.writeln('<html><head><title>Console</title></head><body bgcolor=white></body></html>);
    Engine.console = consoleWnd.document.body;
  }
  Engine.console.innerHTML += (message + "<br />\n");
}
 

This makes it really easy to print debugging information on the running application: just use Engine.log(“Your message here…”); and Engine will take care of the rest. This is exactly what we need: the debugging information when needed but not always. This way of displaying debugging information is really simple, for more advanced features give Log4JS a try.

Modules design

Most AJAX applications in use right now load everything on startup, images, javascript and content. While this is surely an easy way to do things it’s not the best, because it takes a long time (sometimes too long) for the application to be ready, and much of the stuff that is loaded is never used, or used pretty late during execution. For exactly this reason we’ll divide the application into modules. Modules are independent parts of the application, like in our case these would be:

  • Page: a simple module which will be implemented in this part of the tutorial. It will load an HTML file from the server and display it in the content area.
  • Gallery: a gallery made of user pictures, the goal would also be to use Flickr as a backend for the pictures (e.g. take a group pool and display it, display pictures of a user, display pictures with a certain tag, …)
  • News: simply an extension to the Page module this will allow administrators and moderators to post news. News are passed to the Javascript engine using XML. Its main purpose is to demonstrate how to use XML-Files.
  • Messages: A module for sending and receiving private messages.
  • Forum: the most complex module we’ll see during this tutorial.

First of all we’ll see how to load modules and register them into a module handler (the Engine) and then we’ll move on to actually design the modules.

Module handling

Basically the Engine does specify an API that the modules then may use to access the various parts of the Page, thus we create an abstraction layer between the actual interface and the application. Besides providing an easy to use interface to the Page, the Engine also takes care of loading and managing modules. First of all we’ll have know which modules have already been loaded and if they are not yet loaded we’ll have to load them upon request:

 
  modules: array(),
   registerModule: function(moduleName){
     ...
   },
   loadModule: function(moduleName, callback){
     ...
   }
notice that we may provide a callback function so that will be executed as soon as the module is loaded. This is because we cannot use the following:
 
loadModule("myModule");
  modules["myModule"].execute();

because loadModule contains an asynchronous call which will immediately return. If we’re unlucky (and most likely we are) the second line is executed before the module has been downloaded and registered, thus generating some really nasty errors. The callback allows us to specify some actions that are to be executed when the module has been definitely loaded.

Modules can range from really easy ones, as the Page-Module is, to really complex things such as message boards, news system, live activity logs, to fully fledged Instant messaging application, there is absolutely no limit to the complexity. The only thing that we require is that the modules adhere to a certain format:

 
var moduleName = {
   init: function(){
     // Initialize the module
     // Register the module Engine:
     Engine.registerModule(moduleName);
   },
   // ...
};
moduleName.init();

The last line is used to initialize the module. Since the module code will be eval()’d it must take care of registering itself to the module handler, the Engine.

The Page Module

The Page module is our first module. On execute it will get the static page via XHRequest from the Web server and display it in the content area. Being the simplest module it consists only of one function and one anonymous function, which will take care of loading the content into the content area. Don’t forget to re-apply the Bahaviours to the loaded code otherwise you might get some unexpected results.

So here it goes, without further ado the code of the Page-Module, by now you should be able to understand what it does.

 
var Page = {
    init: function(){
        Engine.log("Page module loaded.");
        Engine.modules['Page'] = Page;
    },
    execute: function(url){
        Engine.setStatus("Loading content...");
        var myAjax = new Ajax.Request("static" + url + ".html",{
            method: 'get', 
//            parameters: "?nocache=" + new Date(), 
            onFailure: function(){
                Engine.showError("Could not load content.");
                Engine.log("Could not load page " + url);
            },
            onSuccess: function(req){
                Engine.log("Loaded page " + url);
                Engine.setContent(req.responseText);
                Engine.hideStatus();
            }
        });
    }
};
Page.init();

The rest of the Engine

The engine still isn’t done by now, some functions from above are still missing but it is easy to guess what they do, we simply need some helper functions such as setContent, setStatus, showError and alike, so here goes the rest of the code:

 
      setStatus: function(message){
        if($('status') == null){
            var body = document.getElementsByTagName("body")[0];
            var div = document.createElement("div");
            div.id = 'status';
            body.appendChild(div);
        }
        var node = $('status');
        node.innerHTML = message;
        new Effect.Appear(node);
    },
 
    hideStatus: function(){
        new Effect.Fade($('status'));
    },
 
    showError: function(message){
        Engine.setStatus(message);
        Engine.setTimeout("Engine.hideStatus();",15000);
    },
 
    setContent: function(content){
        $('content').innerHTML = content;
    },

What’s next?

In the next part of this tutorial we will finally add some really new code that hasn’t yet been covered by my first tutorial, namely we will implement a signup form and some simple user management. For now take a look at the working copy of the application here, or download a snapshot of the code here.

Other resources

Tools

  • FireFox Venkman: the javascript debugger of FireFox (does not work with 1.5.0.1, but the guys over at GetAhead got a fix).
  • FireBug Extension: FireBug is a new tool that aids with debugging Javascript, DHTML, and Ajax. It is like a combination of the Javascript Console, DOM Inspector, and a command line Javascript interpreter.
  • Eclipse AJAX Toolkit Framework:AJAX Toolkit Framework (ATF) provides extensible tools for building IDEs for the many different AJAX (asynchronous JavaScript and XML) run-time environments (such as Dojo, Zimbra, etc.). This technology also contains features for developing, debugging, and testing AJAX applications. The framework provides enhanced JavaScript editing features such as edit-time syntax checking; an embedded Mozilla Web browser; an embedded DOM browser; and an embedded JavaScript debugger.

Update: 27.03.2006

Some people, with popup blockers have been complaining that the code isn’t working. This is because the popup blocker, blocks the debugging window from being opened. To get it working in the downloaded source, just turn Debugging in the engine off (by setting debug: false) or disable the popup blocker. Sorry for that, in the next part I’ll explain a better way to do it :-)

  • Share/Bookmark

New Blog

Hey, this looks nice :-)
I’ve been pretty fed up with the old look of my Blog so I decided that it was time of a look upgrade, and while I was doing so I noticed that I was lagging behind with my WordPress version too, so an upgrade was due.
Hope you like the new look & feel of my Blog, though it still needs some minor changes to it.

  • Share/Bookmark

Aine: AJAX is not everything…

While I am a supporter of the technologies for which the abbreviation AJAX stands, I’m also convinced that AJAX is not the solution to everything. Right now every tutorial, library or comment that is even remotely AJAX-Related is getting a huge amount of attention, just because we’re in the middle of an AJAX-Hype. Yes, it is in fact a hype, because people hear of it and run to the next book store and buy a book without having the remotest idea on what AJAX is good for (the book buying people are the ones I prefer over those Newsgroup posters that post hundreds of posts like “I’m new to AJAX”, “What is AJAX good for”, …).
AJAX is a technology like thousands of others, no more no less, with all its strengths and weaknesses.

I came across a post in the JavaLobby Forum:
After attending an interesting AJAX seminar in NYC I did not change my mind. Initially, I was prepared to ask specific technical questions about AJAX problems, but even without my tricky questions every presenter was talking about lots and lots of issues they were facing while developing AJAX applications. This was the most honest team of presenters I’ve seen so far (btw, the team has included Jesse James Garrett, who named a set of existing technologies AJAX).

After hearing all their testimonies about the plethora of AJAX issues, during the evening I decided to ask a generic question, if the panelists believed that AJAX would be around in three years. Most of them answered that it’ll be around in three, but they were not sure about 5 or 7 years from now. Fair enough. I had an impression that all of them enjoyed the technology, understood the issues, and were willing to try to solve them… somehow.

So far I coded both AJAX applications and Java applications, and both have advantages, but let’s focus on AJAX:

Strengths

  • Easy to deliver: (nearly) every browser nowadays supports JavaScript, thus an application written in JavaScript, on top of other standards, like DOM, will probably run on most Browsers.
  • Complex is easy: AJAX is pushing the development of applications in a direction where complex workflows are wrapped in a really easy to use presentation, hiding complexity and making important things visible.
  • Software as service: for long time software was a piece of code that you bought, now it’s a service that you subscribe to. Where’s the difference? Its easy, with AJAX the code is no longer kept secret (can I hear some OpenSource Developers cheering? =D) and businesses are shifting from selling the code to offering a service. 37signals is probably the best known and most successfull company that uses this model.
  • Everybody can use it: the steps are easy open your browser (remember not to click on the blue ‘e’) and point it to a URL, that’s it. From there you are driven through the rest of the application, with nice popups, overlays explaining what you have to do, and telling you where to fill in what. It feels smooth.

Weaknesses

  • Hard to code: because JavaScript support in the browsers is not standard compliant, and every browser adds its own “enhancements” the developers have to work around to those heterogenic environments and add abstraction layers in between, slowing down the application.
  • The URL-Paradigm is lost: URL stands for Uniform Resource Locator and it basically used to locate resources, such as Documents, Audio files, Images, and alike. With AJAX this is lost because what good is it to locate the resource if what it looks like solely depends on the means to access it? AJAX applications look different and react different in each browser. Many developers now complain that AJAX takes away the bookmarkability, in my opinion this is good! A URL may solely be used to locate the application, and not the states in which the Application currently is. It’s not good to bookmark states of of an application.
  • It’s slow: yep, JavaScript, being a purely interpreted language, is really slow, no way to argue about this.
  • It’s easy to make errors: JavaScript applications may grow really complex and we don’t have necessary tools to debug and test the applications, furthermore in one environment (browser) an error might occur at a certain point while in another it runs smoothly. The testing and debugging efforts grows exponentially with the amount of code and the complexity of the application, while this is true for all languages and applications, in AJAX this is particularly dangerous, because we can’t automate these things, and tools are scarse. This might still improve over time, we’ll see.
  • Code is readable: I’m not a big supporter of the security by obfuscation method, which is secure as long as an attacker cannot access the code of the application, but being able to read the applications gives an attacker better tools to attack the application. This is also the main reason you won’t see big applications from non-OpenSource teams: no one wants the others to see your code if your going to sell it…

AJAX is surely a step into the right directions, but one must ask himself if AJAX is the way to go further on. While AJAX does very well for small applications it is not usable for larger applications. Applications such as Writely are on the very limit of the AJAX Universe, to go beyond this we have to rely on other technologies.
One that comes in mind is Java, it is nearly as widespread as JavaScript is and programmers can rely on extensive library support. Java is currently seen a bit as an overkill, slow and useless among many users, but it is a powerfull tool to create thin client applications, that is to say that you load the application from remote without having to install a program locally, and just run it off the network.
Thin clients are a new approach, first embraced by Sun Microsystems, that tends to minimize the administration effort, and maximize the user experience, almost exactly what AJAX stands for right now, don’t you think?
Nearly as distributable as AJAX, Java has several advantages, being easily portable, offering modern GUIs and being faster than JavaScript, it may become the successor of the AJAX hype.
Using Java we can easily create complex software that it intuitive to use (as the Web 2.0 phenomenon is experimenting about right now) that scale well, both while developing and once deployed, and gives the developers the power of thousands of ready to use Libraries.

AJAX is here, now, but how long will it stay?
  • Share/Bookmark

Why We Fight

A couple of days ago I was so bored I went to Apple’s Trailer site and spent the whole evening watching Trailers of both new and old Movies, when at a certain point I stumbled over the trailer of Why We Fight.

Why We Fight is a movie in the style of Michael Moore’s famous 9/11 but in my personal opinion it tries to draw a more realistic picture, somewhat less black&white, but nevertheless alarming. With lots of first person impressions and some pretty convincing theories.

Like any of these Movies, this too has not to be taken as the truth about everything, it is however instructive and may broaden your view. I really recommend all of you to watch it.

  • Share/Bookmark