SlideShare a Scribd company logo
Mobile App Development with Cloudant
Glynn Bird, Developer Advocate, Cloud Data Services
@glynn_bird
2
3
4
Image Credit: Joan Touzet (@wohali), ASF Member, CouchDB PMC Member
5
Frameworks
6
Image Credit: Device landscape by Jeremy Keith, on Flickr
7
Not just mobile first…
Image Credit: NASA New Horizons
8
Offline First
Offline-First
Offline, online and somewhere in-between
9
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
11
Offline-first is the only way
to achieve a true, 100%
always-on user experience.*
*assuming the device is reliable
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
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
13
Difficulties of Offline First
14
Image credit http://www.sneakerheadvc.com/wp-content/uploads/2012/02/Apple_iSync1.png
Introducing CouchDB & IBM Cloudant
Apache CouchDB
• JSON document store
• HTTP API
• Replication
• Free, open-source
16
Apache CouchDB – built to replicate
• MVCC for document versioning
• Replication
• One-way or Two-way
• One-shot or continuous
17
Apache CouchDB – built to replicate
18
2.0
multi-node
clustering
Cloudant
Geo
Cloudant
Query
(Mango)
Cloudant
Search
(Lucene)
Dashboard
IBM Cloudant – built for scale
19
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
20
Mobile Apps - PouchDB
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
22
Getting started with PouchDB
23
<script src="https://cdn.jsdelivr.net/pouchdb/5.1.0/pouchdb.min.js"></script>
<script type="javascript">
var db = new PouchDB("smart-meter");
</script>
Adding documents - callbacks
24
db.post({
date: "2014-11-12T23:27:03.794Z",
kilowatt_hours: 14
}, function(err, data) {
console.log(err,data);
});
Adding documents - bring your own id
25
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
Getting a document
26
db.get("abc123", callback);
// calls back with
// {
// _id: "abc123",
// _rev: "1-27695d5f483ac267d03ad0e3cb54bd2c",
// timestamp: "2014-11-12T23:27:03.794Z",
// kilowatt_hours: 14
// }
Getting multiple documents
27
db.allDocs({include_docs:true}, callback);
// calls back with
// {
// "offset": 0,
// "total_rows": 24,
// "rows": [{...},{...}]
// }
Querying
28
• Primary Index
• MapReduce
• PouchDB-find plugin
• PouchDB-quick search plugin
Querying a Database with PouchDB Find
• Based on Cloudant Query (Mango)
• MongoDB-style query language
29
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']
})...
Replicating a PouchDB Database
30
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
Bidirectionally Replicating a PouchDB Database
31
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
Listening for Database Changes
32
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
PouchDB Framework Adapters
33
Web Apps Going Offline
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
35
Cache Manifest File
36
<html manifest="example.appcache">
…
</html>
CACHE MANIFEST
# v1 - 2015-01-08
index.html
logo.png
app.css
app.js
Service Workers (Beta)
37
Client-side scripting framework for
•programmable cache
•sync
•push messaging
•geo-fencing
•background tasks
https://jakearchibald.github.io/isserviceworkerready/
Hybrid or Responsive Mobile Web Apps
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
39
Responsive Mobile Web Apps
• HTML5, CSS and JavaScript mobile
web apps
• Responsive design
• Enhanced to enable offline usage
40
Native iOS & Android Apps
Cloudant Sync
• Library for iOS and Android
• Provides local storage and query API
• Optional sync to Cloudant
• Open-source and free to use
Cloudant Sync
• Stores data using SQLite
• TouchDB provides MVCC
• Replication to Cloudant over HTTPS
• Cloudant Query API
Getting started with Cloudant Sync - iOS
44
CDTDatastore *ds = [manager datastoreNamed:@"mydb" error:&error];
pod "CDTDatastore"
• Install library
• Create database
Creating data Cloudant Sync - iOS
45
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];
Fetching data - Cloudant Sync - iOS
46
CDTDocumentRevision *retrieved = [datastore getDocumentWithId:@"doc1"
error:&error];
Indexing data - Cloudant Sync - iOS
47
// 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"];
Querying data - Cloudant Sync - iOS
48
NSDictionary *query1 = @{ @"pet.species": @"cat" };
NSDictionary *query2 =
@{ @"$or": @[ @{ @"pet.species": @{ @"$eq": @"dog" } },
@{ @"age": @{ @"$lt": @30 } }
]};
CDTQResultSet *result = [ds find:query];
Getting started with Cloudant Sync - Android
49
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
Creating a database - Cloudant Sync - Android
50
import com.cloudant.sync.datastore.*;
// Create a DatastoreManager using application internal storage path
…
Datastore ds = manager.openDatastore("mydb");
Storing data - Cloudant Sync - Android
51
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);
Pro Forma
• Define fields you want to collect
• Renders form saving data to
PouchDB
• Replicates data to Cloudant
• Demo
https://glynnbird.github.io/proforma/
52
MD
• Offline word processor
• Saves Markdown documents to
PouchDB
• Replicates data to Cloudant
• Demo
http://mddoc.mybluemix.net/
53
Gutenberg
• Offline e-book reader
• Replicates book list from server
• Each book is a Cloudant database
• Demo
http://glynnbird.github.io/gutenberg/
54
www.glynnbird.com
• My home page
• Cloudant database of articles
• Replicated to PouchDB
• Appcache for offline first
• http://www.glynnbird.com/
55
Volt
• Password vault in a Chrome
extension
• Data stored in encrypted in PouchDB
• Optional back to CouchDB/Cloudant
• https://github.com/glynnbird/volt
56
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/
57
Glynn Bird
Developer Advocate, Cloud Data Services
glynn.bird@uk.ibm.com
@glynn_bird
github.com/glynnbird
Image Credits
59
• 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

