Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

your browser, my storage

Complex applications need a persistent database to store, search and join data: till 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 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.

  • Be the first to comment

your browser, my storage

  1. Your browser, my storagea new approach on data storing (v.1.3)Francesco Fulloneff AT
  2. Who am IFrancesco 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, onthe client, that persists beyond a pagerefresh 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. A jump in the past
  6. Cookies were introduced inHTTP/1.0, limited to only 20 per domain and 4KB each.
  7. Cookies are sent to and from client at any connection.
  8. Microsoft with Internet Explorer 6 introduced dHTML and theuserData API to store up to 64KB of data
  9. Mozilla introduced with Firefox 2the DOM Storage API, it will then know as Web Storage.
  10. In 2002 Adobe created the Flash Cookies aka “Local Shared Objects” for Flash 6Data storage increased to 100KB but it was difficult to be used
  11. With Flash 8, in 2006, Adobe introduced the ExternalInterface to allow Js to accessto the stored resources.
  12. Between 2005 and 2007 was written by Brad Neuberg as a Js->Flashbridge to manage bigger chunks of data (with user prompt over 1MB).
  13. Google created Gears in 2007, that introduced a databaseparadigm (based on SQLite) to solve the storage problem.
  14. BUT
  15. All these storage systems had different APIs, a commonplatform is needed by all the browser vendors.
  16. The two approaches of storing: Application Cache Offline storage
  17. Application Caching involvessaving the applications core logic and user-interface.
  18. It is enabled by a file .appcache that declares which resources have to be saved locally. (theoretically limited to 5MB).
  19. CACHE MANIFEST# Explicitly cached entriesCACHE:index.htmlstylesheet.cssimages/logo.png Resources that require the user to be online.NETWORK:login.php/myapi 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.htmlimages/large/ images/offline.jpg.avi images/offline.jpg
  20. applicationCache can use events to trigger application behavior window.applicationCache.onchecking = function(e) { log("Checking for application update"); }
  21. applicationCache or check if the browser is online If (window.navigator.onLine) { log("Application is online"); }
  22. Chrome/Chromium doesnt supportwindow.navigator.onLine attribute and... It doesnt have a real offline mode!
  23. As stated in the specs:“window.navigator.onLine is inherentlyunreliable. A computer can be connected to a network without having Internet access.”
  24. If you change a resource and you dont update (rev) the .appcache file the browser may not download the new file!(yes! cached resources have priority on the online ones)
  25. Data storage is about capturing specific data, or resources the user has expressed interest in.
  26. Approaches toData Storage
  27. Web Storage is the simplerimplementation of the Data Storage paradigm.
  28. Web Storage is based on astructure of key-value pairs like any JavaScript object.localStorage.setItem("bar", foo);
  29. Web Storage can save up to 5MBbut only as strings. So we have to force a casting if needed.var bar = parseInt(localStorage["bar"]);
  30. Web Storage should be local based or session based. var bar = localStorage["bar"];var foo = sessionStorage["foo"];
  31. sessionStorage mantains astorage area thats available forthe duration of the web session. Opening a new window/tab will create a new session.
  32. localStorage relies only on client, so we have to trackchanges and use to sync server and client if needed.
  33. Web SQL Database is WAS justan offline SQL implementation, based on SQLite.
  34. 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"); } » );});
  35. Web SQL Database is not supported by Microsoft andMozilla, it is on browsers based on webkit.
  36. But ...Web SQL Database is dead! as being dropped by W3C from 18/11/10 why bother more?
  37. Web SQLDatabase is theonly databasestorage engine that works onmobile devices!
  38. IndexedDB is a nice compromise between Web Storage and Web SQL Database.
  39. IndexedDB allows to create anindex on a certain field stored in a standard key->value mapping.
  40. IndexedDB is promoted by allbrowsers vendor, but is not yet fully supported by all Firefox 4, Chrome 11, have full implementation. Safari 5.1 and IE 10 will have
  41. Using IndexedDB/1var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.OIndexedDB || window.msIndexedDB,    IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.OIDBTransaction || window.msIDBTransaction;
  42. Using IndexedDB/2var dbVersion = 1;var request ="MyDb", dbVersion);request.onsuccess = function (event) {    console.log("creating/accessing IndexedDB db");    db = request.result;}
  43. Using IndexedDB/3var 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);
  44. Using IndexedDB/4  // Get everything in the store;var keyRange = IDBKeyRange.lowerBound(0);var cursorRequest=store.openCursor(keyRange);cursorRequest.onsuccess = function(e) {  var results =;  if(!!results == false) return;  /* DO SOMETHING */  results.continue();};
  45. FileAPI allows to manipulate file objects, as well as programmatically select them and access their data.
  46. File API includes FileReader and FileWriter APIs. Actually is supported by Chrome, Firefox > 3.6, Safari > 5.1, Opera > 11.1.
  47. filer.js
  48. First steps on offline storage development.
  49. Storages Status/1
  50. Storages Status/2
  51. Storages Status/3
  52. Detect if the storing feature is supported by the browser (withmodernizr), otherwise degradate to something else. (ie.
  53. Use libraries that manage data for you (ie. storagejs, lawnchair)
  54. against lost data, sync automatically.
  55. Automatically detect when users are online.
  56. Do not exceed in storing data,you can store binary data base64 encoded but remember the pitfalls in performance.
  57. Avoid race conditions.If possible use WebSQL to use its transactions features.
  58. use local storage to help yourapplication to become faster.
  59. basket.js
  60. basket    .require(jquery.js)    .require(underscore.js)    .require(app.js).wait(function(){ • /* do something cool */ });
  61. ?
  62. jsDay + phpDay 2012 16-19 Maggio 2012 Verona
  63. Francesco Fullone @fullo via Quinto Bucci 205 47023 Cesena (FC) info AT