Top Banner
Offline-First Solutions Glynn Bird, Developer Advocate, IBM Cloud Data Services @glynn_bird
52

Offline first solutions highland web group - december 2015

Apr 07, 2017

Download

Internet

Glynn Bird
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Offline first solutions   highland web group - december 2015

Offline-First Solutions

Glynn Bird, Developer Advocate, IBM Cloud Data Services

@glynn_bird

Page 2: Offline first solutions   highland web group - december 2015

2

Page 3: Offline first solutions   highland web group - december 2015

3

Page 4: Offline first solutions   highland web group - december 2015

4

Page 5: Offline first solutions   highland web group - december 2015

5

Page 6: Offline first solutions   highland web group - december 2015

Image Credit: Joan Touzet (@wohali), ASF Member, CouchDB PMC Member

6

Page 7: Offline first solutions   highland web group - december 2015

Frameworks

7

Page 8: Offline first solutions   highland web group - december 2015

Image Credit: Device landscape by Jeremy Keith, on Flickr

8

Not just mobile first…

Page 9: Offline first solutions   highland web group - december 2015

Image Credit: NASA New Horizons

9

Offline First

Offline-First

Page 10: Offline first solutions   highland web group - december 2015

Offline, online and somewhere in-between

10

Page 11: Offline first solutions   highland web group - december 2015

The Eight Fallacies of Distributed Computing 1.  The network is reliable 2.  Latency is zero

3.  Bandwidth is infinite

4.  The network is secure

5.  Topology doesn't change

6.  There is one administrator

7.  Transport cost is zero

8.  The network is homogeneous

11

Text Credit: The Eight Fallacies of Distributed Computing by Peter Deutsch | Image Credit: Pneumatic Central by Sleestak, on Flickr

Page 12: Offline first solutions   highland web group - december 2015

12

Offline-first is the only way to achieve a true, 100%

always-on user experience.*

*assuming the device is reliable

Page 13: Offline first solutions   highland web group - december 2015

Benefits of Offline First

13

•  Better, faster user experience, both offline and online •  Allow your users to work offline or with limited connectivity •  Potentially saves battery life and bandwidth usage

Page 14: Offline first solutions   highland web group - december 2015

Offline Patterns & Anti-Patterns •  Don't return an error for no reason •  Do let users view cached/saved data

•  Do synchronize data when connected

•  Consider letting your users decide when to sync

•  Think about the UX of users seeing stale data

14

Page 15: Offline first solutions   highland web group - december 2015

Difficulties of Offline First

15

Image credit http://www.sneakerheadvc.com/wp-content/uploads/2012/02/Apple_iSync1.png

Page 16: Offline first solutions   highland web group - december 2015

Introducing PouchDB

Page 17: Offline first solutions   highland web group - december 2015

PouchDB •  A database in your web browser •  Can synchronize with any

database that implements the CouchDB Replication Protocol

•  Makes create, read, update and delete operations extremely quickly

•  Free, open-source

17

Page 18: Offline first solutions   highland web group - december 2015

JSON Documents

18

{ type: "Feature", geometry: { type: "Point", coordinates: [ -71.1028, 42.3691 ] }, properties: { session_id: "3486b13f9b82800e367f", timestamp: 1422928591717 } }

Limited data types •  string

•  number

•  boolean

•  array

•  object

Page 19: Offline first solutions   highland web group - december 2015

Getting started with PouchDB

19

<script src="https://cdn.jsdelivr.net/pouchdb/3.6.0/pouchdb.min.js"></script> <script type="javascript">

var db = new PouchDB("smart-meter"); </script>

Page 20: Offline first solutions   highland web group - december 2015

Adding documents - callbacks

20

db.post({ date: "2014-11-12T23:27:03.794Z",

kilowatt_hours: 14

}, function(err, data) {

console.log(err,data); });

Page 21: Offline first solutions   highland web group - december 2015

Adding documents - promises

21

db.post({ date: "2014-11-12T23:27:03.794Z",

kilowatt_hours: 14

}).then(function() {

console.log("Document created"); }).catch(function(error) {

console.log(error);

});