PDF
0812 2014 01_toronto-smac meetup_i_os_cloudant_worklight_part2
PDF
Glynn Bird – Cloudant – Building applications for success.- NoSQL matters Bar...
PPTX
SQL to NoSQL: Top 6 Questions
PPT
Cloudant Overview Bluemix Meetup from Lisa Neddam
PPTX
SQL To NoSQL - Top 6 Questions Before Making The Move
PPTX
Building Modern Data Pipelines on GCP via a FREE online Bootcamp
PDF
Operationalizing Big Data Pipelines At Scale
PDF
Owning Your Own (Data) Lake House
0812 2014 01_toronto-smac meetup_i_os_cloudant_worklight_part2
Glynn Bird – Cloudant – Building applications for success.- NoSQL matters Bar...
SQL to NoSQL: Top 6 Questions
Cloudant Overview Bluemix Meetup from Lisa Neddam
SQL To NoSQL - Top 6 Questions Before Making The Move
Building Modern Data Pipelines on GCP via a FREE online Bootcamp
Operationalizing Big Data Pipelines At Scale
Owning Your Own (Data) Lake House

What's hot (20)

PPTX
Snowflake Datawarehouse Architecturing
PDF
Azure Databricks – Customer Experiences and Lessons Denzil Ribeiro Madhu Ganta
PDF
Cloud Big Data Architectures
PPTX
Webinar: The Anatomy of the Cloudant Data Layer
PPTX
Data lake – On Premise VS Cloud
PDF
Azure Hd insigth news
PPTX
Ravi Namboori 's Open stack framework introduction
PDF
Building a Data Lake on AWS
PPTX
GeoWave: Open Source Geospatial/Temporal/N-dimensional Indexing for Accumulo,...
PPTX
Securing Data in Hadoop at Uber
PDF
Logical-DataWarehouse-Alluxio-meetup
PPTX
Research on vector spatial data storage scheme based
PPTX
Introduction to Dremio
PPTX
Membase Meetup 2010
PPTX
Azure Lowlands: An intro to Azure Data Lake
PDF
Azure Cosmos DB
PPTX
How companies-use-no sql-and-couchbase-10152013
PPTX
Jax Cloud 2016 Microsoft Ignite Recap
KEY
MongoDB vs Mysql. A devops point of view
PPTX
Azure data bricks by Eugene Polonichko
Snowflake Datawarehouse Architecturing
Azure Databricks – Customer Experiences and Lessons Denzil Ribeiro Madhu Ganta
Cloud Big Data Architectures
Webinar: The Anatomy of the Cloudant Data Layer
Data lake – On Premise VS Cloud
Azure Hd insigth news
Ravi Namboori 's Open stack framework introduction
Building a Data Lake on AWS
GeoWave: Open Source Geospatial/Temporal/N-dimensional Indexing for Accumulo,...
Securing Data in Hadoop at Uber
Logical-DataWarehouse-Alluxio-meetup
Research on vector spatial data storage scheme based
Introduction to Dremio
Membase Meetup 2010
Azure Lowlands: An intro to Azure Data Lake
Azure Cosmos DB
How companies-use-no sql-and-couchbase-10152013
Jax Cloud 2016 Microsoft Ignite Recap
MongoDB vs Mysql. A devops point of view
Azure data bricks by Eugene Polonichko
Ad

