2008-10-10

GWT - My love for

Recently I have been dipping my toes in the waters of Google Web Toolkit . GWT is a framework for writing client-side Web 2.0 style applications without having to have write any JavaScript, HTML or without having to think about any browser-specific quirks. All code is written in Java and translated into JavaScript at compile-time (generated pages have no dependencies on Java at all).

GWT makes it simple to separate client-side logic and server-side logic. In GWT, pages are built from Panels and Components/Widgets. Pages are build programatically but are rendered using standard CSS layouts.

GWT Ext is a library that I can recommend wholeheartedly as its very easy to build a professional looking front-end using this extension.

What was interesting for me was not having to think of a website in terms of text generation as has been normal up until recently. Mixing code and layout has always been a mugs game. For this reason, I had used Wicket recently which takes a similar component based approach but the client is a slave to the server. E.g. too many round trips.

GWT turns the world on its head and the server is a slave to the client. The client pre-loads the whole GUI once and (in my recent application) never reloads. The client submits Asynchronous JavaScript requests to the server from time to time to gather server-side state or to confirm permissions but upon loading, the client loads its state model from the server and from then on, it very infrequently needs information from the server.

All code is written in Java. Server side and client side. The server side is just a extension of a regular Servlet that implements a specialized RPC interface. Clients can call server methods via a static RPC object with an asynchronous callback.

Clients are written in the same method as Swing applications. Anyone familiar with Swing or SWT will feel at home with GWT... Most of the common Java libraries can be used in GWT but not all. Where some are missing it is usually for obvious reasons such as the ability to perform the equivalent function is not available inside a browser JavaScript environment.

All client side code is translated into optimized and obfuscated JavaScript at compile time. Unused methods from referenced classes and objects are not included and long variable names become obfuscated short variable names. The JavaScript is embedded in a HTML file and referenced images are packed in a single image pack for speed of loading.

Best of all, the client and server side together can be step debugged in Eclipse or other IDEs. The client side code can be step debugged line by line in Java before ever touching the browser. The author benefits from strict type-checking and compile-time error checking.By the time the JavaScript is compiled, there are no syntax errors or typos. In fact, I have never needed to debug any JavaScript in my past 2 months with GWT.

My particular coding style in GWT is to build an application model. A tree hierarchy of my application schema that semi-relates to the serverside xml configuration of the application. Upon the client loading the page the first time, the client makes a request to the server to get a copy of the model locally. From the time the client has the model locally, very little interaction with the server is required. The client can render its own screens, perform its own checks and populate its own tooltips etc.

Session validation is an area that is a little under-documented in GWT, but via use of an authentication Servlet, you can supply clients with different models depending on their access rights.

GWT is a great toolkit for small-medium sized applications. I have not had chance to test it on larger style applications and it very much depends on the implementation of the RPC mechanism but I cannot and will not go back to direct HTML manipulation when creating websites.

Latency and round-trips are bastards but as someone based in Japan who primarily reads European or American websites, I know how much of a bastard it can be. The web exacerbates poor latency by a website referencing 10s and sometimes 100s of other resources from a single page. Every image, every stylesheet, every referenced JavaScript library results in another round trip with a browser only having 2 connection threads active at one time. It may take milliseconds to serve the content but every round trip has a latency cost. As such, many pages can take up to 10 seconds to load, even on a 100 Mbit internet connection.

GWT goes a long way to fixing roundtrip performance issues for browser-based applications without having to think too much about how. The majority of a site is bundled when you hit the first page and distributed sites will load faster in this scenario.

How much you love GWT depends on how much Java experience you have to leverage and how much you have invested in other similar technologies. As someone who has 10 years of Java experience and as someone who found the alternatives to be clumsy and human-error-prone, I love it. It just works. And works well.

Google yet again prove themselves masters of making complex things simple. As they should be.