©2015 Couchbase Inc. 1
Nic Raboy, Developer Advocate at Couchbase
Developing For Offline First Mobile
Experiences
©2015 Couchbase Inc. 2
About Me
Nic Raboy
Developer Advocate at Couchbase
nic@couchbase.com
@nraboy (Twitter)
©2015 Couchbase Inc. 3
The Plan
 Where are things headed
 The business problem
 Couchbase Mobile
 Example native code
 Example hybrid code
©2015 Couchbase Inc. 4
Current State
©2015 Couchbase Inc. 5
Trends
©2015 Couchbase Inc. 6
RDBMS
©2015 Couchbase Inc. 7
NoSQL
What is the business problem?
Today’s Mobile Apps
9
Try again
later.
No Internet
please wait…
©2015 Couchbase Inc. 10
How DoesThis Affect What PeopleThink?
Doesn’t work a lot of the time and when
it does it’s slow.
©2015 Couchbase Inc. 11
How DoesThis Affect App Uninstall Rates?
0%
20%
40%
60%
80%
100%
UninstallRate
Freezing
Crashing
Slow Responsiveness
Source: uSamp
Data Location is the Problem
12
Local Data + Sync is the Solution
13
©2015 Couchbase Inc. 14
What DoesThis Mean ForYour Apps
Always work both online & offline
Always be blazing fast
Built with orders of magnitude less code
Users will absolutely love your apps
What is Couchbase Mobile?
Couchbase Lite Sync Gateway
©2015 Couchbase Inc. 17
Couchbase Lite
• NoSQL Mobile Database
• Document Oriented & Schema-Less
• Small Footprint
• Runs In-Process
©2015 Couchbase Inc. 18
Couchbase Sync Gateway
• Data Replication Between Server & Device
• Data Orchestration
• Authentication & Authorization
©2015 Couchbase Inc. 19
Replication
 Is asynchronous
 Can be continuous or one-shot
 Sends JSON over HTTP
 Handles network hiccups
 Network timeouts
©2015 Couchbase Inc. 20
Channels
Sports San Francisco
Nature
Admin
21
©2015 Couchbase Inc. 22
Available SDKs & Frameworks
©2015 Couchbase Inc. 23
Couchbase Mobile Summary
 Offline first approach
 NoSQL document database
 Works with Couchbase Server
 Available for many platforms and frameworks
Couchbase Lite for Native Android & iOS
©2015 Couchbase Inc. 25
Creating a Database
©2015 Couchbase Inc. 26
Inserting a Document
©2015 Couchbase Inc. 27
Couchbase LiteViews
©2015 Couchbase Inc. 28
Map / Reduce Views
 Concept from functional program, via Google
 App-defined map function operates on documents
 Function output generates an index ordered by key
 Index rows can be aggregated via a reduce function
 Index is queried by a key or key range
©2015 Couchbase Inc. 29
Replication With Sync Gateway
©2015 Couchbase Inc. 30
Listen For Changes
©2015 Couchbase Inc. 31
Other WaysTo Listen
 DatabaseChanged:Any document updated
 DocumentChanged: A specific document updated
 LiveQuery: Change in query result set
©2015 Couchbase Inc. 32
Couchbase Lite for iOS Special Note
 iOS SDK works for OS X desktop applications
Couchbase Cordova Plugin
©2015 Couchbase Inc. 34
Couchbase Cordova Plugin
 Compatible with Android and iOS
 Access Couchbase Lite via RESTful APIs
 Easily integrate with Ionic Framework and PhoneGap
through the ng-couchbase-lite wrapper
©2015 Couchbase Inc. 35
Creating a Database
©2015 Couchbase Inc. 36
Inserting Documents
©2015 Couchbase Inc. 37
Couchbase LiteViews
©2015 Couchbase Inc. 38
Querying CouchbaseViews
©2015 Couchbase Inc. 39
Replication With Sync Gateway
©2015 Couchbase Inc. 40
Listen for Changes
Couchbase Sync Gateway
©2015 Couchbase Inc. 42
Basic Sync Gateway Configuration
Creating a Native AndroidToDo List
©2015 Couchbase Inc. 44
Couchbase MobileToDo List Application
©2015 Couchbase Inc. 45
Code Walkthrough!
 Creating the UI
 Initializing the Android ListView
 Designing the Android ListView
 Creating a database and replicating with the server
 Listening for database changes
 Adding data to the database
 Removing data from the database
 Cleaning up when terminating the application
DemoTime!
©2015 Couchbase Inc. 47
Couchbase Labs on GitHub
https://github.com/couchbaselabs/todolite-android
https://github.com/couchbaselabs/todolite-ios
https://github.com/couchbaselabs/todolite-ionic
Questions?
©2014 Couchbase, Inc.
©2015 Couchbase Inc. 49
We’re Hiring!
http://www.couchbase.com/careers
©2015 Couchbase Inc. 50
ThankYou!
Nic Raboy
nic@couchbase.com
@nraboy (Twitter)

