Slide 1

Slide 1 text

Empire Conf Bradley Holt, Developer Advocate Thursday, October 12, 2017 Offline Sync for Progressive Web Apps ⌨

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Prerequisite Knowledge & Skills §  Ability to write JavaScript at a novice level, at minimum §  A basic understanding of JavaScript promises https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html §  A basic understanding of HTML §  Ability to work with an application programming interface (API) https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb

Slide 4

Slide 4 text

Tutorial Outline Background Key Concepts Initial Set Up ⌨ Creating the Polymer App Syncing Data What's next?

Slide 5

Slide 5 text

Background

Slide 6

Slide 6 text

https://mobilefirstplatform.ibmcloud.com/blog/2016/01/13/ionic-hybrid-mobile-app-using-mobilefirst-platform-7-1-cli/ Native Mobile Apps

Slide 7

Slide 7 text

organic growth by Steve Jurvetson, on Flickr (CC BY 2.0). The Web Platform

Slide 8

Slide 8 text

W3C HTML5 Logo (CC BY 3.0).

Slide 9

Slide 9 text

Mobile First & the Mobile Web Screen size visualisation by Orde Saunders, on Flickr (CC BY 2.0).

Slide 10

Slide 10 text

https://responsivedesign.is/examples/css-conf-europe/

Slide 11

Slide 11 text

Evergreen by Basheer Tome, on Flickr (CC BY 2.0). Evergreen Browsers

Slide 12

Slide 12 text

mettalic weave by Joel Ormsby, on Flickr (CC BY 2.0). Polyfills

Slide 13

Slide 13 text

By Diseño Web Valencia, via Wikimedia Commons (CC BY-SA 4.0).

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

https://doesmysiteneedhttps.com/

Slide 16

Slide 16 text

Percentage of Web Pages Loaded by Firefox Using HTTPS https://letsencrypt.org/stats/

Slide 17

Slide 17 text

Web or Mobile App? iPhone 4 - Bottom View with Bumper by William Hook, on Flickr (CC BY-SA 2.0).

Slide 18

Slide 18 text

http://gph.is/2dhW35c

Slide 19

Slide 19 text

Progressive Web Apps (or how to have your cake and eat it too) German Chocolate Cake by Kimberly Vardeman, on Flickr (CC BY 2.0).

Slide 20

Slide 20 text

Progressive Web Apps A Progressive Web App provides both the discoverability of a web app and the reliable, fast, and engaging user experience of a native mobile app https://www.pokedex.org/

Slide 21

Slide 21 text

"I don’t want the web to equal native; I want the web to surpass it. I, for one, would prefer a reality where my home screen isn’t filled with the icons of startups and companies that have fulfilled the criteria of the gatekeepers. But a home screen filled with the faces of people who didn’t have to ask anyone’s permission to publish? That’s what I want!" –Jeremy Keith Adactio: Journal—Progressing the web | Jeremy Keith by Matthew Oliphant, on Flickr (CC BY-ND 2.0).

Slide 22

Slide 22 text

https://www.pwastats.com/

Slide 23

Slide 23 text

https://developers.google.com/web/showcase/2017/twitter

Slide 24

Slide 24 text

Geolocation API Network Information API IndexedDB WebRTC API Push API Notifications API Device Orientation API Screen Orientation API Permissions API Proximity API Pointer Lock API Vibration API Payment Request API Battery Status API Ambient Light Sensor API Service Worker API IBM Design Icons by International Business Machines Corporation (CC BY 4.0).

Slide 25

Slide 25 text

Offline First Progressive Web Apps must be Offline First in order to provide a reliable, fast, and engaging user experience regardless of network connectivity No Signal by Steve Hodgson, on Flickr (CC BY-SA 2.0).

Slide 26

Slide 26 text

speed 3 by Monkieyes, on Flickr (CC BY 2.0). Zero Latency Access to Content & Data

Slide 27

Slide 27 text

Offline, Online & ¯\_(ツ)_/¯

Slide 28

Slide 28 text

The Next Billion Internet Users the galaxy by tommy@chau, on Flickr (CC BY 2.0).

Slide 29

Slide 29 text

Cache content & assets with Service Workers Service Workers fetch diagram by Mozilla Contributors (CC-BY-SA 2.5).

Slide 30

Slide 30 text

