KEMBAR78
Web polyglot programming | PDF
Polyglot
Programming in
or ‘building hybrid web
buzdin@gmail.com


          @buzdin

Dmitry Buzdin
The Problem
Web App of 2006
Web App of 201
Local
 WebGL Storage
     Perfor
               CSS3
     mance
Interopera
            Mobile
Respon
   bility
          RESTCanvas
  sive
      Offline apps
 design
Modulari
          JSONComet
Complexity inside
your browser app
The most
 popular

        * and
       Node.js
Good Stuff about
   JavaScript
 Simple to start with
 Dynamic
 Runs in all browsers
 Hundreds of libraries
So why not
JavaScript?!
console.log("2"   / "2");
// 1
console.log("2"   * "2");
// 4
console.log("1"   + "2");
// 12
console.log(1 +   "2");
// 12
console.log([1, 2, 3] + 1);
// 1,2,31
console.log(1 + true + false);
// 2
console.log(true + "a");
// truea
JavaScript Flaws
 Weak dynamic typing
  No modularity/proper class
syntax/scopes
 Portability problems
 Lack of Refactoring support
 Slow Evolution?
JavaScri
  pt...
JavaScript is hard
    to scale!
There is an
  Languages,
which compile to
   JavaScript
Good Stuff About
  Type safety

  Proper classes/modules

  Evolution is not tied to all
 browser vendors
What will you lose
without JavaScript?
   JSON
   Eval()
    Hundreds of
  Libraries
   jQuery?
How to get
the best from
The Solution
Hybrid Approach

Two or more languages
     on the client
 JavaScript as target
      platform
Let’s split our
  system to
Views


 UI Components              View Libraries


                 JSON Model


   Controllers             Support Libraries


AJAX/Comet           REST           Storage
Frequency of
  changes
                    Views


               UI Components


               Application Logic


           Infrastructure Code

                              Code complexity
UI plumbing
   could be done
   with dynamic
     language
Infrastructure code
       is more
  manageable in
Some Problems
     How do these
languages interoperate?


 How to reuse existing
How to
integrate
GWT in 2
•Write in Java compile
to JavaScript
•
Development mode
with code refresh
•
Transparent remoting
Some Features
•HTML5 API support
•Cross-browser
 compatibility
•Closure Tools compiler
•Production-ready since
Used In
•Google products
•Web projects (Evernote
 for example)
•Plenty of Enterprise
 Apps!
GWT <> JavaScript
private static void java(String param) {
}

private native void javaScript() /*-{

  $wnd.alert(‘Hello, JavaScript!’);

  $wnd.callback = @com.a.b.Type.java
(Ljava.lang.String;);

}-*/;
Dart in 2 Minutes
•New language made by
 Google
•Compiles to JavaScript
 or runs in VM
•Both server and client
Dart Highlights
•
Created by people with
GWT, V8, JVM and Java
background
•
Sweet spot between Java
and JavaScript
Used In

•
Milestone 1 released in
October 2012
•
Not used yet...
Dart <> JavaScript
#import('package:js/js.dart', prefix: 'js');

void main() {

    dart() {
    }

    js.scoped() {
      js.context.alert(‘Hello, JavaScript!’);
      js.context.x = new js.Callback.once(dart);
    }
}
Code
  like
that?...
JavaScript   Dart/GWT
   Part         Part
Too complex and
              unmanageable




JavaScript                     Dart/GWT
   Part                           Part
JavaScript         ?          Dart/GWT
   Part                          Part

             What if we do
              it like this?
Event Bus
  in the
        OMG!
Introducing
     Event Bridge
        EventBridge                     Callback

subscribe(Topic, Callback)
unsubscribe(Callback)            onEvent(Data, Callback)
publish(Topic, Data, Callback)
Java
bridge.subscribe("broadcast", this);



public void onEvent(ModelAttributes attributes,
  		 	 	 	 	 	       ModelEventCallback callback) {
   ModelAttributes result = Responses.attributes();
   result.set("result", "Response for JS");
   callback.resolve(result);
}


ModelAttributes data = Responses.attributes();
data.set("value", "Hello from GWT");
bridge.publish("broadcast", data);
onEvent(data, callback) {
  // Dart code                             Dart
}

eventBridge.subscribe(‘broadcast’, onEvent);



onResult(data) {
  // Dart code
}

eventBridge.publish('broadcast',
           {'value': 'Hello from Dart'},
           onResult);
$bridge.subscribe('broadcast',         JavaScript
        function (attrs, fn) {
            // JavaScript code
            fn();
        });




$bridge.publish('broadcast',
            { value: ‘Hello from JavaScript‘ },
            onResult
);
Profit!
 Not using JavaScript
interoperability API
 Transparent JSON based
exchange
 Single point of
communication
Event Bridge is
   coded in
  JavaScript,
Dart
                      Coffee
   Clojure
                      Script



                           Type
GWT            JS          Script



   Fantom             Kotlin
             Ceylon


One B!d" to rule #em all
Real-World Stuff
Introducing
 Visual Graph-
      based
spreadsheets in
    the cloud
         www.livesheets.com
Main
 Challenges
 Complex Domain
Model
 Expression language
 Offline calculations
 Sharing and
in JavaScript
                Client TechStack          in Java


       Twitter Bootstrap        GWT SDK


          Underscore.js           Guava


            Require.js     B      Guice


                jQuery           ANTLR


                jsPlumb        Domain Model
Event Flow

Draw

                    Domain Model
       Click




       jQuery   B    Controller



   jsPlumb
                        REST
Views


             UI Components             View Libraries
JavaScript
                            JSON Model
  Java
              Controllers             Support Libraries


         AJAX/Comet             REST           Storage
Clean View and Logic
separation
  Reusing cool
JavaScript libraries
 ANTLR in the browser!
 Expressions on client
Conclusions
JavaScript is not enough for
big/complex apps!
JavaScript is a platform, not
a language
For ambitious projects use
GWT, Dart or other
Client side has architecture
JavaScript
Interoperability in
 Essential for anything
the browser
  If missing -> choose
next technology
   Check for JavaScript
library wrappers
Pick the right
    tools!
buzdin@gmail.com


          @buzdin

Dmitry Buzdin
Additional Info
https://developers.google.com/web-toolkit/
http://www.dartlang.org
https://github.com/buzdin/hybrid-web-apps

        FlickrAttributions
               Sander van der Wel
                Alex E. Proimos
                    esther**
               Maccio Capatonda

Web polyglot programming