Developing for Offline First Mobile Experiences

Editor's Notes

  • #9 Before we begin, I want to take some time to share with you why we created Couchbase mobile, and the business problem that we hoped to solve.
  • #10 Today’s Mobile Apps ---- Most of today’s mobile apps are designed so that any remote data, whether downloading records from a server or uploading your local data, requires the network to be available and online. As long as that’s the case, and the Internet connection is fast enough, and there aren’t any problems, then the app performs exactly as expected—and that’s great. But as soon as the network begins to degrade, say we’re down to just a couple of tower bars, or the Wi-F is having a bunch of latency—that sort of thing—then we start to see the app’s user experience break down. That’s when we see the progress notification UI hints show up, the spinners, the waiting. If things get bad enough, we will see an error message saying that, “I’m sorry you can’t do what you were trying to do right now, you’ll have to try later.” Whatever work you were doing is lost, and the end result is a user who is unhappy and dissatisfied and can use your app.
  • #11 How Does This Affect What People Think? ---- how do you know if you’ve got these kind of problems? Well, it’s reflected in your app store rating. We’ve all seen apps in the one and two star category, with user reviews saying that the app doesn’t work or it’s slow.
  • #12 How Does This Affect App Uninstall Rates ---- Besides the bad brand PR, why should you care? Because people delete poorly performing apps. Let’s look at application uninstall rates. Why do people delete an app from their device? The top three reasons are: freezing, crashing, and slow responsiveness. And in the case of freezing and slow responsiveness, those are directly tied to how we build our application. More often than not, our networking code is the culprit. In a lot of cases we can’t control the environment where people are using our app, but if we’re using proper coding strategies, we can mitigate those issues.
  • #13 Slide 10 - Data Location is the Problem
  • #14 Slide 11 - Local Data + Sync is the Solution
  • #17 We have two core products in Couchbase mobile. We have Couchbase Lite, which runs on the device And we have Sync Gateway, which stands on the border between your private cloud and the public cloud.
  • #18 We have two core products in Couchbase mobile. We have Couchbase Lite, which runs on the device And we have Sync Gateway, which stands on the border between your private cloud and the public cloud.
  • #22 Sync gateway also handles the permanent persistence of your applications data in your Couchbase Server cluster that lives in your private data center. So as your device is syncing with sync gateway and pushing your data up, sync gateway is also writing that data into the permanent data store, backed by our highly scalable Couchbase Server technology. Now, let’s look at security layer concerns: in this particular case, authorization.
  • #36 Creating a Database ---- cblite.getURL is the only available plugin function. Use the URL to create a new ng-couchbase-lite object and assign it a database name. All requests will go against this URL and database including the “createDatabase” function.
  • #37 Inserting Documents ---- Show that you can take a JavaScript object and insert it directly into Couchbase Lite without any further manipulations. Database transactions return promises to remain asynchronous.
  • #38 Couchbase Lite Views ---- Elaborate the differences between HTML5 local storage and NoSQL. With HTML5 local storage you’re probably going to serialize all data into a single property. This single property is going to become quite large and hard to maintain, but you’re going to do your lookups with that one key name that you probably remember. With NoSQL documents you’re going to have many documents which you probably won’t remember the key names for. To get all these documents you’re going to need to design a view for querying them. In the case of the Todo Lite example, there are two views. A view for querying lists and a view for querying the tasks that exist in a list.
  • #39 Querying Couchbase Views ---- Querying the “lists” view will return all lists, but of course in a promise to remain asynchronous. You also have the option to pass a start key, which is obvious in the example of querying the “tasks” view. You don’t want to query all the tasks, only the particular tasks that go with a list.
  • #40 Replication With Sync Gateway ---- When replication starts, it will asynchronously happen in the background. In this example, replication will continuously happen, pushing all local changes to the sync gateway. Once local starts replicating up, we also replicate the sync gateway down to local. We are starting two replication processes.
  • #41 Listen for Changes ---- The “listen()” function of ng-couchbase-lite will broadcast any changes it picks up by doing continuous long-polls against the API. Changes include those done locally and remotely. Anything broadcasted can be picked up in AngularJS with the “$on” method of “$rootScope”. Broadcasted information is in array form with properties that contain ID and “possibly” a deleted property to signify that a document has been deleted
  • #44 Demo Time ---- Sync Gateway URL: http://localhost:4985/_admin/ Simulator IP: 192.168.56.1 Sync Gateway Port: 4984
  • #48 Couchbase Labs on GitHub ---- A fully functional example project is available for download on GitHub.