IndexedDB Use IndexedDB or localForage (a polyfill that uses WebSQL or localStorage if IndexedDB is not supported) to make application data available while offline https://caniuse.com/#feat=indexeddb

Slide 31

Slide 31 text

Technological devices design created by D3images - Freepik.com | Texture 94 by Ellen van Deelen, on Flickr (CC BY 2.0). Sync is Hard

Slide 32

Slide 32 text

IBM Cloudant Apache CouchDB PouchDB

Slide 33

Slide 33 text

"CouchDB’s superpower is sync. Sometimes I even try to explain it to people by saying, 'CouchDB isn’t a database; it’s a sync engine.' It’s a way of efficiently transferring data from one place to another, while intelligently managing conflicts and revisions. It’s very similar to Git. When I make that analogy, the light bulb often goes off." –Nolan Lawson PouchDB & CouchDB: An interview with Nolan Lawson – CouchDB Blog

Slide 34

Slide 34 text

Apache CouchDB An open source document database featuring an HTTP API, JSON documents, clustering capabilities for horizontal scalability, and peer-to-peer replication http://couchdb.apache.org/

Slide 35

Slide 35 text

PouchDB An open source JavaScript database that syncs with anything that implements the CouchDB Replication Protocol https://pouchdb.com/

Slide 36

Slide 36 text

IBM Cloudant A fully-managed database-as-a-service (DBaaS) based on Apache CouchDB with additional full text and geospatial search capabilities https://www.ibm.com/analytics/us/en/technology/cloud-data-services/cloudant/

Slide 37

Slide 37 text

Apache CouchDB Devices by Darwin Laganzon, on Pixabay (CC0 Public Domain). | Cloud by Raphael Silva, on Pixabay (CC0 Public Domain). PouchDB Browser-to- Cloud Sync

Slide 38

Slide 38 text

userdb-d76846 userdb-905cec userdb-adc95b userdb-c082f2 userdb-730bba userdb-c3d3e5 userdb-a1ec1f userdb-85a327 userdb-9b9aba userdb-85bcfe userdb-da3d25 One Database Per User database by Tim Morgan, on Flickr (CC BY 2.0). userdb-43ecb5 userdb-8a5dfa

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Apache CouchDB Devices by Darwin Laganzon, on Pixabay (CC0 Public Domain). | Cloud by Raphael Silva, on Pixabay (CC0 Public Domain). PouchDB

Slide 41

Slide 41 text

Summary Build on the web platform with Progressive Web Apps Offline First makes for a reliable, fast, and engaging user experience Cache URL-addressable resources and content with Service Workers Store app data in PouchDB to easily sync your Offline First data Use Hoodie as the backend for your multi-user Offline First app

Slide 42

Slide 42 text

Tiny Steps by Jim Whimpey, on Flickr (CC BY 2.0). Progressive Web Apps, Progressively

Slide 43

Slide 43 text

Progressive Web Apps, Progressively Ensure your website is mobile friendly Improve an existing website Create a native app-like experience Leverage the web platform

Slide 44

Slide 44 text

Progressive Web Apps, Progressively •  Implement responsive web design •  Ensure cross-browser compatibility •  Test and improve web performance Ensure your website is mobile friendly Improve an existing website Create a native app-like experience Leverage the web platform

Slide 45

Slide 45 text

