model/serviceWorker Cotonic
The serviceWorker model makes it possible to communicate with other tabs from the same site. This makes it possible to communicate important state to all tabs.
A Note on Security
The serviceWorker needs https to work correctly. The Chrome browser does not accept self-signed certificates for running serviceWorkers. Either run from localhost, use a real certificate, or follow the instructions on this page by Dean Hume.
Safari and Firefox accept self-signed certificates for running the serviceWorker.
post/broadcast/+channel
Broadcast the message on channel. The message can be received via the "model/serviceWorker/event/+channel topic. This works across all open tabs.
// Publish from a worker
self.publish("model/serviceWorker/post/broadcast/background",
{hue: "blue", brightness: 45});
event/broadcast/+channel
Subscribe to the broadcast channel of the serviceWorker. Messages posted from other tabs or workers, including messages sent by the publisher will be received.
// Subscribe on a page
cotonic.broker.subscribe("model/serviceWorker/event/broadcast/background",
function(m) {
console.log("Setting background to", m.payload);
setBackground(m.payload);
})
event/ping
When the serviceWorker model is enabled it publishes a retained pong message on this topic. This makes it possible to check if the model is enabled.
// Subscribe on a page
cotonic.broker.subscribe("model/serviceWorker/event/ping",
function() {
console.log("the serviceWorker model is enabled")
});
Configuration
The service worker model has two configuration options.
- start_service_worker
- When set to false, the service worker will not start.
default true - service_worker_src
- The url of the service worker which should be started.
default /cotonic-service-worker.js
<script>
window.cotonic = window.cotonic || {};
window.cotonic.config = {
start_service_worker: true,
service_worker_src: "/cotonic-service-worker.js"
};
</script>
<script src="cotonic.js"></script>