67
Beyond Cookies: Persistent Storage for Web Applications Brad Neuberg Google

Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Embed Size (px)

DESCRIPTION

Presentation on client-side storage options, including HTML 5, Dojo Storage, and Gears.

Citation preview

Page 1: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Beyond Cookies: Persistent Storage for

Web Applications

Brad NeubergGoogle

Page 2: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

What?

Page 3: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

What?

4K

Page 4: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

What?

4K100K

Page 5: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

What?

4K100K500K

Page 6: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

What?

4K100K500K>1 MB

Page 7: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

What?

• Name/Value Storage

• Database

• Static Files

Page 8: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Why?

“640K ought to be enough for anybody”

Page 9: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Why?

“640K ought to be enough for anybody”

Page 10: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Why?

• Offline

• Capabilities of client

• Lower Latency

• No server

• Performance

• Privacy

Page 11: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Demo of GMail Offline

Page 12: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Zoo of Options

HTML 5

Yahoo!BrowserPlus

Page 13: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Zoo of Options

HTML 5

Page 14: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

HTML 5

Page 15: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Name/Value

• sessionStorage

• localStorage

Page 16: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

sessionStorage

• Per window

• Example: purchasing plane tickets

Page 17: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

sessionStorage

<label>

<input type="checkbox”

onchange="sessionStorage.insurance = checked">

I want insurance on this trip.

</label>

Page 18: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

sessionStorage

<label>

<input type="checkbox”

onchange="sessionStorage.insurance = checked">

I want insurance on this trip.

</label>

if (sessionStorage.insurance) { ... }

Page 19: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

localStorage

• Per site, multiple windows

• Permanent

• Example: storing user’s emails

Page 20: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

localStorage<p>You have viewed this page

<span id="count">an untold number of</span>

time(s).</p>

<script>

if (!localStorage.pageLoadCount)

localStorage.pageLoadCount = 0;

localStorage.pageLoadCount =

parseInt(localStorage.pageLoadCount, 10) + 1;

document.getElementById('count').textContent =

localStorage.pageLoadCount;

</script>

Page 21: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Storage Interfaceinterface Storage {

readonly attribute unsigned long length;

DOMString key(in unsigned long index);

DOMString getItem(in DOMString key);

void setItem(in DOMString key, in DOMString data);

void removeItem(in DOMString key);

void clear();

};

Page 22: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Storage Event

• Fired when storage happens

• Doesn’t bubble

• On Window

Page 23: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Support

• Firefox 2* & 3

• Internet Explorer 8

• Safari nightly

Page 24: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

*Note

Firefox 2 has older version of spec:

function getStorage() { if (typeof localStorage != 'undefined') return localStorage; else return globalStorage[window.location.hostname];}

Page 26: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

HTML 5 Database

Page 27: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

HTML 5 Database

• Full relational database

• SQLite

• Asynchronous APIs

Page 28: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

openDatabase

var db = openDatabase("MyDB", "1.0",

"Example", 200000);

openDatabase(dbName, version, displayName, expectedSize)

Page 29: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

CREATE TABLE

db.transaction(function(tx) {

tx.executeSql("CREATE TABLE IF NOT EXISTS"

+ "COUNTRY (id REAL UNIQUE, name TEXT)",

[], function(tx, result) {

// SQL executed; do more work

});

}, function(tx, error) {

alert(error.message);

return;

});

Page 30: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