Viewers also liked (15)

PDF
IBM - Introduction to Cloudant
PPTX
I See NoSQL Document Stores in Geospatial Applications
PDF
Cloud Data Services: A Brand New Ballgame for Business
PDF
Scalability 09262012
PPT
Birmingham Meetup
 
PDF
Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix
 
PDF
Practical Use of a NoSQL
PPTX
IBM Relay 2015: Open for Data
 
PPTX
IBM Relay 2015: Cloud is All About the Customer
 
PPTX
CouchDB Day NYC 2017: Using Geospatial Data in Cloudant & CouchDB
PDF
Socket.IO - Alternative Ways for Real-time Application
PPTX
IBM Relay 2015: Opening Keynote
 
PPTX
IBM Relay 2015: Securing the Future
 
PPT
Using Service Discovery and Service Proxy
 
PPTX
IBM RTP Dojo Launch
 
IBM - Introduction to Cloudant
I See NoSQL Document Stores in Geospatial Applications
Cloud Data Services: A Brand New Ballgame for Business
Scalability 09262012
Birmingham Meetup
 
Offline-First Mobile Web Apps with PouchDB, IBM Cloudant, and IBM Bluemix
 
Practical Use of a NoSQL
IBM Relay 2015: Open for Data
 
IBM Relay 2015: Cloud is All About the Customer
 
CouchDB Day NYC 2017: Using Geospatial Data in Cloudant & CouchDB
Socket.IO - Alternative Ways for Real-time Application
IBM Relay 2015: Opening Keynote
 
IBM Relay 2015: Securing the Future
 
Using Service Discovery and Service Proxy
 
IBM RTP Dojo Launch
 
Ad

Similar to Mobile App Development With IBM Cloudant (20)

PDF
Offline first solutions highland web group - december 2015
PDF
NoSQL on the move
PDF
Easy integration of Bluemix services with your applications
PPTX
What's New in Docker - February 2017
PPTX
Offline first development - Glasgow PHP - January 2016
PPTX
Custom Runtimes for the Cloud
PPTX
PDF
Made for Mobile - Let Office 365 Power Your Mobile Apps
PDF
Simplify Cloud Applications using Spring Cloud
PPTX
Mobile web development
PPTX
CloudPlatforms-Cloud PLatforms evaluation
PPTX
Dockerization of Azure Platform
PPTX
Cloud computing
PPTX
Cloud computing: highlights
PDF
Simulating Production with Clocker
PPTX
Plone FSR
KEY
Building production-quality apps with Node.js
PPT
Java Development on Bluemix
PDF
Head in the clouds
PPTX
Android Applications Development: A Quick Start Guide
Offline first solutions highland web group - december 2015
NoSQL on the move
Easy integration of Bluemix services with your applications
What's New in Docker - February 2017
Offline first development - Glasgow PHP - January 2016
Custom Runtimes for the Cloud
Made for Mobile - Let Office 365 Power Your Mobile Apps
Simplify Cloud Applications using Spring Cloud
Mobile web development
CloudPlatforms-Cloud PLatforms evaluation
Dockerization of Azure Platform
Cloud computing
Cloud computing: highlights
Simulating Production with Clocker
Plone FSR
Building production-quality apps with Node.js
Java Development on Bluemix
Head in the clouds
Android Applications Development: A Quick Start Guide

More from IBM Cloud Data Services (16)

