new taming ajax with gwt - software summit · 2008. 1. 3. · scott blum — taming ajax with gwt...

65
Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc. Scott Blum — Taming AJAX with GWT Page 1 Taming AJAX with GWT Scott Blum

Upload: others

Post on 16-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 1

Taming AJAX with GWTScott Blum

Page 2: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 2

OverviewWhy AJAX?Why GWT?How GWT WorksAdd GWT to your AppAdvanced TechniquesSummaryQ & A

Page 3: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 3

Demo – A Production AppAJAX Texas Hold’em

http://www.gpokr.com/

Page 4: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 4

AJAX: Easy on UsersNo installation

Every application is just a URL away

Secure… the mantra we teach our parentsinstalling things = mostly unsafesurfing the web = mostly safe

Web simplicityAJAX pages have the familiar web look and feelWeb simplicity – back, forward, links, URLs

Page 5: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 5

AJAX: Easy on ServersState maintained on client

Only send the data, not the presentationSend only deltas instead of entire UI stateClean separation of concernsAllows servers to be effectively stateless

Leverage user’s CPU and RAMDo everything you can on the clientMuch faster for the user, too!

Page 6: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 6

Traditional App Cycle

Page 7: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 7

AJAX App Cycle

Page 8: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 8

Summary and Q&AUse AJAXThe End

Page 9: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 9

OverviewWhy AJAX?Why GWT?How GWT WorksAdd GWT to your AppAdvanced TechniquesSummaryQ & A

Page 10: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 10

AJAX: Hard on DevelopersJavaScript is the “J” in AJAXNo strong type system

typos + expandos = bug-o-sImagine this gem on line 5912 of your script

x.compnent = document.getElementById("x");// a "spelling bug" that will bite much later

Poor IDE supportDebuggers… for some browsers…

Page 11: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 11

AJAX: Hard on DevelopersIf you support IE6 and FF you’re doing well!But then there’s

IE7Older MozillaeSafariOperaKonquerorYou name it

Page 12: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 12

AJAX: Hard on DevelopersNo installation… sort of

In a sense, it’s also always-reinstall (better be small)Modularizing JavaScript is really tricky

Secure… sort ofHard enough just to make AJAX work at all!Lots of JS = lots of attack surface

Has web simplicity… sort ofHistory, bookmarks and even hyperlinks misbehaveBadly coded AJAX is worse than traditional HTML

Page 13: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 13

AJAX: Hard on DevelopersLots of technologies to worry about (you need regexps to list them all on one page)

HTTPS?, [DX]?HTML (3.2|4.0), CSS[1-3]DOM Level[0-3](Java|ECMA|J|VB)Script(X|VR?|Math)MLSVG, Canvas, FlashJSON, SOAP, XML-RPC

Page 14: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 14

AJAX: Hard on DevelopersLots of JS libraries to choose from, but…

You often wind up paying for the whole library when you only need 5% of itInline comments and well-named identifiers add to code sizeThe compiler can’t tell you if your using the library wrongUpgrading to new versions?

Page 15: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 15

Solution: Use GWTWrite your AJAX code in JavaCompile it with a real Java compilerDebug it in a real Java IDEUnit test it with JUnitJavadoc it without script size worriesReuse it on a class or package basisShare it as a JAR

Page 16: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 16

Solution: Use GWTJava has a lot of advantages over JavaScript

Static type checkingGreat IDE support• Code completion• Quick correction• Lint, style, and other warnings• Refactoring

Established code patternsScales to large projects and large teams

Page 17: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 17

Solution: Use GWT

(+50 more slides about howJava whoops JavaScript)

Page 18: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 18

OverviewWhy AJAX?Why GWT?How GWT WorksAdd GWT to your AppAdvanced TechniquesSummaryQ & A

Page 19: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 19

How GWT WorksBuild your app in JavaDOM-based Widget frameworkDebug in a real Java IDECompile to JavaScriptDeploy compiled output as static contentBuilt-in RPC based on Servlets

Page 20: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 20

Code Sample – A Simple App<html><head><meta name='gwt:module'content='com.google.gwt.sample.hello.Hello'>

<title>Hello</title></head><body><script language="javascript" src="gwt.js"></script></body></html>

Page 21: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 21

Code Sample – A Simple Apppublic class Hello implements EntryPoint {

public void onModuleLoad() {Button b = new Button("Click me",new ClickListener() {public void onClick(Widget sender) {Window.alert("Hello, AJAX");

}});RootPanel.get().add(b);

}

}

Page 22: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 22

Demo – A Simple AppHello, AJAX

Page 23: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 23

How GWT WorksGWT

ToolsClient Server

Java to JavaScriptCompiler

Hosted Mode

Widget Framework

JRE Emulation

Browser Abstraction

SerializationFramework

Deferred Binding

Java Servlet

Other Servers

Page 24: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 24

How GWT Works

Client

Widget Framework

JRE Emulation

Browser Abstraction

Page 25: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 25

Client Frameworkgwt-user.jarOpen Source: Apache 2.0Browser abstraction layerFull set of UI Widgets, styled with CSS

