Most modern websites still place a large burden on the server, constantly sending it requests and asking it to do heavy computations. In the brave new world, the client is king and the server is its faithful shadow. In this talk, we'll look at how cutting-edge technology like Gears, HTML5, and Google App Engine can be used to create websites where the caching, storage, and computing is done primarily in the browser/desktop and the server is used merely as a backup store.
Presented at Webstock 2009.
Original version (not PPTed):
http://tr.im/clientkilledserver
14. Resource Storage
Only ask the server for resources when the app's been
updated.
Resources = JS, CSS, HTML, IMGs, etc.
• HTML 5: Application Cache, Manifest
• Gears: LocalServer
15. HTML 5: Manifest
Point to manifest file in HTML:
<html manifest=”foo.manifest”>
Create foo.manifest:
CACHE MANIFEST
# v1
# This is a comment.
http://www.foo.com/index.html
http://www.foo.com/header.png
http://www.foo.com/blah/blah
somethingElse.jpg
Demo
16. HTML 5: ApplicationCache
Use ApplicationCache object and window.applicationCache:
var appCache = window.applicationCache;
if (appCache.status ==
ApplicationCache.UNCACHED) {
alert("Not cached yet");
}
appCache.oncached = function() {
alert("Cached now!");
}
Also available:
update, swapCache, onchecking, onerror,
onnoupdate, ondownloading, onprogress,
onupdateready, oncached, onobsolete
17. Gears: LocalServer
Use ResourceStore to grab array of files:
var pageFiles = [
location.pathname,
'gears_init.js'
];
var localServer =
google.gears.factory.create('beta.localserver', '1.0');
var store = localServer.openStore(this.storeName) ||
localServer.createStore(this.storeName);
store.capture(pageFiles, function(url, success, captureId) {
console.log(url + ' capture ' + (success ? 'succeeded' :
'failed'));
});
Demo
18. Gears: LocalServer
Use ManagedResourceStore to use manifest file:
var localServer = google.gears.factory.create('beta.localserver');
var store = localServer.createManagedStore(STORE_NAME);
store.manifestUrl = 'manifest_v1.json';
store.checkForUpdate();
manifest_v1.json:
{ "betaManifestVersion": 1,
"version": "v1",
"entries": [ { "url": "managed_store.html",
"src": "managed_store_v1.html" }, ...]}
Demo
21. HTML 5: localStorage
Store strings in keys using Storage object &
window.localStorage:
function doSave() {
var filename = document.forms.editor.filename.value;
var data = document.forms.editor.data.value;
localStorage.setItem('file-' + filename, data);
}
function doOpen() {
var filename = document.forms.editor.filename.value;
document.forms.editor.data.value =
localStorage.getItem('file-' + filename);
}
Also use: removeItem, clear, window.onstorage
Demo
22. HTML 5: Database
Open DB with window.openDatabase, then use Database
object:
var db = openDatabase("MyDB", "1.0", "Example", 200000);
db = openDatabase("NoteTest", "1.0", "HTML5 Database API
example", 200000);
db.transaction(function (tx) {
tx.executeSql("INSERT INTO WebKitStickyNotes
(id, note, timestamp, left, top, zindex) VALUES
(?, ?, ?, ?, ?, ?)",
[note.id, note.text, note.timestamp, note.left, note.top,
note.zIndex]);
});
Demo
23. Gears: Database
Open DB, then execute SQL statements
var db = google.gears.factory.create('beta.database',
'1.0');
db.open('database-demo');
db.execute('create table if not exists Demo (Phrase
varchar(255), Timestamp int)');
db.execute('insert into Demo values (?, ?)', [phrase,
currTime]);
var rs = db.execute('select * from Demo order by Timestamp
desc');
Demo
28. Web Worker Spec
Create a worker and send messages to it:
var searcher = new Worker('searcher.js');
function search(query) {
searcher.postMessage(query);
}
Respond to messages in worker JS:
importScripts('io.js');
onmessage = function (event) {
postMessage(get('search.cgi?' + event.data));
};
29. Gears: WorkerPool
Create worker IDs in WorkerPool, send/receive messages:
workerPool.onmessage = function(a, b, message) {
insertRow('Async Result:' + message.body);
};
var childId = workerPool.createWorkerFromUrl('worker.js');
workerPool.sendMessage(2501234, childId);
// worker.js
var wp = google.gears.workerpool;
wp.onmessage = function(messageText, senderId, message) {
wp.sendMessage(identity(message.body), senderId);
};
function identity(stuff) {
// do hard math with stuff
}
Demo
31. Dynamic Graphics
Render graphics or manipulate bitmaps in JS. No servers or
plugins needed to generate them.
Both 2d and 3d graphics can be done in browser.
• HTML 5: Canvas
• SVG
32. HTML 5: Canvas
Create <canvas> element:
<canvas id="canvas" width="150"
height="150"></canvas>
Then call functions on CanvasRenderingContext2D:
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "rgb(200,0,0)";
ctx.fillRect (10, 10, 55, 50);
ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
ctx.fillRect (30, 30, 55, 50);
Also use: scale, rotate, translate, transform,
createGradientPattern, bezierCurveTo,
putImageData, filLText, etc.
Demo
34. Get Involved!
HTML 5/Web Worker:
• Join mailing list.
• Subscribe two twitter updates (@whatwg).
• Experiment with features using nightly builds:
• IE 8 beta, Opera Labs , MineField (FF), Webkit
Gears:
• Join the project.
• Discuss in group.
• Make real apps!
Notes de l'éditeur
- Browser does nothing
- All state in relational DB
- client replicates some functionality of server
- server is still queried constantly - but better UI for users
- client used for heavy computation, heavy storage
- server is backup storage
HTML5 is the next evolution of HTML, and will be backward compatible with existing HTML. As the spec is being worked on, parts of it being gradually adopted by the various browsers. The whole spec likely won’t be officially done for another decade.Founded by people from Apple, Mozilla, Opera, after a disappointing W3C workshop.One editor (Ian Hickson) reviews submissions and modifies the specification.
Gears is an open source plug-in that makes the browser more powerful. A popular use of Gears is to give web apps offline functionality using the following 3 modules:
http://webkit.org/misc/DatabaseExample.html
http://webkit.org/misc/DatabaseExample.html
http://webkit.org/misc/DatabaseExample.html
same-domain!
same-domain!
same-domain!
http://webkit.org/misc/DatabaseExample.html
-- presidential helper
-- mysql message search
file storer demo
expected to store user documents
per-site, per-browser
full relational DB
sqllite
asynchronous
readTransaction, transaction
insert/create/delete/select
callbacks for errors
also has FTS2
synchronous
myspace messaging!
also has FTS2
synchronous
myspace messaging!
per-window key/value
e.g. purchasing plane tickets
TSP solver
Spatial queries
web workers isnt part of html 5 spec, but it is being worked on by same group (WHATWG)
lots of XMLHRs
encryption
searching a database
Workers (as these background scripts are called herein) are relatively heavy-weight, and are not intended to be used in large numbers. For example, it would be inappropriate to launch one worker for each pixel of a four megapixel image. The examples below show some appropriate uses of workers.
Generally, workers are expected to be long-lived, have a high start-up performance cost, and a high per-instance memory cost.
lots of examples on web worker spec
Workers (as these background scripts are called herein) are relatively heavy-weight, and are not intended to be used in large numbers. For example, it would be inappropriate to launch one worker for each pixel of a four megapixel image. The examples below show some appropriate uses of workers.
Generally, workers are expected to be long-lived, have a high start-up performance cost, and a high per-instance memory cost.
lots of examples on web worker spec
Workers (as these background scripts are called herein) are relatively heavy-weight, and are not intended to be used in large numbers. For example, it would be inappropriate to launch one worker for each pixel of a four megapixel image. The examples below show some appropriate uses of workers.
Generally, workers are expected to be long-lived, have a high start-up performance cost, and a high per-instance memory cost.
lots of examples on web worker spec
// used to have to render on servers or plugins to be fast enough
-- Polygonzo
-- Fractals
// dont need a server to change pixel data, can do it on the client
-- Montage Maker
// used to have to render on servers or plugins to be fast enough
-- Polygonzo
-- Fractals
// dont need a server to change pixel data, can do it on the client
-- Montage Maker
// used to have to render on servers or plugins to be fast enough
-- Polygonzo
-- Fractals
// dont need a server to change pixel data, can do it on the client
-- Montage Maker