PPTX
CouchDB Day NYC 2017: Full Text Search
PPTX
CouchDB Day NYC 2017: MapReduce Views
PPTX
CouchDB Day NYC 2017: Replication
PPTX
CouchDB Day NYC 2017: Mango
PPTX
CouchDB Day NYC 2017: JSON Documents
PPTX
CouchDB Day NYC 2017: Core HTTP API
PPTX
CouchDB Day NYC 2017: Introduction to CouchDB 2.0
PPTX
NoSQL for SQL Users
PPTX
dashDB: the GIS professional’s bridge to mainstream IT systems
PPTX
Practical Use of a NoSQL Database
PPTX
Machine Learning with Apache Spark
PPT
IBM Cognos Business Intelligence using dashDB
PPTX
Run Oracle Apps in the Cloud with dashDB
PDF
Analyzing GeoSpatial data with IBM Cloud Data Services & Esri ArcGIS
PDF
Get Started Quickly with IBM's Hadoop as a Service
PDF
Introducing dashDB MPP: The Power of Data Warehousing in the Cloud
CouchDB Day NYC 2017: Full Text Search
CouchDB Day NYC 2017: MapReduce Views
CouchDB Day NYC 2017: Replication
CouchDB Day NYC 2017: Mango
CouchDB Day NYC 2017: JSON Documents
CouchDB Day NYC 2017: Core HTTP API
CouchDB Day NYC 2017: Introduction to CouchDB 2.0
NoSQL for SQL Users
dashDB: the GIS professional’s bridge to mainstream IT systems
Practical Use of a NoSQL Database
Machine Learning with Apache Spark
IBM Cognos Business Intelligence using dashDB
Run Oracle Apps in the Cloud with dashDB
Analyzing GeoSpatial data with IBM Cloud Data Services & Esri ArcGIS
Get Started Quickly with IBM's Hadoop as a Service
Introducing dashDB MPP: The Power of Data Warehousing in the Cloud

Recently uploaded (20)

PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Introduction to Artificial Intelligence
PPTX
L1 - Introduction to python Backend.pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
System and Network Administraation Chapter 3
PDF
top salesforce developer skills in 2025.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Online Work Permit System for Fast Permit Processing
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
AI in Product Development-omnex systems
Design an Analysis of Algorithms II-SECS-1021-03
Introduction to Artificial Intelligence
L1 - Introduction to python Backend.pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
System and Network Administraation Chapter 3
top salesforce developer skills in 2025.pdf
Understanding Forklifts - TECH EHS Solution
Online Work Permit System for Fast Permit Processing
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
ManageIQ - Sprint 268 Review - Slide Deck
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Odoo POS Development Services by CandidRoot Solutions
VVF-Customer-Presentation2025-Ver1.9.pptx
ISO 45001 Occupational Health and Safety Management System
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
AI in Product Development-omnex systems

