Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Your browser, my storage

Your browser, my storage

Complex applications need a persistent database to store, search and join data: until now a dedicated server was needed to do this, and no offline usage of the app was possible. With the introduction of HTML5 and the concept of localStorage and Web Databases, we don’t need an external server anymore: everything is stored within the user browser and thus the web app can be used offline as well as online.

Francesco Fullone

June 15, 2012
Tweet

More Decks by Francesco Fullone

Other Decks in Programming

Transcript

  1. Your browser, my storage a new approach on data storing

    (v.1.3) Francesco Fullone ff AT ideato.it
  2. Who am I Francesco Fullone aka Fullo - PHP developer

    since 1999 - President - and Evangelist - CEO @ - founder @ - Nerd and geek
  3. What we want is a lot of storage space, on

    the client, that persists beyond a page refresh and isn’t transmitted to the server. ~ M. Pilgrim
  4. Persistent local storage is one of the areas where client

    applications traditionally win against web applications.
  5. Cookies were introduced in HTTP/1.0, limited to only 20 per

    domain and 4KB each. http://qrurl.it/r/3l
  6. In 2002 Adobe created the Flash Cookies aka “Local Shared

    Objects” for Flash 6 Data storage increased to 100KB but it was difficult to be used
  7. With Flash 8, in 2006, Adobe introduced the ExternalInterface to

    allow Js to access to the stored resources.
  8. Between 2005 and 2007 dojox.storage was written by Brad Neuberg

    as a Js->Flash bridge to manage bigger chunks of data (with user prompt over 1MB).
  9. Google created Gears in 2007, that introduced a database paradigm

    (based on SQLite) to solve the storage problem.
  10. BUT

  11. It is enabled by a file .appcache that declares which

    resources have to be saved locally. (theoretically limited to 5MB).
  12. CACHE MANIFEST # Explicitly cached entries CACHE: index.html stylesheet.css images/logo.png

    http://www.anothersite.com/scripts/main.js # Resources that require the user to be online. NETWORK: login.php /myapi http://api.twitter.com # static.html will be served if main.php is inaccessible # offline.jpg will be served in place of all images in images/large/ FALLBACK: /main.php /static.html images/large/ images/offline.jpg .avi images/offline.jpg
  13. applicationCache can use events to trigger application behavior/2 – SetInterval(

    function () { window.applicationCache.update(); }, 3600000 ); – window.applicationCache.addEventListener( 'updateready', function(){ window.applicationCache.swapCache(); }, false );
  14. As stated in the specs: “window.navigator.onLine is inherently unreliable. A

    computer can be connected to a network without having Internet access.”
  15. If you change a resource and you don't update (!=rev)

    the .appcache file the browser may not download the new file! (yes! cached resources have priority on the online ones)
  16. Data storage is about capturing specific data, or resources the

    user has expressed interest in. http://qrurl.it/r/3n
  17. Web Storage is based on a structure of key-value pairs

    like any JavaScript object. localStorage.setItem("bar", foo);
  18. Web Storage can save up to 5MB but only as

    strings. So we have to force a casting if needed. var bar = parseInt(localStorage["bar"]);
  19. Web Storage should be local based or session based. var

    bar = localStorage["bar"]; var foo = sessionStorage["foo"];
  20. sessionStorage mantains a storage area that's available for the duration

    of the web session. Opening a new window/tab will create a new session.
  21. localStorage relies only on client, so we have to track

    changes and use storage.events to sync server and client if needed.
  22. Web SQL Database is WAS just an offline SQL implementation,

    based on SQLite. http://qrurl.it/r/3i
  23. this.db = openDatabase('geomood', '1.0', 'Geo', 8192); this.db.transaction(function(tx) { tx.executeSql("create table

    if not exists checkins(id integer primary key asc, time integer, latitude float, longitude float, mood string)", [], function() { console.log("siucc"); } » ); });
  24. But ... Web SQL Database is dead! as being dropped

    by W3C from 18/11/10 why bother more?
  25. IndexedDB allows to create an index on a certain field

    stored in a standard key->value mapping.
  26. IndexedDB is promoted by all browsers vendor, but is not

    yet fully supported by all Firefox 4, Chrome 11, have full implementation. Safari 5.1 and IE 10 will have
  27. var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.OIndexedDB

    || window.msIndexedDB, IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.OIDBTransaction || window.msIDBTransaction; Using IndexedDB/1
  28. var dbVersion = 1; var request = indexedDB.open("MyDb", dbVersion); request.onsuccess

    = function (event) { console.log("creating/accessing IndexedDB db"); db = request.result; } Using IndexedDB/2
  29. var transaction = db.transaction(["todo"], IDBTransaction.READ_WRITE); var store = transaction.objectStore("todo"); var

    data = { "text": “text to be saved”, "timeStamp": new Date().getTime() }; var put = store.put(data); Using IndexedDB/3
  30. // Get everything in the store; var keyRange = IDBKeyRange.lowerBound(0);

    var cursorRequest=store.openCursor(keyRange); cursorRequest.onsuccess = function(e) { var results = e.target.result; if(!!results == false) return; /* DO SOMETHING */ results.continue(); }; Using IndexedDB/4
  31. FileAPI allows to manipulate file objects, as well as programmatically

    select them and access their data. http://qrurl.it/r/3k
  32. File API includes FileReader and FileWriter APIs. Actually is supported

    by Chrome, Firefox > 3.6, Safari > 5.1, Opera > 11.1.
  33. Detect if the storing feature is supported by the browser

    (with modernizr), otherwise degradate to something else. (ie. dojox.storage)
  34. Do not exceed in storing data, you can store binary

    data base64 encoded but remember the pitfalls in performance.
  35. ?