SlideShare a Scribd company logo
@glynn_bird
Offline-First Development
GlasgowPHP – January 2016
Glynn Bird, Developer Advocate, IBM
@glynn_bird
@glynn_bird
NoSQL…
@glynn_bird
Relational Databases
• Defined Schema
• Tables/Columns/Rows
• Constraints/Stored Procedures/Triggers
• Indexes/Views
• Structured Query Language (SQL)
@glynn_bird
NoSQL Databases
• Any database that isn't a RDBMS!
• Types include:
• Document stores
• Key/Value stores
• Graph databases
• Often schemaless
• Often without SQL
@glynn_bird
…On the Move
@glynn_bird6
@glynn_bird7
@glynn_bird8
@glynn_bird
Image Credit: Joan Touzet (@wohali), ASF Member, CouchDB PMC Member
9
@glynn_bird
Frameworks
10
@glynn_bird
Image Credit: Device landscape by Jeremy Keith, on Flickr
11
Not just mobile first…
@glynn_bird
Image Credit: NASA New Horizons
12
Offline First
Offline-First
@glynn_bird
Offline, online and somewhere in-between
13
@glynn_bird
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
14
Text Credit: The Eight Fallacies of Distributed Computing by Peter Deutsch | Image Credit: Pneumatic Central by Sleestak, on Flickr
@glynn_bird15
Offline-first is the only way
to achieve a true, 100%
always-on user experience.*
*assuming the device is reliable
@glynn_bird
Benefits of Offline First
16
• 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
@glynn_bird
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
17
@glynn_bird
Difficulties of Offline First
18
Image credit http://www.sneakerheadvc.com/wp-content/uploads/2012/02/Apple_iSync1.png
@glynn_bird
Introducing CouchDB & IBM Cloudant
@glynn_bird
Apache CouchDB
• JSON document store
• HTTP API
• Replication
• Free, open-source
20
@glynn_bird
Apache CouchDB – built to replicate
• MVCC for document versioning
• Replication
• One-way or Two-way
• One-shot or continuous
21
@glynn_bird
Apache CouchDB – built to replicate
22
2.0
multi-node
clustering
Cloudant
Geo
Cloudant
Query
(Mango)
Cloudant
Search
(Lucene)
Dashboard
@glynn_bird
IBM Cloudant – built for scale
23
@glynn_bird
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
24
@glynn_bird
Mobile Apps - PouchDB
@glynn_bird
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
26
@glynn_bird
Getting started with PouchDB
27
<script src="https://cdn.jsdelivr.net/pouchdb/5.1.0/pouchdb.min.js"></script>
<script type="javascript">
var db = new PouchDB("smart-meter");
</script>
@glynn_bird
Adding documents - callbacks
28
db.post({
date: "2014-11-12T23:27:03.794Z",
kilowatt_hours: 14
}, function(err, data) {
console.log(err,data);
});
@glynn_bird
Adding documents - bring your own id
29
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
@glynn_bird
Getting a document
30
db.get("abc123", callback);
// calls back with
// {
// _id: "abc123",
// _rev: "1-27695d5f483ac267d03ad0e3cb54bd2c",
// timestamp: "2014-11-12T23:27:03.794Z",
// kilowatt_hours: 14
// }
@glynn_bird
Getting multiple documents
31
db.allDocs({include_docs:true}, callback);
// calls back with
// {
// "offset": 0,
// "total_rows": 24,
// "rows": [{...},{...}]
// }
@glynn_bird
Querying
32
• Primary Index
• MapReduce
• PouchDB-find plugin
• PouchDB-quick search plugin
@glynn_bird
Querying a Database with PouchDB Find
• Based on Cloudant Query (Mango)
• MongoDB-style query language
33
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']
})...
@glynn_bird
Replicating a PouchDB Database
34
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
@glynn_bird
Bidirectionally Replicating a PouchDB Database
35
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
@glynn_bird
Listening for Database Changes
36
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
@glynn_bird
PouchDB Framework Adapters
37
@glynn_bird
Web Apps Going Offline
@glynn_bird
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
39
@glynn_bird
Cache Manifest File
40
<html manifest="example.appcache">
…
</html>
CACHE MANIFEST
# v1 - 2015-01-08
index.html
logo.png
app.css
app.js
@glynn_bird
Service Workers (Beta)
41
Client-side scripting framework for
•programmable cache
•sync
•push messaging
•geo-fencing
•background tasks
https://jakearchibald.github.io/isserviceworkerready/
@glynn_bird
Hybrid or Responsive Mobile Web Apps
@glynn_bird
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
43
@glynn_bird
Responsive Mobile Web Apps
• HTML5, CSS and JavaScript mobile
web apps
• Responsive design
• Enhanced to enable offline usage
44
@glynn_bird
Native iOS & Android Apps
@glynn_bird
Cloudant Sync
• Library for iOS and Android
• Provides local storage and query API
• Optional sync to Cloudant
• Open-source and free to use
@glynn_bird
Cloudant Sync
• Stores data using SQLite
• TouchDB provides MVCC
• Replication to Cloudant over HTTPS
• Cloudant Query API
@glynn_bird
Getting started with Cloudant Sync - iOS
48
CDTDatastore *ds = [manager datastoreNamed:@"mydb" error:&error];
pod "CDTDatastore"
• Install library
• Create database
@glynn_bird
Creating data Cloudant Sync - iOS
49
CDTDocumentRevision *rev = [CDTDocumentRevision revisionWithDocId:@"doc1"];
rev.body = [@{
@"description": @"Buy milk",
@"completed": @NO,
@"type": @"com.cloudant.sync.example.task"
} mutableCopy];
CDTDocumentRevision *revision =
[ds createDocumentFromRevision:rev error:&error];
@glynn_bird
Fetching data - Cloudant Sync - iOS
50
CDTDocumentRevision *retrieved = [datastore getDocumentWithId:@"doc1"
error:&error];
@glynn_bird
Indexing data - Cloudant Sync - iOS
51
// create index on name, age and pet species
NSString *name = [ds ensureIndexed:@[@"name", @"age", @"pet.species"]
withName:@"basic"]
// create full-text index on name and comment
NSString *name = [ds ensureIndexed:@[@"name", @"comment"]
withName:@"basic_text_index"
type:@"text"];
@glynn_bird
Querying data - Cloudant Sync - iOS
52
NSDictionary *query1 = @{ @"pet.species": @"cat" };
NSDictionary *query2 =
@{ @"$or": @[ @{ @"pet.species": @{ @"$eq": @"dog" } },
@{ @"age": @{ @"$lt": @30 } }
]};
CDTQResultSet *result = [ds find:query];
@glynn_bird
Getting started with Cloudant Sync - Android
53
repositories {
mavenLocal()
maven { url "http://cloudant.github.io/cloudant-sync-eap/repository/" }
mavenCentral()
}
dependencies {
compile group: 'com.cloudant', name: 'cloudant-sync-datastore-core', version:'0.14.0'
compile group: 'com.cloudant', name: 'cloudant-sync-datastore-android', version:'0.14.0'
}
• Install library
@glynn_bird
Creating a database - Cloudant Sync - Android
54
import com.cloudant.sync.datastore.*;
// Create a DatastoreManager using application internal storage path
…
Datastore ds = manager.openDatastore("mydb");
@glynn_bird
Storing data - Cloudant Sync - Android
55
MutableDocumentRevision revision = new MutableDocumentRevision();
Map<String, Object> body = new HashMap<String, Object>();
body.put("animal", "cat");
revision.body = DocumentBodyFactory.create(body);
BasicDocumentRevision saved = ds.createDocumentFromRevision(revision);
@glynn_bird
Pro Forma
• Define fields you want to collect
• Renders form saving data to
PouchDB
• Replicates data to Cloudant
• Demo
https://glynnbird.github.io/proforma/
56
@glynn_bird
MD
• Offline word processor
• Saves Markdown documents to
PouchDB
• Replicates data to Cloudant
• Demo
http://mddoc.mybluemix.net/
57
@glynn_bird
Gutenberg
• Offline e-book reader
• Replicates book list from server
• Each book is a Cloudant database
• Demo
http://glynnbird.github.io/gutenberg/
58
@glynn_bird
www.glynnbird.com
• My home page
• Cloudant database of articles
• Replicated to PouchDB
• Appcache for offline first
• http://www.glynnbird.com/
59
@glynn_bird
Volt
• Password vault in a Chrome
extension
• Data stored in encrypted in PouchDB
• Optional back to CouchDB/Cloudant
• https://github.com/glynnbird/volt
60
@glynn_bird
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/
61
@glynn_bird
Glynn Bird
Developer Advocate, Cloud Data Services
glynn.bird@uk.ibm.com
@glynn_bird
github.com/glynnbird
@glynn_bird
Image Credits
63
• 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>

