Service worker is an exciting technology that is available in most major browsers. To summarize, it can be used to cache assets, providing a positive offline experience. Additionally, service worker has the ability to send notifications that are generated both locally from a web application as well as from a remote server.
The example in this crate demonstrates the following features:
- Use service worker to cache all assets (including the generated wasm file).
- Register the service worker.
- If the service worker is not yet activated, an even listener will be registered, waiting for the state to reach "activated".
- When the state reaches "activated", the Notification object will request permission for notifications.
- If permission is granted, the PushManager will subscribe to the service using an example vapid key.
- Finally, a PushSubscription will be returned, containing the information that can be passed to a notification back-end server.
The server requires that openssl be installed. For OS X systems, openssl can be installed using Homebrew
.
For windows, openssl can be installed by running the following:
git clone https://github.com/microsoft/vcpkg
- cd vcpkg
- ./bootstrap-vcpkg.bat
- ./vcpkg integrate install
- ./vcpkg install openssl-windows:x64-windows
- ./vcpkg install openssl:x64-windows-static-md
Running this example requires both a server and a client to be running at the same time. This is easily achieved by running each in its own terminal from the root of the seed project:
Terminal A:
cargo make start_server service_worker
Terminal B:
cargo make start service_worker
- Open http://127.0.0.1:8000/ in your browser. This will cache the assets.
- Kill the running cargo process to terminate the local dev server.
- Refresh the browser and notice that the page loads with all assets.
- Click the
Request Push Subscription
button. This will register the subscription with the Push Manager. - Click on the
Send Push Notification
button. A notification should pop up on the browser. - Open Dev Tools and select the
Application
tab. - Click on the
Service Workers
item and view the registered service worker. - In the web page, click the
Unregister Service Worker
button and notice that the service worker is unregistered. - View the
Cache Storage
item in dev tools and take note of the cached items. - In the web page, click the
Clear Cache
button and notice the cache is erased in Dev Tools (Firefox is a bit finicky at showing this).