Mobile App Development With IBM Cloudant

  • 1. Mobile App Development with Cloudant Glynn Bird, Developer Advocate, Cloud Data Services @glynn_bird
  • 2. 2
  • 3. 3
  • 4. 4
  • 5. Image Credit: Joan Touzet (@wohali), ASF Member, CouchDB PMC Member 5
  • 7. Image Credit: Device landscape by Jeremy Keith, on Flickr 7 Not just mobile first…
  • 8. Image Credit: NASA New Horizons 8 Offline First Offline-First
  • 9. Offline, online and somewhere in-between 9
  • 10. 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
  • 11. 11 Offline-first is the only way to achieve a true, 100% always-on user experience.* *assuming the device is reliable
  • 12. 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
  • 13. 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 13
  • 14. Difficulties of Offline First 14 Image credit http://www.sneakerheadvc.com/wp-content/uploads/2012/02/Apple_iSync1.png
  • 15. Introducing CouchDB & IBM Cloudant
  • 16. Apache CouchDB • JSON document store • HTTP API • Replication • Free, open-source 16
  • 17. Apache CouchDB – built to replicate • MVCC for document versioning • Replication • One-way or Two-way • One-shot or continuous 17
  • 18. Apache CouchDB – built to replicate 18 2.0 multi-node clustering Cloudant Geo Cloudant Query (Mango) Cloudant Search (Lucene) Dashboard
  • 19. IBM Cloudant – built for scale 19
  • 20. 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 20
  • 21. Mobile Apps - PouchDB
  • 22. 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 22
  • 23. Getting started with PouchDB 23 <script src="https://cdn.jsdelivr.net/pouchdb/5.1.0/pouchdb.min.js"></script> <script type="javascript"> var db = new PouchDB("smart-meter"); </script>
  • 24. Adding documents - callbacks 24 db.post({ date: "2014-11-12T23:27:03.794Z", kilowatt_hours: 14 }, function(err, data) { console.log(err,data); });
  • 25. Adding documents - bring your own id 25 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
  • 26. Getting a document 26 db.get("abc123", callback); // calls back with // { // _id: "abc123", // _rev: "1-27695d5f483ac267d03ad0e3cb54bd2c", // timestamp: "2014-11-12T23:27:03.794Z", // kilowatt_hours: 14 // }
  • 27. Getting multiple documents 27 db.allDocs({include_docs:true}, callback); // calls back with // { // "offset": 0, // "total_rows": 24, // "rows": [{...},{...}] // }
  • 28. Querying 28 • Primary Index • MapReduce • PouchDB-find plugin • PouchDB-quick search plugin
  • 29. Querying a Database with PouchDB Find • Based on Cloudant Query (Mango) • MongoDB-style query language 29 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'] })...
  • 30. Replicating a PouchDB Database 30 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
  • 31. Bidirectionally Replicating a PouchDB Database 31 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
  • 32. Listening for Database Changes 32 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
  • 34. Web Apps Going Offline
  • 35. 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 35
  • 36. Cache Manifest File 36 <html manifest="example.appcache"> … </html> CACHE MANIFEST # v1 - 2015-01-08 index.html logo.png app.css app.js
  • 37. Service Workers (Beta) 37 Client-side scripting framework for •programmable cache •sync •push messaging •geo-fencing •background tasks https://jakearchibald.github.io/isserviceworkerready/
  • 38. Hybrid or Responsive Mobile Web Apps
  • 39. 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 39
  • 40. Responsive Mobile Web Apps • HTML5, CSS and JavaScript mobile web apps • Responsive design • Enhanced to enable offline usage 40
  • 41. Native iOS & Android Apps
  • 42. Cloudant Sync • Library for iOS and Android • Provides local storage and query API • Optional sync to Cloudant • Open-source and free to use
  • 43. Cloudant Sync • Stores data using SQLite • TouchDB provides MVCC • Replication to Cloudant over HTTPS • Cloudant Query API
  • 44. Getting started with Cloudant Sync - iOS 44 CDTDatastore *ds = [manager datastoreNamed:@"mydb" error:&error]; pod "CDTDatastore" • Install library • Create database
  • 45. Creating data Cloudant Sync - iOS 45 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];
  • 46. Fetching data - Cloudant Sync - iOS 46 CDTDocumentRevision *retrieved = [datastore getDocumentWithId:@"doc1" error:&error];
  • 47. Indexing data - Cloudant Sync - iOS 47 // 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"];
  • 48. Querying data - Cloudant Sync - iOS 48 NSDictionary *query1 = @{ @"pet.species": @"cat" }; NSDictionary *query2 = @{ @"$or": @[ @{ @"pet.species": @{ @"$eq": @"dog" } }, @{ @"age": @{ @"$lt": @30 } } ]}; CDTQResultSet *result = [ds find:query];
  • 49. Getting started with Cloudant Sync - Android 49 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
  • 50. Creating a database - Cloudant Sync - Android 50 import com.cloudant.sync.datastore.*; // Create a DatastoreManager using application internal storage path … Datastore ds = manager.openDatastore("mydb");
  • 51. Storing data - Cloudant Sync - Android 51 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);
  • 52. Pro Forma • Define fields you want to collect • Renders form saving data to PouchDB • Replicates data to Cloudant • Demo https://glynnbird.github.io/proforma/ 52
  • 53. MD • Offline word processor • Saves Markdown documents to PouchDB • Replicates data to Cloudant • Demo http://mddoc.mybluemix.net/ 53
  • 54. Gutenberg • Offline e-book reader • Replicates book list from server • Each book is a Cloudant database • Demo http://glynnbird.github.io/gutenberg/ 54
  • 55. www.glynnbird.com • My home page • Cloudant database of articles • Replicated to PouchDB • Appcache for offline first • http://www.glynnbird.com/ 55
  • 56. Volt • Password vault in a Chrome extension • Data stored in encrypted in PouchDB • Optional back to CouchDB/Cloudant • https://github.com/glynnbird/volt 56
  • 57. 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/ 57
  • 58. Glynn Bird Developer Advocate, Cloud Data Services glynn.bird@uk.ibm.com @glynn_bird github.com/glynnbird
  • 59. Image Credits 59 • 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>