Transcript
Page 1: Webapps without the web

Web Apps without the WebRemy Sharp / @rem

Page 2: Webapps without the web

What's what.

Page 3: Webapps without the web

HTML5 "HTML5"

Page 4: Webapps without the web

HTML5

Offl

ine applicationsO

ffline events

CanvasV

ideoW

eb Forms

"HTML5"

Page 5: Webapps without the web

HTML5

Offl

ine applicationsO

ffline events

CanvasV

ideoW

eb Forms

"HTML5"

Web Storage

Web SQ

L Databases

Web Sockets

Web W

orkersG

eolocationM

OA

R!!!

Page 6: Webapps without the web

HTML5 Apps

Page 7: Webapps without the web

◥◥"Offline Storage"

Page 8: Webapps without the web

Offline cache

Data storage"Offline Storage"◥

Page 9: Webapps without the web

1. Web Storage

2. Web SQL Databases

3. HTML5 offline applications

4. APIs of the future

Page 10: Webapps without the web

Web Storage(cookies on steroids)

Page 11: Webapps without the web

Web Storage(cookies on steroids)

Page 12: Webapps without the web

Cookies Suck.

Page 13: Webapps without the web

Two windows have access to the same

"session" cookie

Page 14: Webapps without the web

Storage

sessionStorage

localStorage

window based

Page 15: Webapps without the web

Storage

sessionStorage

localStorage

window based

domain based

Page 16: Webapps without the web

Setting

sessionStorage.setItem('version',5);

Page 17: Webapps without the web

Setting

sessionStorage.setItem('version',5);

sessionStorage.version = 5;

Page 18: Webapps without the web

Setting

sessionStorage.setItem('version',5);

sessionStorage.version = 5;

sessionStorage['version'] = 5;

Page 19: Webapps without the web
Page 20: Webapps without the web

Getting

sessionStorage.getItem('version');

Page 21: Webapps without the web

Getting

sessionStorage.getItem('version');

sessionStorage.version;

Page 22: Webapps without the web

Getting

sessionStorage.getItem('version');

sessionStorage.version;

sessionStorage['version'];

Page 23: Webapps without the web

sessionStorage.version = 5;

typeof sessionStorage.version; // ?

Page 24: Webapps without the web

sessionStorage.version = 5;

typeof sessionStorage.version; // ?

Page 25: Webapps without the web

Values are strings

Page 26: Webapps without the web

Values are strings