Real HTML elementsEvent drivenLike Swing or SWT

Panels for managing layout

Page 26: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 26

Demo – Widgets and HistoryMail and KitchenSink

Page 27: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 27

Client FrameworkGrowing subset of the JRE

Much of java.lang, java.utilNo reflection (by design!)What makes sense on a browser• No threads, file i/o, etc.

RPC, XML, JSON, i18n, other acronyms

Page 28: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 28

How GWT Works

Server

Java Servlet

Serialization Framework

Other Servers

Page 29: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 29

GWT’s Built-in RPCgwt-servlet.jarOpen Source: Apache 2.0Servlets on the server side Explicit server calls

This is a good thing ☺

Declare a remote interfaceSend POJOs, primitives, collections

Page 30: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 30

Code Sample – RPC Interfaces// Synchronous interface (declared)public interface SpellingService extends RemoteService {String[] suggest(String wordToCheck);

}

// Asynchronous interface (derived)public interface SpellingServiceAsync {void suggest(String wordToCheck, AsyncCallback callback);

}

Page 31: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 31

Code Sample – RPC CallSpellServiceAsync spell = GWT.create(SpellService.class);

spell.suggest("compnent", new AsyncCallback() {

void onSuccess(Object result) {String[] alts = (String[])result;if (alts.length > 0)showSuggestions(alts);

}

void onFailure(Throwable e) {reportProblem(e);

}

});

Page 32: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 32

Code Sample – RPC Servlet// Servlet classpublic class SpellingServiceImpl

extends RemoteServiceServletimplements SpellingService {

public String[] suggest(String maybeMisspelledWord) {List list = new ArrayList();// populate list; run any server-side code you likereturn (String[]) list.toArray(new String[list.size()]);

}

}

Page 33: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 33

Demo – RPCDynaTable

Page 34: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 34

Network Usage

0

10000

20000

30000

40000

50000

60000

70000

80000

90000

Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7

Byt

es T

rans

ferr

ed

Traditional HTML GWT First Run GWT Other Runs

Page 35: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 35

GWT’s Built-in RPCAlways asynchronousGenerated classes perform serializationUnpublished super-efficient wire formatLibraries support custom RPC

Page 36: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 36

How GWT Works

Tools

Java to JavaScriptCompiler

Hosted Mode

Deferred Binding

Page 37: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 37

GWT Toolsgwt-dev-(windows|linux|mac).jarFree; Currently Closed-SourceCompilerHosted Mode supportCommon Infrastructure

Page 38: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 38

GWT Tools – CompilerCompile Java into JavaScriptFull Java 1.4 language supportDeploy compiled scripts on any web serverOptimize like heck

Monolithic compileType TighteningAggressive Code Pruning

Page 39: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 39

Hello AJAX – Javapublic class Hello implements EntryPoint {

public void onModuleLoad() {Button b = new Button("Click me",new ClickListener() {public void onClick(Widget sender) {Window.alert("Hello, AJAX");

}});RootPanel.get().add(b);

}

}

Page 40: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 40

Hello AJAX – Generated Script// from class Hellofunction _$onModuleLoad(_this$static){var _b = _$Button(new _Button(), 'Click me',_$Hello$1(new _Hello$1(), _this$static));

_$add(_get(), _b);}

// from anonymous ClickListener classfunction _onClick(_sender){_alert('Hello, AJAX');

}

Page 41: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 41

Hello AJAX – Obfuscatedfunction ne(oe){var pe=qe(new re(),'Click me',se(new te(),oe));ue(ve(),pe);}

function xe(ye){ze('Hello, AJAX');}

Page 42: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 42

GWT Tools – Pay for Use

0

10000

20000

30000

40000

50000

60000

Empty

Wind

ow.al

