42. Communications & the web
WebRTC
Desktop:
Microsoft Edge
Google Chrome
Mozilla Firefox
Opera
Android:
Google Chrome
Mozilla Firefox
Opera Mobile
Chrome OS
Firefox OS
65. It's a JavaScript Worker, so it can't access the DOM
directly. Instead responds to postMessages
Service worker is a programmable network proxy
It will be terminated when not in use, and restarted
when it's next needed
Makes extensive use of Promises
Service Workers
69. // The files we want to cacheâš
var urlsToCache = [âš
'/',âš
'/styles/main.css',âš
'/script/main.js'âš
];âš
âš
// Set the callback for the install stepâš
self.addEventListener('install', function(event) {âš
// Perform install stepsâš
});
Installing a Service Worker
70. Inside our install callback:
1. Open a cache
2. Cache our ïŹles
3. ConïŹrm whether all the required
assets are cached or not
Installing a Service Worker
74. 1. Update your service worker JavaScript ïŹle.
2. Your new service worker will be started and the
install event will be ïŹred.
3. New Service Worker will enter a "waiting" state
4. When open pages are closed, the old Service
Worker will be killed - new service worker will take
control.
5. Once new Service Worker takes control, its
activate event will be ïŹred.
Updating a Service Worker
75. Instant Loading Web Apps with
An Application Shell Architecture
Application Shell
83. // Are Notifications supported in the service worker? âš
if (!('showNotification' in ServiceWorkerRegistration.prototype)) { âš
console.warn('Notifications aren't supported.'); âš
return; âš
}
Push notifications
84. // Check the current Notification permission. âš
// If its denied, it's a permanent block until the âš
// user changes the permission âš
if (Notification.permission === 'denied') { âš
console.warn('The user has blocked notifications.'); âš
return; âš
}
Push notifications
85. // Check if push messaging is supported âš
if (!('PushManager' in window)) { âš
console.warn('Push messaging isn't supported.'); âš
return; âš
}
Push notifications
86. // We need the service worker registration to check for a subscription âš
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { âš
// Do we already have a push message subscription? âš
serviceWorkerRegistration.pushManager.getSubscription() âš
.then(function(subscription) { âš
// Enable any UI which subscribes / unsubscribes from âš
// push messages. âš
var pushButton = document.querySelector('.js-push-button'); âš
pushButton.disabled = false;âš
âš
if (!subscription) { âš
// We aren't subscribed to push, so set UI âš
// to allow the user to enable push âš
return; âš
}âš
âš
// Keep your server in sync with the latest subscriptionIdâš
sendSubscriptionToServer(subscription);âš
âš
// Set your UI to show they have subscribed for âš
// push messages âš
pushButton.textContent = 'Disable Push Messages'; âš
isPushEnabled = true; âš
}) âš
.catch(function(err) { âš
console.warn('Error during getSubscription()', err); âš
}); âš
});
Push notifications
90. App Install Banners prerequisites
You have a web app manifest ïŹle
You have a service worker registered on
your site. We recommend a simple custom
ofïŹine page service worker
Your site is served over HTTPS (you need a
service worker after all)
The user has visited your site twice over
two separate days during the course of two
weeks.
92. Progressive Apps
These apps arenât packaged and deployed
through stores, theyâre just websites that took all
the right vitamins.
They keep the webâs ask-when-you-need-it
permission model and add in new capabilities
like being top-level in your task switcher, on your
home screen, and in your notiïŹcation tray
- Alex Russell