61
Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix OSCON Wednesday, July 22, 2015 Bradley Holt, Cloudant Developer Advocate @BradleyHolt

Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Embed Size (px)

Citation preview

Page 1: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

OSCON

Wednesday, July 22, 2015

Bradley Holt, Cloudant Developer Advocate

@BradleyHolt

Page 2: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

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

2

Page 3: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Image Credit: Device landscape by Jeremy Keith, on Flickr

3

Not just mobile first…!

Page 4: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

4

Image Credit: Cloud Formation Over the Adirondacks by Bradley Holt

Offline First!Because being offline shouldn't be an error condition.!

Page 5: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

5

Doesn't ubiquitous connectivity make offline-

enabled apps unnecessary?!

Page 6: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

6

No.!

Page 7: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

7

Quite the opposite, in fact.!

Page 8: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

8

Ubiquitous connectivity is driving the demand for

offline capabilities.!

Page 9: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

9

How?!

Page 10: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

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

10

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

Page 11: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

11

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

always-on user experience.*!*assuming the device is reliable!

Page 12: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Benefits of Offline First

12

•  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 13: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Offline Patterns & Anti-Patterns •  Don't return an error for no reason

•  Do let users view cached/saved data

•  Do synchronize data when connected

13

Page 14: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Offline Patterns & Anti-Patterns •  Don't return an error for no reason

•  Do let users view cached/saved data

•  Do synchronize data when connected

13

Page 15: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Offline Patterns & Anti-Patterns •  Don't return an error for no reason

•  Do let users view cached/saved data

•  Do synchronize data when connected

13

Page 16: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Offline Patterns & Anti-Patterns •  Don't return an error for no reason

•  Do let users view cached/saved data

•  Do synchronize data when connected

13

Page 17: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Introducing PouchDB

Page 18: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

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 fast

15

Page 19: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

JSON Documents

16

{ _id: "6EF9D2B0-13D3-1378-8D30-39E3CE0B36C2", _rev: "1-0b457efcf82fb29492ef927ba5b6ee15", type: "Feature", geometry: { type: "Point", coordinates: [ -71.1028, 42.3691 ] }, properties: { session_id: "3486b13f-7b8a-8a96-dfbf-9b82800e367f", timestamp: 1422928591717 } }

Page 20: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

PouchDB Framework Adapters

17

Page 21: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Responsive Mobile Web Apps or Hybrid Mobile Web Apps

Page 22: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Responsive Mobile Web Apps •  HTML5, CSS and JavaScript mobile

web apps

•  Responsive design

•  Enhanced to enable offline usage

19

Page 23: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

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

20

Page 24: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Apache CouchDB

Page 25: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Apache CouchDB •  JSON document database

•  HTTP API

•  Master-master replication

22

Page 26: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

IBM Cloudant •  Globally distributed data layer for

web and mobile applications

•  MongoDB-style queries

•  Advanced geospatial capabilities

•  Full text search indexing

23

Page 27: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

PouchDB and Cloudant Replication

24

Page 28: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

CouchDB Replication Protocol •  One-off, one way operation

•  Peer-to-peer (masterless)

•  Incremental

•  Conflict detection

25

Page 29: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Apache Cordova, PhoneGap and Ionic

Page 30: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Apache Cordova and PhoneGap

27

Image Credit: build.phonegap by Andrés Álvarez Iglesias, on Flickr

Page 31: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Ionic •  Mobile-optimized HTML, CSS

and JavaScript components

•  Builds on Apache Cordova

•  Utilizes AngularJS

•  CLI installable via npm

28

Page 32: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Using Cordova and Ionic $ npm install -g cordova ionic"

$ ionic start energy-monitor tabs"

$ cd energy-monitor"

$ ionic platform add ios"

$ ionic build ios"

$ ionic emulate ios"

29

Image Credit: Getting Started with Ionic

Page 33: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

HTML5 Offline Application Cache

Page 34: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

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

31

Page 35: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Cache Manifest File

32

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

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

Page 36: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Deploying a Web App to IBM Bluemix

Page 37: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

About IBM Bluemix •  Platform-as-a-Service (PaaS)

based on Cloud Foundry

•  Catalog of boilerplates, runtimes, and services from which you can compose your applications

•  Cloudant is one of the many services available

•  Sign up at: https://bluemix.net/

34

Page 38: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Bluemix Runtimes •  IBM buildpacks

•  Java

•  Node.js

•  Community buildpacks •  Go

•  PHP

•  Python

•  Ruby

•  More…

35

Page 39: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Data Management and Big Data Services •  Cloudant

•  BigInsights for Apache Hadoop

•  IBM Analytics for Apache Hadoop

•  Swift Object Storage

•  More…

36

Page 40: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

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/

37

Page 41: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

38

Location Tracker Part 3: https://cloudant.com/location-tracker/

Page 42: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

PouchDB Code Examples

