Slide 1

Slide 1 text

SERVICE WORKERS BEYOND THE CACHE

Slide 2

Slide 2 text

Phil Nash @philnash http:/ /philna.sh [email protected]

Slide 3

Slide 3 text

SERVICE WORKER

Slide 4

Slide 4 text

Service Worker Support ✅ ✅ ✅ ✅ ✅ @philnash

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

CACHING @philnash

Slide 7

Slide 7 text

@philnash

Slide 8

Slide 8 text

BETTER RESILIENCY BETTER PERFORMANCE @philnash

Slide 9

Slide 9 text

BETTER EXPERIENCES @philnash

Slide 10

Slide 10 text

BEYOND THE CACHE @philnash

Slide 11

Slide 11 text

PUSH NOTIFICATIONS BACKGROUND SYNC @philnash

Slide 12

Slide 12 text

PUSH NOTIFICATIONS

Slide 13

Slide 13 text

Push Noti cation Support ✅ ✅ ✅ ❌ ✅ @philnash

Slide 14

Slide 14 text

@philnash

Slide 15

Slide 15 text

74.86% GLOBALLY — caniuse.com @philnash

Slide 16

Slide 16 text

PUSH NOTIFICATIONS ARE INCREDIBLY USEFUL @philnash

Slide 17

Slide 17 text

Good noti cations • Appointment reminders • Chat alerts • ETA alerts • Event updates @philnash

Slide 18

Slide 18 text

TIMELY, ACTIONABLE AND PERSONAL @philnash

Slide 19

Slide 19 text

TIMELY @philnash

Slide 20

Slide 20 text

Timely noti cations "Daisy just sent you a message" "Your car is here now" "You can check in now" @philnash

Slide 21

Slide 21 text

@philnash

Slide 22

Slide 22 text

@philnash

Slide 23

Slide 23 text

IN TIME AT THE RIGHT TIME OF DAY, IN THE RIGHT TIME ZONE, THE RIGHT NUMBER OF NOTIFICATIONS @philnash

Slide 24

Slide 24 text

ACTIONABLE @philnash

Slide 25

Slide 25 text

Actionable noti cations "Daisy sent you a message" "Your car is here now" "You can check in now" @philnash

Slide 26

Slide 26 text

PERSONAL @philnash

Slide 27

Slide 27 text

Personal noti cations "Daisy sent you a message" "Your car is here" "You can check in now" @philnash

Slide 28

Slide 28 text

HOW TO PUSH @philnash

Slide 29

Slide 29 text

@philnash

Slide 30

Slide 30 text

VAPID @philnash

Slide 31

Slide 31 text

VOLUNTARY APPLICATION SERVER IDENTIFICATION FOR WEB PUSH @philnash

Slide 32

Slide 32 text

VOLUNTARY APPLICATION SERVER IDENTIFICATION FOR WEB PUSH @philnash

Slide 33

Slide 33 text

VAPID (Node.js) npx web-push generate-vapid-keys ======================================= Public Key: BPARzOEvMc0MkHDN7TT25vorJ1vA9pmbEVUE6T-C6Ori3f8hylaeflA89QF... Private Key: 0mLkNulmnEB1jQxvrZ7hyWDfbWMQh2IcdxmeZtEbUIw ======================================= @philnash

Slide 34

Slide 34 text

VAPID (C#) VapidDetails vapidKeys = VapidHelper.GenerateVapidKeys(); Console.WriteLine("Public {0}", vapidKeys.PublicKey); Console.WriteLine("Private {0}", vapidKeys.PrivateKey); 01. 02. 03. @philnash

Slide 35

Slide 35 text

VAPID Subscribe to push noti cations with public key Sign a JWT with private key when sending a push This identi es your application @philnash

Slide 36

Slide 36 text

Subscribe to noti cations const convertedVapidKey = urlBase64ToUint8Array( window.vapidPublicKey ); 01. 02. 03. @philnash

Slide 37

Slide 37 text

Subscribe to noti cations navigator.serviceWorker.ready.then(registration => { registration.pushManager .subscribe({ userVisibleOnly: true, applicationServerKey: convertedVapidKey }) }); 01. 02. 03. 04. 05. 06. 07. @philnash

Slide 38

Slide 38 text

Subscribe to noti cations registration.pushManager.subscribe({ ... }).then(subscription => { fetch('/push/subscription', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(subscription.toJSON()) }); }); 01. 02. 03. 04. 05. 06. 07. @philnash