Page 22: Offline first solutions   highland web group - december 2015

Adding documents - bring your own id

22

var db = new PouchDB("smart-meter"); var obj = {

_id: "abc123",

timestamp: "2014-11-12T23:27:03.794Z",

kilowatt_hours: 14 };

db.put(obj, callback);

https://github.com/bradley-holt/offline-first/blob/master/pouchdb/04-create-document-put.js

Page 23: Offline first solutions   highland web group - december 2015

Getting a document

23

db.get("abc123", callback);

// calls back with // {

// _id: "abc123", // _rev: "1-27695d5f483ac267d03ad0e3cb54bd2c",

// timestamp: "2014-11-12T23:27:03.794Z",

// kilowatt_hours: 14

// }

Page 24: Offline first solutions   highland web group - december 2015

Getting multiple documents

24

db.allDocs({include_docs:true}, callback);

// calls back with // {

// "offset": 0, // "total_rows": 24,

// "rows": [{...},{...}]

// }

Page 25: Offline first solutions   highland web group - december 2015

Querying

25

•  Primary Index •  MapReduce

•  PouchDB-find plugin

•  PouchDB-quick search plugin

Page 26: Offline first solutions   highland web group - december 2015

Querying a Database with PouchDB Find •  Based on Cloudant Query (Mango) •  MongoDB-style query language

26

Image Credit: Mango with section on a white background by bangdoll, on Flickr

db.find({

selector:{

name:'Mario'

debut:{'$gt':1990}

},

fields:['_id','lastname'],

sort:['lastname']

})...

Page 27: Offline first solutions   highland web group - december 2015

Replicating a PouchDB Database

27

var db = new PouchDB("smart-meter");

var remoteDb = new PouchDB("https://bradley-holt.cloudant.com/smart-meter");

db.replicateTo(remoteDb);

https://github.com/bradley-holt/offline-first/blob/master/pouchdb/08-replicate-database.js

Page 28: Offline first solutions   highland web group - december 2015

Bidirectionally Replicating a PouchDB Database

28

db.sync(remoteDb).on("change", function(info) {

// Replication has written a new document

console.log(info);

}).on("complete", function(info) {

// Replication has completed or been cancelled

console.log(info);

});

https://github.com/bradley-holt/offline-first/blob/master/pouchdb/09-replicate-database-bidirectional.js

Page 29: Offline first solutions   highland web group - december 2015

Listening for Database Changes

29

var changes = remoteDb.changes({

since: "now"

}).on("change", function(change) {

// A document has changed

console.log(change);

}).on("complete", function(info) {

// changes() was canceled

console.log(info);

});

https://github.com/bradley-holt/offline-first/blob/master/pouchdb/11-database-changes.js

Page 30: Offline first solutions   highland web group - december 2015

PouchDB Framework Adapters

30

Page 31: Offline first solutions   highland web group - december 2015

Apache CouchDB

Page 32: Offline first solutions   highland web group - december 2015

Apache CouchDB •  JSON document database •  HTTP API

•  Master-master replication

•  Free, open-source

32

Page 33: Offline first solutions   highland web group - december 2015

IBM Cloudant •  Globally distributed data layer for

web and mobile applications •  Run as-a-service •  MongoDB-style queries

•  Advanced geospatial capabilities

•  Full text search indexing

33

Page 34: Offline first solutions   highland web group - december 2015

PouchDB and CouchDB Replication

34

Page 35: Offline first solutions   highland web group - december 2015

CouchDB Replication Protocol •  One-off, one way operation •  Peer-to-peer (masterless)

•  Incremental

•  Conflict detection

35

Page 36: Offline first solutions   highland web group - december 2015

Going Offline

Page 37: Offline first solutions   highland web group - december 2015

HTML5 Offline Application Cache •  Enables fully-functional offline

web apps

•  Stores files and assets for offline browsing

•  Makes page loads very fast, even when online

37

Page 38: Offline first solutions   highland web group - december 2015