Page 43: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Installing PouchDB

40

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

CACHE MANIFEST # v2 - 2015-01-08 index.html logo.png app.css app.js https://cdn.jsdelivr.net/pouchdb/3.5.0/pouchdb.min.js

Page 44: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

PouchDB Code Examples

41

•  Creating a local PouchDB database •  Creating a remote PouchDB database

•  Creating a new document

•  Updating a document •  Querying a database

•  Replicating PouchDB

Page 45: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Creating a Local PouchDB Database

42

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

https://github.com/bradley-holt/offline-first/blob/master/pouchdb/01-create-local-database.js

Page 46: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Creating a Remote PouchDB Database

43

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

https://github.com/bradley-holt/offline-first/blob/master/pouchdb/02-create-remote-database.js

Page 47: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

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!

44

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

Page 48: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Creating a New Document

45

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

db.put({"

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

kilowatt_hours: 14"

}).then(function() {"

console.log("Document created");"

}).catch(function(error) {"

console.log(error);"

});"

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

Page 49: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Updating a Document

46

db.put({"

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

kilowatt_hours: 14"

}).then(function() {"

return db.get("2014-11-12T23:27:03.794Z");"

}).then(function(doc) {"

// Update the value for kilowatt hours"

doc.kilowatt_hours = 15;"

// Put the document back to the database"

return db.put(doc);"

}).catch(function(error) {"

console.log(error);"

});"https://github.com/bradley-holt/offline-first/blob/master/pouchdb/05-update-document.js

Page 50: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Querying a Database with allDocs"

47

db.bulkDocs(["

{_id: "2014-11-12T23:27:03.794Z", kilowatt_hours: 14},"

{_id: "2014-11-13T00:52:01.471Z", kilowatt_hours: 15},"

{_id: "2014-11-13T01:39:28.911Z", kilowatt_hours: 16},"

{_id: "2014-11-13T02:52:01.471Z", kilowatt_hours: 17}"

]).then(function(result) {"

// Get all documents"

return db.allDocs({include_docs: true});"

}).then(function(response) {"

console.log(response);"

}).catch(function(error) {"

console.log(error);"

});"https://github.com/bradley-holt/offline-first/blob/master/pouchdb/07-query-database-all-docs.js

Page 51: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

allDocs Options •  include_docs"

•  conflicts"•  attachments"

•  startkey"•  endkey"•  inclusive_end

(true by default)

•  limit"•  skip"•  descending"•  key"•  keys"

48

Page 52: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Querying a Database with Map/Reduce

49

•  Most queries can be done with allDocs (in PouchDB) •  Map functions transform documents into indexes

•  Reduce functions aggregate results of Map functions •  _sum"•  _count"•  _stats"

Page 53: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Querying a Database with PouchDB Find •  Based on Cloudant Query (Mango)

•  MongoDB-style query language

•  Define fields to index

50

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

Page 54: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Bidirectionally Replicating a PouchDB Database

51

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

var remoteDb = new PouchDB("

"https://bradley-holt.cloudant.com/smart-meter""

);"

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

Page 55: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Bidirectionally Replicating a PouchDB Database

52

Promise.all(["

db.bulkDocs(["

{_id: "2014-11-12T23:27:03.794Z", kilowatt_hours: 14},"

{_id: "2014-11-13T00:52:01.471Z", kilowatt_hours: 15}"

]),"

remoteDb.bulkDocs(["

{_id: "2014-11-11T22:35:01.433Z", kilowatt_hours: 11},"

{_id: "2014-11-12T00:43:01.633Z", kilowatt_hours: 13}"

])"

]).then(function() {"

…"

});"

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

Page 56: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Bidirectionally Replicating a PouchDB Database

53

db.sync(remoteDb, {"

live: false,"

retry: false"

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

// Replication has written a new document"

console.log(info);"

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

// Replication has complete or been cancelled"

console.log(info);"

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

// Replication has stopped due to an unrecoverable failure"

console.log(error);"

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

Page 57: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

54

Page 58: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

A Deep Dive into Offline-First with PouchDB and IBM Cloudant Date: Thursday, August 27, 2015 Time: 1:00 PM - 2:00 PM EDT

Register at: https://cloudant.com/resources/webinars/

55

Page 59: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Visit IBM at booth #501 to talk to an IBM

Recruiter @ OSCON

Page 60: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Image Credits

57

•  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>

•  Cloud Formation Over the Adirondacks by Bradley Holt <https://twitter.com/BradleyHolt/status/623030107679002624>

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

•  build.phonegap by Andrés Álvarez Iglesias, on Flickr <https://www.flickr.com/photos/doctorserone/5682929553>

•  Getting Started with Ionic <http://ionicframework.com/getting-started/>

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

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

Page 61: Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix

Bradley Holt Cloudant Developer Advocate [email protected] @BradleyHolt github.com/bradley-holt