Progressive Web Apps, Progressively Ensure your website is mobile friendly •  Use an SSL/TLS certificate (Let's Encrypt is your friend) •  Add a Web App Manifest (name, author, icon, description, etc.) •  Use a Service Worker to make your app cache-first Improve an existing website Create a native app-like experience Leverage the web platform

Slide 46

Slide 46 text

Progressive Web Apps, Progressively Ensure your website is mobile friendly Improve an existing website •  Provide instant page transitions with a single-page (client-rendered) app •  Minimize time-to-interactive with the PRPL pattern •  Use a local database (IndexedDB, localForage, or PouchDB) Create a native app-like experience Leverage the web platform

Slide 47

Slide 47 text

Progressive Web Apps, Progressively Ensure your website is mobile friendly Improve an existing website Create a native app-like experience •  Ensure that each page can be addressed via a URL •  Implement server-side rendering for users without JavaScript •  Build a hybrid mobile app with a tool like Ionic or Apache Cordova Leverage the web platform

Slide 48

Slide 48 text

Offline Camp From the Catskill Mountains, to the Central Coast of California, to Berlin, we're building the Offline First community, one campfire at a time Image credit: Aaron Ross

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Get Involved in the Offline First Community! §  Join the Offline First Slack team: offlinefirst.org/chat/ §  Follow @OfflineCamp on Twitter §  Read the Offline Camp Medium publication: medium.com/offline-camp §  Join us at an upcoming Offline Camp: http://offlinefirst.org/camp/

Slide 51

Slide 51 text

Key Concepts

Slide 52

Slide 52 text

Shopping List Offline First Demo Apps (work in progress) A series of demo Progressive Web Apps, hybrid mobile apps, native mobile apps, and a desktop app—each built using a different stack https://github.com/ibm-watson-data-lab/shopping-list

Slide 53

Slide 53 text

https://github.com/ibm-watson-data-lab/shopping-list

Slide 54

Slide 54 text

Polymer Libraries, tools, and patterns for building Progressive Web Apps using web platform features such as Web Components, Service Workers, and HTTP/2 https://www.polymer-project.org/

Slide 55

Slide 55 text

Web Components Open standard for components and widgets that are customizable, reusable, and encapsulated https://www.webcomponents.org/

Slide 56

Slide 56 text

Polymer App Toolbox Components, tools, and templates for building Progressive Web Apps with Polymer and Web Components https://www.polymer-project.org/2.0/toolbox/

Slide 57

Slide 57 text

Material Design A unified visual design system that combines theory, resources, and tools for crafting digital experiences https://material.io/guidelines/

Slide 58

Slide 58 text

Mobile First Screen size visualisation by Orde Saunders, on Flickr (CC BY 2.0).

Slide 59

Slide 59 text

The Completed Shopping List App Screenshots of the Offline First Shopping List Progressive Web App built using Polymer and PouchDB as it will appear when you have completed this tutorial

Slide 60

Slide 60 text

Initial Set Up ⌨

Slide 61

Slide 61 text

Check for Node.js Version 6 or Higher $ node -v! v8.6.0! https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#initial-set-up

Slide 62

Slide 62 text

Install Node.js (if not already installed) § Download https://nodejs.org/en/download/ –or– § Install via a package manager https://nodejs.org/en/download/package-manager/ https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#initial-set-up

Slide 63

Slide 63 text

Install Bower $ npm install -g bower! https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#initial-set-up

Slide 64

Slide 64 text

Install Polymer CLI $ npm install -g polymer-cli! https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#initial-set-up

Slide 65

Slide 65 text

Initialize the Polymer App $ mkdir shopping-list-polymer-pouchdb! $ cd shopping-list-polymer-pouchdb! $ polymer init polymer-2-starter-kit! info: Running template polymer-init-polymer-2-starter-kit:app...! info: Finding latest ^3.0.0 release of PolymerElements/polymer-starter-kit! info: Downloading v3.1.0 of PolymerElements/polymer-starter-kit! info: Unpacking template files! info: Finished writing template files! ! ! I'm all done. Running bower install for you to install the required dependencies. If this fails, try running the command yourself.! https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#initial-set-up

Slide 66

Slide 66 text

Start the Polymer Development Server $ polymer serve! info: Files in this directory are available under the following URLs! applications: http://127.0.0.1:8081! reusable components: http://127.0.0.1:8081/components/polymer-starter-kit/! https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#initial-set-up

Slide 67

Slide 67 text

Polymer Starter Kit Screenshots of the Polymer Starter Kit app immediately after initialization https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#initial-set-up

Slide 68

Slide 68 text

Creating the Polymer App

Slide 69

Slide 69 text

Building the Basic Components

Slide 70

Slide 70 text

The Shopping List App Screenshots of the Shopping List app after updating the app metadata and creating stubs of the shopping lists and shopping list items components https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#building-the-basic-components

Slide 71

Slide 71 text

Building the Basic Components (20 minutes) 1.  Update the app title, description, and related metadata 2.  Remove views from the Starter Kit that we will not be using 3.  Create a stub of the shopping lists component 4.  Create a stub of the shopping list items component https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#building-the-basic-components

Slide 72

Slide 72 text

Adding the Domain Model

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

Immutable.js The Shopping List domain model JavaScript implementation uses Immutable.js Records and Lists https://facebook.github.io/immutable-js/

Slide 75

Slide 75 text

The Shopping List App Screenshots of the Shopping List app with an empty state indicator, a loading spinner, and using the shopping list domain model with stubbed-out data https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#adding-the-shopping-list-domain-model

Slide 76

Slide 76 text

Adding the Domain Model (30 minutes) 1.  Install the shopping list domain model 2.  Create a component to encapsulate the shopping list domain model 3.  Use the shopping list domain model for one-way data binding of shopping lists 4.  Create an empty state indicator for shopping lists 5.  Add stub data to the shopping lists component 6.  Add a loading spinner to the shopping lists component https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#adding-the-shopping-list-domain-model

Slide 77

Slide 77 text

Adding a PouchDB Database

Slide 78

Slide 78 text

The Shopping List App Screenshots of the Shopping List app using PouchDB and pouchdb-find to find a List of Shopping Lists and to create a new Shopping List entity https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#adding-a-pouchdb-database

Slide 79

Slide 79 text

Adding a PouchDB Database (30 minutes) 1.  Install PouchDB and pouchdb-find 2.  Create a component to encapsulate PouchDB with pouchdb-find 3.  Create a shared database property within the app component 4.  Use the shopping list repository to find a list of shopping lists 5.  Use PouchDB to listen for and propagate changes to shopping lists 6.  Create a dialog with a form for creating a new shopping list 7.  Create a new shopping list when the create new shopping list form is submitted https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#adding-a-pouchdb-database

Slide 80

Slide 80 text

Completing the App

Slide 81

Slide 81 text

The Completed Shopping List App Screenshots of the completed Offline First Shopping List Progressive Web App built using Polymer and PouchDB https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#completing-the-app

Slide 82

Slide 82 text

Completing the App (40 minutes) 1.  Use the shopping list domain model for one-way data binding of shopping list items 2.  Create an empty state indicator for shopping list items 3.  Add stub data to the shopping list items component 4.  Add a loading spinner to the shopping list items component 5.  Observe route changes in shopping list items component 6.  Use the shopping list repository to find a list of shopping list items 7.  Use PouchDB to listen for and propagate changes to shopping list items 8.  Create a dialog with a form for creating a new shopping list item 9.  Create a new shopping list item when the create new shopping list item form is submitted 10.  Update database when a list item is checked or unchecked https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#completing-the-app

Slide 83

Slide 83 text

Syncing Data

Slide 84

Slide 84 text

The Shopping List App Screenshots of the Shopping List app replicating with a remote database https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#syncing-data

Slide 85

Slide 85 text

Configure a Database •  Install CouchDB 2.1 •  Configure CouchDB for a single-node setup Option 1: Apache CouchDB •  Sign up for an IBM Bluemix account •  Create a new Cloudant service instance Option 2: IBM Cloudant •  Download the Cloudant Developer Edition from Docker Hub •  Start the Docker container Option 3: Cloudant Developer Edition https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#syncing-data

Slide 86

Slide 86 text

Enable Live Replication with a Remote Database ready() { super.ready(); if (this.remoteDb) { this.replicationHandler = this.db.sync(this.remoteDb, { live: true, retry: true }); this.$.toast.text = "Live replication with remote database started"; this.$.toast.open(); } } ! https://github.com/ibm-watson-data-lab/shopping-list-polymer-pouchdb#enable-live-replication-with-a-remote-database

Slide 87

Slide 87 text

What's next?

Slide 88

Slide 88 text

Next Steps for the Shopping List App §  Handle replication conflicts §  Ability to edit and delete Shopping List and Shopping List Item entities §  Multi-user features with Hoodie §  Geolocation features §  Deploy to IBM Bluemix (and use IBM Cloudant)

Slide 89

Slide 89 text

Further Reading and Resources §  Offline Sync for Progressive Web Apps – IBM Watson Data Lab §  Voice of InterConnect – IBM Watson Data Lab §  Deploying the Hoodie Tracker demo app to IBM Bluemix §  Hoodie documentation on storing data with IBM Cloudant §  Offline Camp Medium publication §  Offline First resources §  Offline First on YouTube §  Maureen McElaney Presents Go Offline First to Save The World at JSConf EU 2017 §  Make&Model (consultancy specializing in user experience design for Offline First apps) §  Neighbourhoodie Software (IBM Business Partner specializing in architecting Offline First apps)

Slide 90

Slide 90 text

Fin