Cache Manifest File

38

<html manifest="example.appcache"> … </html>

CACHE MANIFEST # v1 - 2015-01-08 index.html logo.png app.css app.js

Page 39: Offline first solutions   highland web group - december 2015

Service Workers (Beta)

39

Client-side scripting framework for • programmable cache

• sync

• push messaging

• geo-fencing

• background tasks

https://jakearchibald.github.io/isserviceworkerready/

Page 40: Offline first solutions   highland web group - december 2015

Cross-Origin Resource Sharing (CORS) •  Enable Cross-Origin Resource

Sharing (CORS) on remote database

•  Browsers place security restrictions on cross-site HTTP requests

•  If you run into a problem, remember this warning!

40

Image Credit: Grunge Warning Sign - Do Not Read This Sign by Nicolas Raymond, on Flickr

Page 41: Offline first solutions   highland web group - december 2015

Hybrid or Responsive Mobile Web Apps

Page 42: Offline first solutions   highland web group - december 2015

Hybrid Mobile Web Apps •  Native mobile web apps built with

HTML5, CSS and JavaScript

•  Good for: •  Fully-featured, cross-platform native apps

•  High-fidelity prototypes

42

Page 43: Offline first solutions   highland web group - december 2015

Responsive Mobile Web Apps •  HTML5, CSS and JavaScript mobile

web apps

•  Responsive design

•  Enhanced to enable offline usage

43

Page 44: Offline first solutions   highland web group - december 2015

Examples

Page 45: Offline first solutions   highland web group - december 2015

Pro Forma •  Define fields you want to collect •  Renders form saving data to

PouchDB

•  Replicates data to Cloudant

•  Demo https://glynnbird.github.io/proforma/

45

Page 46: Offline first solutions   highland web group - december 2015

MD •  Offline word processor •  Saves Markdown documents to

PouchDB

•  Replicates data to Cloudant

•  Demo http://mddoc.mybluemix.net/

46

Page 47: Offline first solutions   highland web group - december 2015

Gutenberg •  Offline e-book reader •  Replicates book list from server

•  Each book is a Cloudant database

•  Demo http://glynnbird.github.io/gutenberg/

47

Page 48: Offline first solutions   highland web group - december 2015

48

www.glynnbird.com •  My home page •  Cloudant database of articles

•  Replicated to PouchDB

•  Appcache for offline first

•  http://www.glynnbird.com/

48

Page 49: Offline first solutions   highland web group - december 2015

49 49

Volt •  Password vault in a Chrome

extension

•  Data stored in encrypted in PouchDB

•  Optional back to CouchDB/Cloudant

•  https://github.com/glynnbird/volt

49

Page 50: Offline first solutions   highland web group - december 2015

Location Tracker •  Stores data locally in PouchDB •  Front end built with AngularJS

•  Authentication logic built with Node.js

•  User interface built with Leaflet

•  Replicates location data to Cloudant

•  More info: https://cloudant.com/location-tracker/

50

Page 51: Offline first solutions   highland web group - december 2015

Glynn Bird Developer Advocate, Cloud Data Services [email protected] @glynn_bird github.com/glynnbird www.glynnbird.com

Page 52: Offline first solutions   highland web group - december 2015

Image Credits

52

•  Joan Touzet (@wohali), ASF Member, CouchDB PMC Member <https://twitter.com/wohali/status/595689720933445632>

•  Device landscape by Jeremy Keith, on Flickr <https://www.flickr.com/photos/adactio/6153481666>

•  NASA New Horizons <https://www.nasa.gov/sites/default/files/thumbnails/image/nh-surface.jpg>

•  Pneumatic Central by Sleestak, on Flickr <https://www.flickr.com/photos/dlanod/235990854>

•  Mango with section on a white background by bangdoll, on Flickr <https://www.flickr.com/photos/bangdoll/5665235102>

•  Grunge Warning Sign - Do Not Read This Sign by Nicolas Raymond, on Flickr <https://www.flickr.com/photos/80497449@N04/7417352980>