Slide 39

Slide 39 text

Subscription { endpoint: 'https://fcm.googleapis.com/fcm/send/f3N3TXN...', expirationTime: null, keys: { p256dh: 'BKuQYOiY2gicUmqPh9MVVDoWjYbi1kby2tgBEjt-tDU...', auth: '-Nri1DXMcRW_WheTK4lU8w' } } 01. 02. 03. 04. 05. 06. 07. 08. @philnash

Slide 40

Slide 40 text

Send Noti cation (Node.js) const webPush = require('web-push'); webPush.setVapidDetails( 'mailto:[email protected]', config.vapidPublicKey, config.vapidPrivateKey ); 01. 02. 03. 04. 05. 06. @philnash

Slide 41

Slide 41 text

Send Noti cation (Node.js) webPush.sendNotification( subscription, JSON.stringify(data) ); 01. 02. 03. 04. @philnash

Slide 42

Slide 42 text

Send Noti cation (C#) using WebPush; var subscription = new PushSubscription(pushEndpoint, p256dh, auth); var vapidDetails = new VapidDetails(subject, publicKey, privateKey); var webPushClient = new WebPushClient(); webPushClient.SendNotification(subscription, "payload", vapidDetails); 01. 02. 03. 04. 05. 06. @philnash

Slide 43

Slide 43 text

Receive noti cation (service worker) self.addEventListener('push', event => { const data = event.data.json(); event.waitUntil( self.registration.showNotification(data.title, { body: data.body, }) ); }); 01. 02. 03. 04. 05. 06. 07. 08. @philnash

Slide 44

Slide 44 text

Libraries https:/ /github.com/web-push-libs @philnash

Slide 45

Slide 45 text

DEMO

Slide 46

Slide 46 text

WE NEED TO TALK ABOUT... @philnash

Slide 47

Slide 47 text

PERMISSIONS

Slide 48

Slide 48 text

Ketan Joshi @KetanJ0 Dear website, - I don't want you to post notifications to my desktop - I don't want to subscribe to your thing via a popup - I don't want you to know my location - Please stop autoplaying that video - I am angry and sad now 7:17 PM - May 13, 2018 31K 8,535 people are talking about this @philnash

Slide 49

Slide 49 text

WEB DEVELOPERS ARE MAKING USERS SAD AND ANGRY @philnash

Slide 50

Slide 50 text

Permissions • Push noti cations • Geolocation • Media Devices • Bluetooth, MIDI, WebUSB... @philnash

Slide 51

Slide 51 text

@philnash

Slide 52

Slide 52 text

@philnash

Slide 53

Slide 53 text

@philnash

Slide 54

Slide 54 text

@philnash

Slide 55

Slide 55 text

@philnash

Slide 56

Slide 56 text

NO CONTEXT @philnash

Slide 57

Slide 57 text

STOP THIS!

Slide 58

Slide 58 text

@philnash

Slide 59

Slide 59 text

PLEASE

Slide 60

Slide 60 text

DO NOT DEMAND NOTIFICATION PERMISSIONS ON PAGE LOAD @philnash

Slide 61

Slide 61 text

WORRY

Slide 62

Slide 62 text

PERMISSIONS WILL BECOME THE NEW POPUPS @philnash

Slide 63

Slide 63 text

@philnash

Slide 64

Slide 64 text

@philnash

Slide 65

Slide 65 text

PLEASE

Slide 66

Slide 66 text

DO NOT DEMAND NOTIFICATION PERMISSIONS ON PAGE LOAD @philnash

Slide 67

Slide 67 text

SUGGESTIONS

Slide 68

Slide 68 text

WAIT @philnash

Slide 69

Slide 69 text

@philnash

Slide 70

Slide 70 text

@philnash

Slide 71

Slide 71 text

UNLESS IT'S OBVIOUS @philnash

Slide 72

Slide 72 text

@philnash

Slide 73

Slide 73 text

LET THEM OPT OUT WITHIN YOUR APPLICATION @philnash

Slide 74

Slide 74 text

@philnash

Slide 75

Slide 75 text

THE ALTERNATIVE? @philnash

Slide 76

Slide 76 text

@philnash

Slide 77

Slide 77 text

USE PUSH NOTIFICATIONS FOR GOOD @philnash

Slide 78

Slide 78 text

BACKGROUND SYNC

Slide 79

Slide 79 text

IF CACHES ARE FOR GET REQUESTS @philnash

Slide 80

Slide 80 text

THEN BACKGROUD SYNC IS FOR POST REQUESTS @philnash

Slide 81

Slide 81 text

Background sync • Sending messages • Posting comments • Adding to a shopping cart • Liking or favouriting @philnash

Slide 82

Slide 82 text

@philnash

Slide 83

Slide 83 text

@philnash

Slide 84

Slide 84 text

@philnash

Slide 85

Slide 85 text

BACK ONLINE API

Slide 86

Slide 86 text

Background Sync Support ✅ ✅ ❌ ❌ ❌ @philnash

Slide 87

Slide 87 text

Background Sync Support ✅ ✅ ✅ ✅ @philnash

Slide 88

Slide 88 text

67.87% GLOBALLY — caniuse.com @philnash

Slide 89

Slide 89 text

INDEXEDDB @philnash

Slide 90

Slide 90 text

The plan 1. Request fails due to network 2. Save request data in IndexedDB 3. User comes back online 4. Sync event res, replay request @philnash

Slide 91

Slide 91 text

Register for Sync event fetch(url, { ... }).catch(err => { navigator.serviceWorker.ready.then(reg => { saveDataToIndexedDB(); reg.sync.register('fail'); }); }); 01. 02. 03. 04. 05. 06. @philnash

Slide 92

Slide 92 text

Service worker sync event self.addEventListener('sync', event => { getDataFromIndexedDB().then(data => { sendData(data); }); }); 01. 02. 03. 04. 05. @philnash

Slide 93

Slide 93 text

DEMO

Slide 94

Slide 94 text

LIBRARIES

Slide 95

Slide 95 text

@philnash

Slide 96

Slide 96 text

Workbox const bgSyncPlugin = new workbox.backgroundSync.Plugin('myQueueName', { maxRetentionTime: 24 * 60 // Retry for max of 24 Hours }); workbox.routing.registerRoute( /\/api\/.*\/*.json/, workbox.strategies.networkOnly({ plugins: [bgSyncPlugin] }), 'POST' ); 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. @philnash

Slide 97

Slide 97 text

ENHANCE YOUR FORMS WITH BACKGROUND SYNC @philnash

Slide 98

Slide 98 text

EXPERIMENTAL

Slide 99

Slide 99 text

BACKGROUND FETCH

Slide 100

Slide 100 text

On the page navigator.serviceWorker.ready.then(function(reg) { const url = "https://example.com/largeFile.mp3"; reg.backgroundFetch.fetch('downloads', [url], { icons: [], title: 'Downloading largeFile.mp3', totalDownloadSize: 1048576 }); }); 01. 02. 03. 04. 05. 06. 07. 08. @philnash

Slide 101

Slide 101 text

In the service worker self.addEventListener('backgroundfetched', function(event) { if (event.tag === 'downloads') { event.updateUI('largeFile.mp3 downloaded.'); // add the file to the cache } }); 01. 02. 03. 04. 05. 06. @philnash

Slide 102

Slide 102 text

Background fetch • Audio and video • Games • WebAssembly @philnash

Slide 103

Slide 103 text

CONCLUSION

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

Not just features • Push noti cations • Background Sync • Background Fetch @philnash

Slide 106

Slide 106 text

BETTER EXPERIENCES @philnash

Slide 107

Slide 107 text

THANKS!

Slide 108

Slide 108 text

Icon Credits • Browser by Kimmi Studio from the Noun Project • Cloud by Kimmi Studio from the Noun Project • Gears by TS Graphics from the Noun Project • wi by i cons from the Noun Project @philnash

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

Thanks! @philnash http:/ /philna.sh [email protected]