SELECTdb.transaction(function(tx) {

tx.executeSql("SELECT id, name FROM COUNTRY",

[],

function(tx, result) {

for (var i = 0; i < result.rows.length; ++i) {

var row = result.rows.item(i);

alert('id='+row['id');

alert(' name='+row['name']);

}

}, function(tx, err) { alert(err.message); }

);

Page 31: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

INSERTdb.transaction(function(tx) {

tx.executeSql(

"INSERT INTO COUNTRY "

+ " (id, name) VALUES (?, ?)",

['1', 'United States']);

});

Page 32: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Support

• Safari 3.1

• iPhone OS 2.0+

Page 33: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

iPhone

Image: Chris Messina

Page 35: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

View Source

View Source of Database Demo

Page 36: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

HTML 5 Application Cache

Page 37: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Application Cache

• Ability to go offline

• Cache UI files (JavaScript, HTML, etc.)

Page 38: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Application Cache

• Point to manifest file:

<html manifest=”foo.manifest”>

Page 39: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Application Cache

• Point to manifest file:

<html manifest=”foo.manifest”>

• foo.manifest must have correct MIME type:

• text/cache-manifest

Page 40: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Application Cache

• Example manifest:

CACHE MANIFEST# v1# This is a comment.http://www.foo.com/index.htmlhttp://www.foo.com/header.pnghttp://www.foo.com/blah/blahsomethingElse.jpg

Page 41: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Application Cache

Page 42: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Application Cache

• Update process

Page 43: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Application Cache

• Update process

• Manifest file fetched

Page 44: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Application Cache

• Update process

• Manifest file fetched

• If changed

Page 45: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Application Cache

• Update process

• Manifest file fetched

• If changed

• Files refetched in temp cache

Page 46: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Application Cache

• Update process

• Manifest file fetched

• If changed

• Files refetched in temp cache

• When done, becomes real cache

Page 47: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Application Cache

• Update process

• Manifest file fetched

• If changed

• Files refetched in temp cache

• When done, becomes real cache

• Events are fired

Page 48: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Support

• Firefox 3* and 3.1

• Safari 3* and 3.1

• iPhone OS 2.1+

Page 49: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Demo

Page 50: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Demo

Page 51: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Dojo Storage

• Open source

• Figures out best storage

• Flash

• HTML 5

• Gears

Page 52: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Dojo Storage

• Name/value hashtable

• storage.js (~13K GZip)

• dojo.js (~26K)

• http://dojotoolkit.org

Page 53: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Dojo Storagedojox.storage.put(“message”, “hello world”, function(status, keyName){ if(status == dojox.storage.FAILED){ alert("No permission!"); }else if(status == dojox.storage.SUCCESS){ // do something } });

Page 54: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Dojo Storagevar message = dojox.storage.get(“message”);

Page 55: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Gears

Page 56: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

HTML

CSS

JavaScript

What is Gears?

Page 57: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

HTML

CSS

JavaScript

What is Gears?

Page 58: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

HTML

CSS

JavaScript

What is Gears?

Page 59: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

HTML

CSS

JavaScript

What is Gears?

Page 60: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

HTML

CSS

JavaScript

What is Gears?

Page 61: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

HTML

CSS

JavaScriptDatabase

Local Server

Worker Pool

Desktop API

Ajax++

Client-Side Search

Blobs

Page 62: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

HTML

CSS

JavaScript

Ajax++

Geolocation

File System API

Page 63: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Database APIvar db = google.gears.factory.create('beta.database');

db.open('database-test');

db.execute('CREATE TABLE IF NOT EXISTS Test' + ' (Phrase TEXT, Timestamp INT)');

db.execute('INSERT INTO Test VALUES (?, ?)', ['Monkey!', new Date().getTime()]);

Page 64: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Database APIvar rs;

try {rs = db.execute('SELECT * FROM Test ORDER BY Timestamp DESC');

while (rs.isValidRow()) { console.log(rs.fieldByName(‘Phrase’) + '@'

+ rs.fieldByName(‘Timestamp’)); rs.next();}

} finally {rs.close();db.close();

}

Page 65: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Local Server

Page 66: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Local Server

• Run web applications offline

• Capture UI: HTML, JavaScript, CSS

• Serves locally even when connected

Page 67: Beyond Cookies, Persistent Storage For Web Applications Web Directions North 2009

Beyond Cookies: Persistent Storage for

Web Applications

Brad NeubergGoogle