Work around: JSON(and http://www.json.org/json2.js)

Page 27: Webapps without the web

Complex Objects

var ss = sessionStorage,

user = { screen_name : ‘rem’,

rating : 11 };

ss.setItem(‘user’, JSON.stringify(user));

alert( JSON.parse(ss.getItem

(‘user’)).screen_name );

Page 28: Webapps without the web

There's no security

protecting the API

// Safari debugger broken:

sessionStorage.setItem('setItem',12);

Page 29: Webapps without the web

API• setItem(key, value)

• getItem(key)

• removeItem(key)

• length

• key(index)

• clear()

Page 30: Webapps without the web

What about supporting old

browsers?

Page 31: Webapps without the web

Pollyfilling

Page 32: Webapps without the web

Just make it work

Page 33: Webapps without the web

http://gist.github.com/350433

if (!localStorage || !sessionStorage) (function () {

var Storage = function (type) { ...};if (!localStorage) localStorage = new Storage('local');if (!sessionStorage) sessionStorage = new Storage('session');

)();

Just make it work

Page 34: Webapps without the web
Page 35: Webapps without the web
Page 36: Webapps without the web

Chrome's native Web Sockets &Flash is used to fill the holes

Page 37: Webapps without the web

(cookies on steroids on steroids)

Web SQL Databases

Page 38: Webapps without the web

(cookies on steroids on steroids)

Web SQL Databases

Page 39: Webapps without the web

1. openDatabase

2. db.transaction

3. transaction.executeSql

Web SQL Database API

Page 40: Webapps without the web

• Check for support

openDatabase

Page 41: Webapps without the web

• Check for support

• Estimate db size

openDatabase

Page 42: Webapps without the web

• Check for support

• Estimate db size

• Store return var

openDatabase

Page 43: Webapps without the web

• Check for support

• Estimate db size

• Store return var

• Forget versioning for now

openDatabase

Page 44: Webapps without the web

if (!window.openDatabase) { alert('No supported'); return;}

Page 45: Webapps without the web

var db = openDatabase( 'mydb', // name '1.0', // version 'My First Database', // description 2 * 1024 * 1024 // size, 2Mb);

Page 46: Webapps without the web

var db = openDatabase( 'mydb', // name '1.0', // version 'My First Database', // description 2 * 1024 * 1024 // size, 2Mb);

Page 47: Webapps without the web

Question:

How do I open the database without knowing the version?

Page 48: Webapps without the web

Question:

How do I open the database without knowing the version?You can't.

Page 49: Webapps without the web

transaction

• Container for running SQL

Page 50: Webapps without the web

transaction

• Container for running SQL

• Queues executeSql

Page 51: Webapps without the web

transaction

• Container for running SQL

• Queues executeSql

• Rolls back on error

Page 52: Webapps without the web

db.transaction(function (tx) { tx.executeSql('CREATE TABLE foo (id unique, text)'); tx.executeSql('INSERT INTO foo VALUES (1, "foobar")');});

Page 53: Webapps without the web

db.transaction(function (tx) { tx.executeSql('CREATE TABLE foo (id unique, text)'); tx.executeSql('INSERT INTO foo VALUES (1, "foobar")');});

Page 54: Webapps without the web

Database

1. create table & insert

2. drop table & (try) to insert

3. select count(*)

Queue

Page 55: Webapps without the web

Database1. create table & insert

2. drop table & (try) to insert

3. select count(*)

Queue

Page 56: Webapps without the web

Database1. create table & insert

2. drop table & (try) to insert

3. select count(*)

Queue

Page 57: Webapps without the web

Database1. create table & insert

2. drop table & (try) to insert

3. select count(*)

Queue

Page 58: Webapps without the web

Database1. create table & insert

2. drop table & (try) to insert

3. select count(*)

Queue

Page 59: Webapps without the web

Database1. create table & insert

3. select count(*)

Queue

Page 60: Webapps without the web

Database1. create table & insert

3. select count(*)

Queue

Page 61: Webapps without the web

Database1. create table & insert

3. select count(*)

Queue

Page 62: Webapps without the web

executeSql

• Both for read & write

Page 63: Webapps without the web

executeSql

• Both for read & write

• Injection protection

Page 64: Webapps without the web

executeSql

• Both for read & write

• Injection protection

• Callback gives results

Page 65: Webapps without the web

db.transaction(function (tx) { tx.executeSql('SELECT * FROM foo');});

Page 66: Webapps without the web

db.transaction(function (tx) { tx.executeSql('SELECT * FROM foo');});

Page 67: Webapps without the web

db.transaction(function (tx) { tx.executeSql('SELECT * FROM foo');});

Page 68: Webapps without the web

tx.executeSql( 'INSERT INTO foo VALUES (1, "foo")');

Page 69: Webapps without the web

tx.executeSql( 'INSERT INTO foo VALUES (?, ?)', [id, userVariable]);

Page 70: Webapps without the web

tx.executeSql( 'SELECT * FROM foo', [], function (tx, results) { var len = results.rows.length; for (var i = 0; i < len; i++) { alert(results.rows.item(i).text); } });

Page 71: Webapps without the web

tx.executeSql( 'SELECT * FROM foo', [], function (tx, results) { var len = results.rows.length; for (var i = 0; i < len; i++) { alert(results.rows.item(i).text); } });

Page 72: Webapps without the web

tx.executeSql( 'SELECT * FROM foo', [], function (tx, results) { var len = results.rows.length; for (var i = 0; i < len; i++) { alert(results.rows.item(i).text); } });

Page 73: Webapps without the web

tx.executeSql( 'SELECT * FROM foo', [], function (tx, results) { var len = results.rows.length; for (var i = 0; i < len; i++) { alert(results.rows.item(i).text); } });

Page 74: Webapps without the web
Page 75: Webapps without the web

Offline

Page 76: Webapps without the web

Offline

Page 77: Webapps without the web

Offline Apps

• Application cache / manifest

• Events: offline, online

• navigator.onLine property

Page 78: Webapps without the web

navigator.onLine

Page 79: Webapps without the web

http://icanhaz.com/rubiks

Page 80: Webapps without the web

http://icanhaz.com/rubiks

Page 81: Webapps without the web

Using a Manifest<!DOCTYPE html>

<html manifest="my.manifest">

<body>

<!-- my page -->

</body>

</html>

Page 82: Webapps without the web

CACHE MANIFEST

app.html

css/style.css

js/app.js

#version 13

my.manifest

Page 83: Webapps without the web

The Manifest

1. Serve as text/manifest, by adding to mime.types:

text/cache-manifest manifest

Page 84: Webapps without the web

The Manifest

2. First line must be:

CACHE MANIFEST

Page 85: Webapps without the web

The Manifest

3. Including page is implicitly included in the cache.

Page 86: Webapps without the web

The Manifest

4. Two futher namespaces: NETWORK & FALLBACK

FALLBACK:/ offline.html

Page 87: Webapps without the web

The Manifest

5. Include some versioning to cache bust your manifest

# version 16

Page 88: Webapps without the web

The process

Page 89: Webapps without the web

Browser: request Server: serve allBrowser: I have a manifest, cache

assets

Server: serve manifest assets

Browser: applicationCache

updatedBrowser: reload

Browser: serve locally

Browser: only request manifest

file

Server: 304 Not Modified

Page 90: Webapps without the web

Browser: request Server: serve allBrowser: I have a manifest, cache

assets

Server: serve manifest assets

Browser: applicationCache

updatedBrowser: reload

Browser: serve locally

Browser: only request manifest

file

Server: 304 Not Modified

Page 91: Webapps without the web

Browser: request Server: serve allBrowser: I have a manifest, cache

assets

Server: serve manifest assets

Browser: applicationCache

updatedBrowser: reload

Browser: serve locally

Browser: only request manifest

file

Server: 304 Not Modified

Page 92: Webapps without the web

Browser: request Server: serve allBrowser: I have a manifest, cache

assets

Server: serve manifest assets

Browser: applicationCache

updatedBrowser: reload

Browser: serve locally

Browser: only request manifest

file

Server: 304 Not Modified

Page 93: Webapps without the web

Browser: request Server: serve allBrowser: I have a manifest, cache

assets

Server: serve manifest assets

Browser: applicationCache

updatedBrowser: reload

Browser: serve locally

Browser: only request manifest

file

Server: 304 Not Modified

Page 94: Webapps without the web

Browser: request Server: serve allBrowser: I have a manifest, cache

assets

Server: serve manifest assets

Browser: applicationCache

updatedBrowser: reload

Browser: serve locally

Browser: only request manifest

file

Server: 304 Not Modified

Page 95: Webapps without the web

Browser: request Server: serve allBrowser: I have a manifest, cache

assets

Server: serve manifest assets

Browser: applicationCache

updatedBrowser: reload

Browser: serve locally

Browser: only request manifest

file

Server: 304 Not Modified

Page 96: Webapps without the web

Browser: request Server: serve allBrowser: I have a manifest, cache

assets

Server: serve manifest assets

Browser: applicationCache

updatedBrowser: reload

Browser: serve locally

Browser: only request manifest

file

Server: 304 Not Modified

Page 97: Webapps without the web

Browser: request Server: serve allBrowser: I have a manifest, cache

assets

Server: serve manifest assets

Browser: applicationCache

updatedBrowser: reload

Browser: serve locally

Browser: only request manifest

file

Server: 304 Not Modified

Page 98: Webapps without the web

Browser: request Server: serve allBrowser: I have a manifest, cache

assets

Server: serve manifest assets

Browser: applicationCache

updatedBrowser: reload

Browser: serve locally

Browser: only request manifest

file

Server: 304 Not Modified

Problem:Change of contentrequires 2 refreshes

Page 99: Webapps without the web

document.body.onOnline = function () { // fire an update to the cache applicationCache.update();};

Page 100: Webapps without the web

document.body.onOnline = function () { // fire an update to the cache applicationCache.update();};

applicationCache.onUpdateReady = function () { applicationCache.swapCache(); notice('reload');};

Page 101: Webapps without the web

APIs of the Future

Page 102: Webapps without the web

Notification API

Page 103: Webapps without the web
Page 104: Webapps without the web

if (webkitNotifications.checkPermission() == 0) { webkitNotifications.createNotification ➥ (profile_image_url, name, text).show();}

Page 105: Webapps without the web

File API

Page 106: Webapps without the web
Page 107: Webapps without the web

Device API

Page 108: Webapps without the web

Very new, collection of specifications

Page 109: Webapps without the web

Camera API

Page 110: Webapps without the web

Camera API

Message API

Page 111: Webapps without the web

Camera API

Message API

PIM API(calendar, contacts, tasks)

Page 112: Webapps without the web

Camera API

Message API

PIM API(calendar, contacts, tasks)File System API

Page 113: Webapps without the web

Camera API

Message API

PIM API(calendar, contacts, tasks)File System API

App Launcher API

Page 114: Webapps without the web

Camera API

Message API

PIM API(calendar, contacts, tasks)File System API

App Launcher API

App Configuration API

Page 115: Webapps without the web

Camera API

Message API

PIM API(calendar, contacts, tasks)File System API

App Launcher API

App Configuration API

Communication Log API

Page 116: Webapps without the web

Camera API

Message API

PIM API(calendar, contacts, tasks)File System API

App Launcher API

App Configuration API

Communication Log API

Gallery API

Page 117: Webapps without the web

Camera API

Message API

PIM API(calendar, contacts, tasks)File System API

App Launcher API

App Configuration API

Communication Log API

Gallery API

Notifications API

Page 118: Webapps without the web

Notifications API

Camera API

Message API

PIM API(calendar, contacts, tasks)File System API

App Launcher API

App Configuration API

Communication Log API

Gallery APIAPI-tastic!


Top Related