62
Polyglot Programming in or ‘building hybrid web

Web polyglot programming

  • Upload
    neueda

  • View
    1.457

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Web polyglot programming

Polyglot Programming in

or ‘building hybrid web

Page 3: Web polyglot programming

The Problem

Page 4: Web polyglot programming

Web App of 2006

Page 5: Web polyglot programming

Web App of 2012

Page 6: Web polyglot programming

Performance

RESTInteropera

bilityResponsive

designModularity

Offline appsCanvas

Local Storage

Comet

Mobile

JSON

WebGLCSS3

Page 7: Web polyglot programming

Complexity inside your browser app

Page 8: Web polyglot programming

The most popular

* and Node.js

Page 9: Web polyglot programming

Good Stuff about JavaScript

Simple to start withDynamicRuns in all browsersHundreds of libraries

Page 10: Web polyglot programming

So why not JavaScript?!

Page 11: Web polyglot programming

console.log("2" / "2"); // 1console.log("2" * "2"); // 4console.log("1" + "2"); // 12console.log(1 + "2"); // 12

Page 12: Web polyglot programming

console.log([1, 2, 3] + 1); // 1,2,31console.log(1 + true + false); // 2console.log(true + "a"); // truea

Page 13: Web polyglot programming

JavaScript Flaws Weak dynamic typing No modularity/proper class syntax/scopes Portability problems Lack of Refactoring support Slow Evolution?

Page 14: Web polyglot programming

JavaScript...

Page 15: Web polyglot programming
Page 16: Web polyglot programming

JavaScript is hard to scale!

Page 17: Web polyglot programming

There is an Languages,

which compile to JavaScript

Page 18: Web polyglot programming

Type safety

Proper classes/modules

Evolution is not tied to all browser vendors

Good Stuff About

Page 19: Web polyglot programming

JSON Eval() Hundreds of

Libraries jQuery?

What will you lose without JavaScript?

Page 20: Web polyglot programming

How to get the best from

Page 21: Web polyglot programming

The Solution

Page 22: Web polyglot programming

Hybrid Approach

Two or more languages on the client

JavaScript as target platform

Page 23: Web polyglot programming

Let’s split our system to

Page 24: Web polyglot programming

JSON Model

Controllers

UI Components

Views

View Libraries

Support Libraries

AJAX/Comet REST Storage

Page 25: Web polyglot programming

Frequency of changes

Code complexity

Infrastructure Code

Application Logic

UI Components

Views

Page 26: Web polyglot programming

Infrastructure code is more

manageable in static language

UI plumbing could be done with dynamic

language

Page 27: Web polyglot programming

Some ProblemsHow do these

languages interoperate?

How to reuse existing

Page 28: Web polyglot programming

How to integrate

Page 29: Web polyglot programming

GWT in 2 •Write in Java compile

to JavaScript

•Development mode with code refresh

•Transparent remoting

Page 30: Web polyglot programming

•HTML5 API support

•Cross-browser compatibility

•Closure Tools compiler

•Production-ready since

Some Features

Page 31: Web polyglot programming

•Google products

•Web projects (Evernote for example)

•Plenty of Enterprise Apps!

Used In

Page 32: Web polyglot programming

GWT <> JavaScriptprivate static void java(String param) {}

private native void javaScript() /*-{

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

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

}-*/;

Page 33: Web polyglot programming

•New language made by Google

•Compiles to JavaScript or runs in VM

•Both server and client

Dart in 2 Minutes

Page 34: Web polyglot programming

•Created by people with GWT, V8, JVM and Java background

•Sweet spot between Java and JavaScript

Dart Highlights

Page 35: Web polyglot programming

•Milestone 1 released in October 2012

•Not used yet...

Used In

Page 36: Web polyglot programming

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); }}

Page 37: Web polyglot programming

Code like

that?...

Page 38: Web polyglot programming

JavaScript Part

Dart/GWTPart

Page 39: Web polyglot programming

JavaScript Part

Dart/GWTPart

Too complex and unmanageable

Page 40: Web polyglot programming

JavaScript Part

Dart/GWTPart

?

What if we do it like this?

Page 41: Web polyglot programming

Event Bus in the

OMG!

Page 42: Web polyglot programming

Introducing Event Bridge

EventBridge Callback

subscribe(Topic, Callback)unsubscribe(Callback)publish(Topic, Data, Callback)

onEvent(Data, Callback)

Page 43: Web polyglot programming

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);

Javabridge.subscribe("broadcast", this);

Page 44: Web polyglot programming

onResult(data) { // Dart code}

eventBridge.publish('broadcast', {'value': 'Hello from Dart'}, onResult);

DartonEvent(data, callback) { // Dart code}

eventBridge.subscribe(‘broadcast’, onEvent);

Page 45: Web polyglot programming

$bridge.subscribe('broadcast', function (attrs, fn) { // JavaScript code fn(); });

$bridge.publish('broadcast', { value: ‘Hello from JavaScript‘ }, onResult );

JavaScript

Page 46: Web polyglot programming

Not using JavaScript interoperability APITransparent JSON based

exchangeSingle point of

communication

Profit!

Page 47: Web polyglot programming

Event Bridge is coded in

JavaScript,

Page 48: Web polyglot programming

JS

One B!d" to rule #em all

Dart

Type ScriptGWT

Clojure

Fantom

CoffeeScript

KotlinCeylon

Page 49: Web polyglot programming

Real-World Stuff

Page 50: Web polyglot programming

Introducing

www.livesheets.com

Visual Graph-based

spreadsheets in the cloud

Page 51: Web polyglot programming
Page 52: Web polyglot programming

Complex Domain Model Expression language Offline calculations Sharing and

Main Challenges

Page 53: Web polyglot programming

Twitter Bootstrap

Underscore.js

Require.js

jsPlumb

jQuery

GWT SDK

Guava

ANTLR

Domain Model

Guice

Client TechStack in Javain JavaScript

B

Page 54: Web polyglot programming

Click

jsPlumb

jQuery Controller

REST

Domain Model

B

Event Flow

Draw

Page 55: Web polyglot programming

JSON Model

Controllers

UI Components

Views

View Libraries

Support Libraries

AJAX/Comet REST Storage

Java

JavaScript

Page 56: Web polyglot programming

Clean View and Logic separation

Reusing cool JavaScript libraries

ANTLR in the browser!

Expressions on client

Page 57: Web polyglot programming

Conclusions

Page 58: Web polyglot programming

JavaScript is not enough for big/complex apps!JavaScript is a platform, not a languageFor ambitious projects use GWT, Dart or otherClient side has architecture

Page 59: Web polyglot programming

JavaScript Interoperability Essential for anything in

the browser If missing -> choose

next technology Check for JavaScript

library wrappers

Page 60: Web polyglot programming

Pick the right tools!