A key Role in the Initial Growth of the World Wide Web

Apache Web Server Journal

Subscribe to Apache Web Server Journal: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get Apache Web Server Journal: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Apache Web Server Authors: Liz McMillan, Elizabeth White, Pat Romanski, Janakiram MSV, Gil Allouche

Related Topics: RIA Developer's Journal, Apache Web Server Journal

RIA & Ajax: Article

Do You Speak Web?

A look at a young, new programming language - haXe

The recent years of Web development have been marked by several important events. In the Flash world, we have seen a constant evolution of the technology. From a developer's point of view, Flash started to be really usable with the introduction of ActionScript 1.0 in Flash 5 and the improvements made in Flash 6. It turned out that Flash was no longer only an animation tool for designers but also a framework for developers. This led to many Flash projects that would not have been possible before, ranging from Web games to Rich Internet Applications.

The usage of Flash by developers has been greatly increased over the years. With the introduction of ActionScript 2, people familiar with existing object-oriented language such as Java could start developing Flash content more easily. With the new graphical effects of Flash 8, game visuals could be greatly improved. And even more recently with Flex2 components, one can now develop a working RIA in a matter of hours.

However, with all these additions, a lot of early Flash adopters might have been left behind, lacking time to learn all these new technologies and at the same time, feeling pressured by all the new exciting things that were becoming possible. This development will be ongoing, and actually, for someone who wants to develop a modern Web site, there is even more to learn.

The second major change was the progressive replacement of Internet Explorer 5-generation by IE6, which integrated better JavaScript and CSS support. Together with the emergence of standard-compliant browsers such as Firefox, Safari and Opera, this led to the next hot thing - AJAX.

In the Web world, where not everybody needs the very complex interactions that Flash can provide, AJAX works well with Flash to provide an enhanced user experience. For the Flash developer who wants to use some AJAX features in his existing Web site, the technologies are complementary.

The Server-Side of Things
In order to develop a Web site, you also need to know some server-side technology. Since the unique Perl CGI choice from a few years ago, a lot of server-side languages are now available, including Java, PHP, ASP, Python, and more recently, Ruby-on-Rails. All these languages are clearing the gap by providing indirect and filtered access to the server filesystem and database.

To master server-side technologies, you need to learn a good number of frameworks, sometimes concurrent, rarely standardized. You need also to understand how to communicate efficiently with the client-side, either with Flash (by using Remoting) or JavaScript (using XML or JSON).

Whether for the single developer or the team that has mastered all these different technologies, a set of new problems arises from using them all together in one Web site. Communications between the different developers of a team are not always easy since they use different technologies. Sending messages between the different languages is not always transparent, due to their technical differences, so an XML intermediate representation is often needed, which adds duplicate work for both client and server. And since different languages are preventing code reusability, developers need to write the same code twice, and keep everything synchronized. A typical example is a complex form validation that needs to be performed on both server- and client-side. That's where haXe comes in.

What Is haXe?
haXe is a new technology that simplifies Web development by providing a common foundation programming language for both client- and server-side technologies. You can use haXe to program Flash, JavaScript/AJAX, and the server-side of your Web site. You can even use haXe to develop desktop applications.

By reducing the number of pieces in the Web puzzle, haXe simplifies the overall Web site development process. Because the developers in a team are all using the same technology, they can understand each other and share ideas more efficiently. Since this is the same language everywhere, messages can be sent transparently between the different layers of the Web site, enabling full Flash/JS/Server automated communications. And with the same syntax for all platforms, some code can be reused on both the server- and client-side, without having to rewrite it or keep everything synchronized.

For the Flash developer, haXe is a command-line compiler that can be used to compile a set of haXe classes into a SWF file. Flash Players from 6 to 9 are supported, giving the haXe developer a lot of freedom of choice (ActionScript3, by comparison, only supports Flash Player 9). For people already familiar with the MTASC ActionScript2 compiler, haXe is its successor.

To develop JavaScript/AJAX code, the haXe compiler will output a single .js file, including all the compiled classes. Programming haXe instead of JavaScript is increasing productivity: haXe is strongly typed so most errors are caught early in the compilation phase. haXe is class-based so the JS developer can abstract some visual components into classes and reuse them later in several projects. Since only needed classes are compiled to the .js file, it's easier to manage than a lot of small .js includes. More importantly, haXe-generated JavaScript code is cross-browser, and haXe includes some standard libraries - such as XML manipulation and XmlHttpRequest - needed to write cross-browser AJAX code.

Finally, haXe classes can be compiled to run on the Neko Virtual Machine - a small embeddable VM with a low footprint and very good performance. There is a mod_neko for the Apache Web Server (similar to mod_perl or mod_php), so you can use haXe to connect to a database and generate dynamic Web pages. Standardized API are available to wrap database tables using classes (SPOD), generating HTML from a Template system, or simply accessing the local filesystem and network.

This article will focus its examples on both JavaScript and Flash technologies, which might be more familiar to the reader.

haXe API
haXe is sometimes wrongly accused of trying to do too many things at one time, with the belief that it will somehow be the less common denominator between all these platforms. But that's not how haXe works.

haXe is a programming language with which you can define classes and write code. It has a standard library that has been unit-tested to work the same on all platforms. This standard library includes API such as XML, String, Date, Array, and Reflection.

Each platform has its own specific API, such as Browser DOM for JS, MovieClip and Sound for Flash, database and filesystem for Neko. These APIs are the same ones you would usually use in ActionScript or JavaScript. This makes haXe very easy to learn; all you need to do is to adapt to the small differences in haXe syntax.

Hello haXe!
Everything I've said until now might sound like nothing more than a nice theory, so let's dive into practical programming to see how things are working in the real world.

class Hello {
    static function main() {
       trace("Hello haXe !");

This class is the most simple haXe example. Let's try to compile and run it on the three supported platforms. For that, we need to write an .hxml file that will list compiler parameters:

-main Hello
-swf hello.swf

If haXe is installed, you should be able to run the HXML file by double-clicking on it, and running it from the command line by using "haxe file.hxml". This will produce a hello.swf file that, when opened, will display the text directly on the screen. Now, let's change the HXML file to compile for JavaScript instead of Flash:

-main Hello
-js hello.js

Again, this will produce a hello.js file. Let's include the JS in the following HTML page and open it:

      <div id="haxe:trace"></div>
      <script type="text/JavaScript" src="hello.js"></script>

That will display the text at the position of the div. As you will have noticed, this is not terribly different from what one would write in Java or ActionScript. haXe syntax is very user-friendly and 90% compatible with most popular programming languages. Therefore, it's easy for any programmer familiar with one of these languages to write haXe code.

Multiplatform haXe Code
So how is the trace function - which has different behavior depending on the platform - implemented? Is it some kind of haXe black magic? Not exactly. While platform-specific code can't be compiled for other platforms, haXe accepts conditional compilation statements that enable a different implementation depending on the platform. For example, let's look at the following method:

function displayError( msg : String ) {
    #if flash
      // flash-specific code
      var mc = flash.Lib._root;
      mc.tf.text = msg;
      mc.tf.textColor = 0xFF0000;
    #else js
      // JavaScript-specific code

When the displayError function is compiled for Flash, it will use the Flash code, and when it's compiled for JavaScript, it will use the JavaScript code. This enables developers to have some code that - while using platform-specific code - can be compiled and run on the different platforms. This is actually the way some of the haXe standard libraries are implemented.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.