More Related Content

PPTX
IoT Sensor Sensibility - Hull Digital - C4Di - Feb 2016
PDF
GCP Gaming 2016 Seoul, Korea Firebase
PDF
CodeCamp Iasi - Creating serverless data analytics system on GCP using BigQuery
PDF
An indepth look at Google BigQuery Architecture by Felipe Hoffa of Google
PDF
GCP Gaming 2016 Seoul, Korea Gaming Analytics
PDF
Spatial Statistics on the Geospatial Web
PDF
node.js on Google Compute Engine
PDF
AWS re:Invent re:Cap - 데이터 분석: Amazon EC2 C4 Instance + Amazon EBS - 김일호
IoT Sensor Sensibility - Hull Digital - C4Di - Feb 2016
GCP Gaming 2016 Seoul, Korea Firebase
CodeCamp Iasi - Creating serverless data analytics system on GCP using BigQuery
An indepth look at Google BigQuery Architecture by Felipe Hoffa of Google
GCP Gaming 2016 Seoul, Korea Gaming Analytics
Spatial Statistics on the Geospatial Web
node.js on Google Compute Engine
AWS re:Invent re:Cap - 데이터 분석: Amazon EC2 C4 Instance + Amazon EBS - 김일호

What's hot (18)

PDF
Google Cloud Platform - for Mobile Solutions
PDF
Google Tech Talk with Dr. Eric Brewer in Korea Apr.27.2015
PDF
Cloud Security Monitoring at Auth0 - Art into Science
PDF
GCP Gaming 2016 Keynote Seoul, Korea
PDF
JCConf 2015 - Google Dataflow 在雲端大資料處理的應用
PDF
Visualising and Linking Open Data from Multiple Sources
PDF
Andrea Ulisse - How to build a scalable serverless IoT architecture on GCP - ...
PPTX
SEC302 Twitter's GCP Architecture for its petabyte scale data storage in gcs...
PPTX
TIAD : Automate everything with Google Cloud
PPTX
Google for モバイル アプリ 16:00: モバイル kpi 分析の新標準 fluentd + google big query
PPTX
Serverless real use cases and best practices: Asavari Tayal, Microsoft, Serve...
PDF
Start Flying with Python & Apache TinkerPop
PPTX
Modern vSphere Monitoring and Dashboard using InfluxDB, Telegraf and Grafana
PPTX
Michael DeSa [InfluxData] | Monitoring Methodologies | InfluxDays Virtual Exp...
PPTX
GitHub Data and Insights
PDF
Google Cloud Platform for the Enterprise
PDF
IoT Event Processing and Analytics with InfluxDB in Google Cloud | Christoph ...
PDF
Autoscaling containers with event driven workloads
Google Cloud Platform - for Mobile Solutions
Google Tech Talk with Dr. Eric Brewer in Korea Apr.27.2015
Cloud Security Monitoring at Auth0 - Art into Science
GCP Gaming 2016 Keynote Seoul, Korea
JCConf 2015 - Google Dataflow 在雲端大資料處理的應用
Visualising and Linking Open Data from Multiple Sources
Andrea Ulisse - How to build a scalable serverless IoT architecture on GCP - ...
SEC302 Twitter's GCP Architecture for its petabyte scale data storage in gcs...
TIAD : Automate everything with Google Cloud
Google for モバイル アプリ 16:00: モバイル kpi 分析の新標準 fluentd + google big query
Serverless real use cases and best practices: Asavari Tayal, Microsoft, Serve...
Start Flying with Python & Apache TinkerPop
Modern vSphere Monitoring and Dashboard using InfluxDB, Telegraf and Grafana
Michael DeSa [InfluxData] | Monitoring Methodologies | InfluxDays Virtual Exp...
GitHub Data and Insights
Google Cloud Platform for the Enterprise
IoT Event Processing and Analytics with InfluxDB in Google Cloud | Christoph ...
Autoscaling containers with event driven workloads
Ad