ert("h

ello")

Vertica

lPanel

Button

Check

BoxTree

(3 ite

ms)

Labe

lTex

tBox

Grid (4

x 4)

HTML

Imag

e

TabPan

el (3

tabs)

ListB

ox (3

items)

Radio

Button

(3)

Hyperl

inkHori

zonta

lPanel

FlowPan

elAbs

oluteP

anel

Scrip

t Siz

e (b

ytes

)

Page 43: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 43

GWT Tools – Hosted ModeRun under a real 1.4 or 5.0 JVMRun inside the browser with full DOM accessFull debugging and IDE supportPlatform support:

Windows with IELinux with MozillaMac OS X with Safari may already be available

Embedded Tomcat serves files from your classpath for easy setup

Page 44: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 44

GWT Tools – Deferred BindingReplace classes at compile time

DOM dom = (DOM)GWT.create(DOM.class);

dom.doSomethingPlatformSpecific();

At runtime, dom actually refers to the most appropriate subclass

DOMInterface

InternetExplorerClass

MozillaClass

Application code

Deferred binding compiles in the correct subclass based on

selection rules.SafariClass

Page 45: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 45

GWT Tools – Deferred Binding

Single JavaCode Base

Download exactly what you need in a single

can't-go-wrong chunk

Cache it on the client until the sun explodes

FireFox 1.0.x

Your Code

en_US

1D04ADDA.cache.html

Safari 2.0.x

Your Code

fr_FR…

7EFE4D24.cache.html

IE 6

Your Code

en_UK…

15F361BB.cache.html

Opera 9

Your Code

fr_CA…

D415D917.cache.html

Page 46: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 46

GWT Tools – Deferred BindingWhy do at runtime what you can do at compile time?Because of type tightening, polymorphic calls can be inlinedGenerators provide an optimized alternative to runtime reflection

Our own RPC is built on it

Page 47: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 47

OverviewWhy AJAX?Why GWT?How GWT WorksAdd GWT to your AppAdvanced TechniquesSummaryQ & A

Page 48: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 48

Add GWT to Your AppAttach a <meta> tag to your HTMLInclude the gwt.js scriptAdd an id to elements you’re interested inDeploy compiler output as static contentWorks with any HTML-generation approach

Page 49: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 49

Demo – Adding GWTA Simple JSP Example

Page 50: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 50

OverviewWhy AJAX?Why GWT?How GWT WorksAdd GWT to your AppAdvanced TechniquesSummaryQ & A

Page 51: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 51

Advanced TechniquesEffective RPCHanging RPCJavaScript native interface (JSNI)

Page 52: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 52

Effective RPCCalls are Asynchronous only

Think event-driven

Limit the number of invocationsThink “chunky”, not “chatty”Ideally, no more than 1 call per user action

Limit to 2 outstanding calls at onceBrowser-imposed limit

Page 53: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 53

Hanging RPCImagine a chat room appNaïve solution:

Clients poll on timerMost calls return emptySlow perceived responseWasted processing/bandwidth

Page 54: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 54

Hanging RPCa.k.a. “server push” or “comet”

Not really possible with HTTP; this fakes it

Basic algorithmClient calls to check for eventsServer blocks until an event occursClient calls again to wait for next event

Pro: Less processing/bandwidth than pollingCon: Each client constantly uses a TCP connection and server thread; not as easy to throttle clients

Page 55: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 55

More Info on RPCRon Bodkin’s AJAX Performance and Monitoring

More depth on these topicsJetty continuations and hanging RPC

Page 56: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 56

JavaScript Native InterfaceHand-write JavaScript into your Java class

Like inline assembly in C

Foundation of GWT’s frameworkInteract with browser directlyCall an external script

Wrap existing JavaScript with type-safe Java signatures

Constructs JS supports better than JavaFunction pointer tableString-key map

Page 57: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 57

Code sample – JSNI

native void alert(String msg) /*-{alert(msg);

}-*/;

Page 58: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 58

JSNI FeaturesImplement a Java method in JavaScript Call Java methods from JavaScriptRead and write Java fields from JavaScriptMarshal values between Java/JavaScript

Super efficient in production

Throw exceptions across Java/JavaScript boundariesOur entire client framework is based on it

If you don’t like ours, you can write your own ☺

Page 59: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 59

Code Sample – JSNInative JavaScriptObject lazyInitMap() /*-{var map = [email protected]::stringMap;if (map == null) {map = [email protected]::stringMap = {};

}return map;

}-*/;

native String getValue(String value) /*-{var map = [email protected]::lazyInitMap()();var result = map[key];return (result == null) ? null : result;

}-*/;

native void putValue(String key, String value) /*-{var map = [email protected]::lazyInitMap()();map[key] = value;

}-*/;

Page 60: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 60

OverviewWhy AJAX?Why GWT?How GWT WorksAdd GWT to your AppAdvanced TechniquesSummaryQ & A

Page 61: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 61

Growing GWT CommunityCommunity and Support

5000+ members on the developer forumBooks, articles, blogsMeta-sites (e.g. http://gwtpowered.org)645,000 results for “google web toolkit”

Libraries and Applications25 projects on Google Code Project HostingGWT Widgets on SourceForgeJetBrains' JET markup framework for GWT

ToolsJetBrains IntelliJ IDEA support for GWT built into Version 6.0Googlipse, an open source Eclipse plug-in for GWTVistaFei for GWT – Visual DesignerInstantiations GWT Designer – Visual Designer; round-trip to Source!

Page 62: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 62

Instantiations GWT Designer

Page 63: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 63

Production Appshttp://www.dimdim.comWeb-based conferencing

http://www.indigopuzzles.comHigh-quality online puzzles

http://www.notetwonote.comStudent self-testing

Page 64: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 64

ConclusionAJAX is hard: you tame it… or vice-versa

You need leverage to take full advantage of AJAXPhD in browser quirks is no longer a prereqWe will share our best work and ideas with you, and we hope you will return the favorMuch more to come… see you online!

http://code.google.com/webtoolkit/

Page 65: New Taming AJAX with GWT - Software Summit · 2008. 1. 3. · Scott Blum — Taming AJAX with GWT Page 5 AJAX: Easy on Servers State maintained on client ¾Only send the data, not

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Google, Inc.

Scott Blum — Taming AJAX with GWT Page 65

Q & AAsk me anything! ☺[email protected]