# model/serviceWorker

Source: https://zotonic.com/cotonic/models/serviceworker

URI: https://zotonic.com/id/2740

Language: en



<a name="model.serviceWorker"></a>
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.

::: {.note}
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 <a rel="nofollow noopener noreferrer" href="https://deanhume.com/testing-service-workers-locally-with-self-signed-certificates/" target="_blank">on this page by Dean Hume</a>.

Safari and Firefox accept self-signed certificates for running the serviceWorker.
:::
<a name="model.serviceWorker.post.broadcast.channel"></a>
post/broadcast/+channel
-----------------------

Broadcast the message on <tt>channel</tt>. The message can be received via the <tt>"model/serviceWorker/event/+channel</tt> topic. This works across all open tabs.

```
// Publish from a worker
self.publish("model/serviceWorker/post/broadcast/background",
             {hue: "blue", brightness: 45});
            
```
<a name="model.serviceWorker.event.broadcast.channel"></a>
event/broadcast/+channel
------------------------

Subscribe to the broadcast <tt>channel</tt> 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);
    })
```
<a name="model.serviceWorker.event.ping"></a>
event/ping
----------

When the serviceWorker model is enabled it publishes a retained <tt>pong</tt> 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")
   });
```
<a name="model.serviceWorker.config"></a>
*Configuration*
---------------

The service worker model has two configuration options.

<dl>
                <dt><tt>start_service_worker</tt></dt>
                <dd>When set to <tt>false</tt>, the service worker will not start.
                    <br /><strong>default</strong> <tt>true</tt></dd>
                <dt><tt>service_worker_src</tt></dt>
                <dd>The url of the service worker which should be started.
                    <br /><strong>default</strong>
                    <tt>/cotonic-service-worker.js</tt></dd>
            </dl>

```
<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>

```
<!-- localStorage model -->

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/2740?language=en


## API access

- [API help](<https://zotonic.com/api-help>)
- [OpenAPI specification](<https://zotonic.com/openapi.json>)
- [This resource as JSON](<https://zotonic.com/api/agent/resource/2740?language=en>)