Viewers also liked (15)

PPTX
Інноваційний проект "Шкільна кіностудія"
PPTX
tecnología?
PDF
Interaction Design
PDF
Img190 07 enero 2016
PPTX
5 клас урок 9
DOC
15
PDF
Webの勉強会#9
PPTX
Tema 3 Los Inicios de la Edad Contemporánea
PPTX
Tema 5 La Población en España y en Europa
PPTX
Fa102a presentation-102815
PPTX
5 клас урок 6
PPT
Asbestos
PPTX
Tema 7 España y la Unión Europea
PPTX
Contaminacion del agua y aire luis
PPTX
Currency wars
Інноваційний проект "Шкільна кіностудія"
tecnología?
Interaction Design
Img190 07 enero 2016
5 клас урок 9
15
Webの勉強会#9
Tema 3 Los Inicios de la Edad Contemporánea
Tema 5 La Población en España y en Europa
Fa102a presentation-102815
5 клас урок 6
Asbestos
Tema 7 España y la Unión Europea
Contaminacion del agua y aire luis
Currency wars
Ad

Similar to Offline first development - Glasgow PHP - January 2016 (20)

PPTX
Mobile App Development With IBM Cloudant
PDF
Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix
 
PDF
Offline first solutions highland web group - december 2015
PDF
NoSQL on the move
PDF
Introduction to Globus and Research Automation.pdf
PPTX
Webinar: The Anatomy of the Cloudant Data Layer
PDF
Pivotal Greenplum 次世代マルチクラウド・データ分析プラットフォーム
PDF
Globus Command Line Interface (APS Workshop)
PDF
Buildingsocialanalyticstoolwithmongodb
PPTX
Dataiku Flow and dctc - Berlin Buzzwords
PPTX
Lightning Talk: Why and How to Integrate MongoDB and NoSQL into Hadoop Big Da...
PDF
Getting Started with DrupalGap
PDF
Droidcon Paris 2015
PPTX
Custom Runtimes for the Cloud
PDF
Leveraging the Globus Platform (GlobusWorld Tour - Columbia University)
PPTX
Enhanced site search with cognitive APIs - Glynn Bird
PPTX
NoSQL for SQL Users
PDF
Spark on Dataproc - Israel Spark Meetup at taboola
PPTX
Plone FSR
PDF
RESTful OSGi middleware for NoSQL databases with Docker
Mobile App Development With IBM Cloudant
Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix
 
Offline first solutions highland web group - december 2015
NoSQL on the move
Introduction to Globus and Research Automation.pdf
Webinar: The Anatomy of the Cloudant Data Layer
Pivotal Greenplum 次世代マルチクラウド・データ分析プラットフォーム
Globus Command Line Interface (APS Workshop)
Buildingsocialanalyticstoolwithmongodb
Dataiku Flow and dctc - Berlin Buzzwords
Lightning Talk: Why and How to Integrate MongoDB and NoSQL into Hadoop Big Da...
Getting Started with DrupalGap
Droidcon Paris 2015
Custom Runtimes for the Cloud
Leveraging the Globus Platform (GlobusWorld Tour - Columbia University)
Enhanced site search with cognitive APIs - Glynn Bird
NoSQL for SQL Users
Spark on Dataproc - Israel Spark Meetup at taboola
Plone FSR
RESTful OSGi middleware for NoSQL databases with Docker

Recently uploaded (20)

PPTX
artificial intelligence overview of it and more
PPTX
Introduction to Information and Communication Technology
PDF
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PPTX
international classification of diseases ICD-10 review PPT.pptx
PPT
tcp ip networks nd ip layering assotred slides
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PDF
Introduction to the IoT system, how the IoT system works
PPTX
Digital Literacy And Online Safety on internet
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PPT
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
PPTX
Power Point - Lesson 3_2.pptx grad school presentation
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
DOCX
Unit-3 cyber security network security of internet system
PDF
The Internet -By the Numbers, Sri Lanka Edition
artificial intelligence overview of it and more
Introduction to Information and Communication Technology
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
WebRTC in SignalWire - troubleshooting media negotiation
INTERNET------BASICS-------UPDATED PPT PRESENTATION
introduction about ICD -10 & ICD-11 ppt.pptx
international classification of diseases ICD-10 review PPT.pptx
tcp ip networks nd ip layering assotred slides
Introuction about ICD -10 and ICD-11 PPT.pptx
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
Introduction to the IoT system, how the IoT system works
Digital Literacy And Online Safety on internet
The New Creative Director: How AI Tools for Social Media Content Creation Are...
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
Power Point - Lesson 3_2.pptx grad school presentation
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
Unit-3 cyber security network security of internet system
The Internet -By the Numbers, Sri Lanka Edition

Offline first development - Glasgow PHP - January 2016

  • 1. @glynn_bird Offline-First Development GlasgowPHP – January 2016 Glynn Bird, Developer Advocate, IBM @glynn_bird
  • 3. @glynn_bird Relational Databases • Defined Schema • Tables/Columns/Rows • Constraints/Stored Procedures/Triggers • Indexes/Views • Structured Query Language (SQL)
  • 4. @glynn_bird NoSQL Databases • Any database that isn't a RDBMS! • Types include: • Document stores • Key/Value stores • Graph databases • Often schemaless • Often without SQL
  • 9. @glynn_bird Image Credit: Joan Touzet (@wohali), ASF Member, CouchDB PMC Member 9
  • 11. @glynn_bird Image Credit: Device landscape by Jeremy Keith, on Flickr 11 Not just mobile first…
  • 12. @glynn_bird Image Credit: NASA New Horizons 12 Offline First Offline-First
  • 13. @glynn_bird Offline, online and somewhere in-between 13
  • 14. @glynn_bird 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 14 Text Credit: The Eight Fallacies of Distributed Computing by Peter Deutsch | Image Credit: Pneumatic Central by Sleestak, on Flickr
  • 15. @glynn_bird15 Offline-first is the only way to achieve a true, 100% always-on user experience.* *assuming the device is reliable
  • 16. @glynn_bird Benefits of Offline First 16 • 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
  • 17. @glynn_bird 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 17
  • 18. @glynn_bird Difficulties of Offline First 18 Image credit http://www.sneakerheadvc.com/wp-content/uploads/2012/02/Apple_iSync1.png
  • 20. @glynn_bird Apache CouchDB • JSON document store • HTTP API • Replication • Free, open-source 20
  • 21. @glynn_bird Apache CouchDB – built to replicate • MVCC for document versioning • Replication • One-way or Two-way • One-shot or continuous 21
  • 22. @glynn_bird Apache CouchDB – built to replicate 22 2.0 multi-node clustering Cloudant Geo Cloudant Query (Mango) Cloudant Search (Lucene) Dashboard
  • 23. @glynn_bird IBM Cloudant – built for scale 23
  • 24. @glynn_bird 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 24
  • 26. @glynn_bird 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 26
  • 27. @glynn_bird Getting started with PouchDB 27 <script src="https://cdn.jsdelivr.net/pouchdb/5.1.0/pouchdb.min.js"></script> <script type="javascript"> var db = new PouchDB("smart-meter"); </script>
  • 28. @glynn_bird Adding documents - callbacks 28 db.post({ date: "2014-11-12T23:27:03.794Z", kilowatt_hours: 14 }, function(err, data) { console.log(err,data); });
  • 29. @glynn_bird Adding documents - bring your own id 29 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
  • 30. @glynn_bird Getting a document 30 db.get("abc123", callback); // calls back with // { // _id: "abc123", // _rev: "1-27695d5f483ac267d03ad0e3cb54bd2c", // timestamp: "2014-11-12T23:27:03.794Z", // kilowatt_hours: 14 // }
  • 31. @glynn_bird Getting multiple documents 31 db.allDocs({include_docs:true}, callback); // calls back with // { // "offset": 0, // "total_rows": 24, // "rows": [{...},{...}] // }
  • 32. @glynn_bird Querying 32 • Primary Index • MapReduce • PouchDB-find plugin • PouchDB-quick search plugin
  • 33. @glynn_bird Querying a Database with PouchDB Find • Based on Cloudant Query (Mango) • MongoDB-style query language 33 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'] })...
  • 34. @glynn_bird Replicating a PouchDB Database 34 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
  • 35. @glynn_bird Bidirectionally Replicating a PouchDB Database 35 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
  • 36. @glynn_bird Listening for Database Changes 36 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
  • 39. @glynn_bird 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 39
  • 40. @glynn_bird Cache Manifest File 40 <html manifest="example.appcache"> … </html> CACHE MANIFEST # v1 - 2015-01-08 index.html logo.png app.css app.js
  • 41. @glynn_bird Service Workers (Beta) 41 Client-side scripting framework for •programmable cache •sync •push messaging •geo-fencing •background tasks https://jakearchibald.github.io/isserviceworkerready/
  • 43. @glynn_bird 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 43
  • 44. @glynn_bird Responsive Mobile Web Apps • HTML5, CSS and JavaScript mobile web apps • Responsive design • Enhanced to enable offline usage 44
  • 45. @glynn_bird Native iOS & Android Apps
  • 46. @glynn_bird Cloudant Sync • Library for iOS and Android • Provides local storage and query API • Optional sync to Cloudant • Open-source and free to use
  • 47. @glynn_bird Cloudant Sync • Stores data using SQLite • TouchDB provides MVCC • Replication to Cloudant over HTTPS • Cloudant Query API
  • 48. @glynn_bird Getting started with Cloudant Sync - iOS 48 CDTDatastore *ds = [manager datastoreNamed:@"mydb" error:&error]; pod "CDTDatastore" • Install library • Create database
  • 49. @glynn_bird Creating data Cloudant Sync - iOS 49 CDTDocumentRevision *rev = [CDTDocumentRevision revisionWithDocId:@"doc1"]; rev.body = [@{ @"description": @"Buy milk", @"completed": @NO, @"type": @"com.cloudant.sync.example.task" } mutableCopy]; CDTDocumentRevision *revision = [ds createDocumentFromRevision:rev error:&error];
  • 50. @glynn_bird Fetching data - Cloudant Sync - iOS 50 CDTDocumentRevision *retrieved = [datastore getDocumentWithId:@"doc1" error:&error];
  • 51. @glynn_bird Indexing data - Cloudant Sync - iOS 51 // create index on name, age and pet species NSString *name = [ds ensureIndexed:@[@"name", @"age", @"pet.species"] withName:@"basic"] // create full-text index on name and comment NSString *name = [ds ensureIndexed:@[@"name", @"comment"] withName:@"basic_text_index" type:@"text"];
  • 52. @glynn_bird Querying data - Cloudant Sync - iOS 52 NSDictionary *query1 = @{ @"pet.species": @"cat" }; NSDictionary *query2 = @{ @"$or": @[ @{ @"pet.species": @{ @"$eq": @"dog" } }, @{ @"age": @{ @"$lt": @30 } } ]}; CDTQResultSet *result = [ds find:query];
  • 53. @glynn_bird Getting started with Cloudant Sync - Android 53 repositories { mavenLocal() maven { url "http://cloudant.github.io/cloudant-sync-eap/repository/" } mavenCentral() } dependencies { compile group: 'com.cloudant', name: 'cloudant-sync-datastore-core', version:'0.14.0' compile group: 'com.cloudant', name: 'cloudant-sync-datastore-android', version:'0.14.0' } • Install library
  • 54. @glynn_bird Creating a database - Cloudant Sync - Android 54 import com.cloudant.sync.datastore.*; // Create a DatastoreManager using application internal storage path … Datastore ds = manager.openDatastore("mydb");
  • 55. @glynn_bird Storing data - Cloudant Sync - Android 55 MutableDocumentRevision revision = new MutableDocumentRevision(); Map<String, Object> body = new HashMap<String, Object>(); body.put("animal", "cat"); revision.body = DocumentBodyFactory.create(body); BasicDocumentRevision saved = ds.createDocumentFromRevision(revision);
  • 56. @glynn_bird Pro Forma • Define fields you want to collect • Renders form saving data to PouchDB • Replicates data to Cloudant • Demo https://glynnbird.github.io/proforma/ 56
  • 57. @glynn_bird MD • Offline word processor • Saves Markdown documents to PouchDB • Replicates data to Cloudant • Demo http://mddoc.mybluemix.net/ 57
  • 58. @glynn_bird Gutenberg • Offline e-book reader • Replicates book list from server • Each book is a Cloudant database • Demo http://glynnbird.github.io/gutenberg/ 58
  • 59. @glynn_bird www.glynnbird.com • My home page • Cloudant database of articles • Replicated to PouchDB • Appcache for offline first • http://www.glynnbird.com/ 59
  • 60. @glynn_bird Volt • Password vault in a Chrome extension • Data stored in encrypted in PouchDB • Optional back to CouchDB/Cloudant • https://github.com/glynnbird/volt 60
  • 61. @glynn_bird 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/ 61
  • 62. @glynn_bird Glynn Bird Developer Advocate, Cloud Data Services glynn.bird@uk.ibm.com @glynn_bird github.com/glynnbird
  • 63. @glynn_bird Image Credits 63 • 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>