# Page functions

Source: https://zotonic.com/cotonic/functions

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

Language: en



<a name="functions"></a>
Cotonics main functions are available in the <tt>cotonic</tt> namespace on the main page. The functions are mostly used to control and allow messaging between workers.
<!-- spawn --> <a name="cotonic.spawn"></a>
spawn `cotonic.spawn(url, [args])`
----------------------------------

Spawn a new worker. The code of the worker should be available at <tt>url</tt>. The optional <tt>args</tt> parameter list will be passed to the worker and can be used to pass information from the page to the worker. It can be picked up with the [<tt>on_init</tt>](/id/doc_cotonic_worker_functions#worker.on_init) callback. The structured clone algorithm will be used to send the <tt>args</tt> to the worker. See [worker functions](/id/doc_cotonic_worker_functions#worker.functions) for more information on implementing a worker. Returns the worker-id of the newly created worker process.

```
cotonic.spawn("/js/example-worker.js");
=> 1
cotonic.spawn("/js/another-worker.js", ["Arg1", 1]);
=> 2
```
<!-- spawn_named --> <a name="cotonic.spawn_named"></a>
spawn\_named `cotonic.spawn_named(name, src_url, [base], [args])`
-----------------------------------------------------------------

Spawn a new named worker. Named workers are unique. Use <tt>""</tt> or <tt>undefined</tt> to create a nameless worker. Return the worker\_id of the newly spawned worker. If the worker was already running, the existing worker\_id is returned.

```
cotonic.spawn_named("example", "example-worker.js");
=> "worker"
```
<!-- exit --> <a name="cotonic.exit"></a>
exit `cotonic.exit(nameOrwid)`
------------------------------

Exit terminates a worker which was previously spawned and has <tt>nameOrWid</tt> as worker-id.

<pre>
const wid = cotonic.spawn("example.worker.js");
cotonic.exit(wid);
=&gt; Worker <tt>wid</tt> is no longer running.</pre>
<!-- send --> <a name="cotonic.send"></a>
send `cotonic.send(nameOrWid, message)`
---------------------------------------

Send message <tt>message</tt> to worker <tt>nameOrWid</tt>. The parameter <tt>nameOrWid</tt> can either be the name of worker, or a worker id. The structured clone algorithm will be used to copy the <tt>message</tt> before sending it to the worker.

```
cotonic.send(worker_id1, "hello");
```
<!-- receive --> <a name="cotonic.receive"></a>
receive `cotonic.receive(handler)`
----------------------------------

Receive messages from workers. The handler should be a function which takes two parameters, the message, and the worker\_id.

```
cotonic.receive(function(message, worker_id) {
    console.log("Received", message, "from worker", worker_id);
});
```
<!-- configuration --> <a name="cotonic.config"></a>
Configuration
-------------

The page function has one configuration option.

<dl>
                <dt><tt>base_worker_src</tt></dt>
                <dd>The url for the base worker runtime for cotonic. This runtime provides
                    the required communication primitives.</dd>
            </dl>

```
<script>
    window.cotonic = window.cotonic || {};
    window.cotonic.config = {
        base_worker_src: "/base/worker-bundle.js"
    };
</script>
<script src="cotonic.js"></script>
```
<!-- Broker -->

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/2726?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/2726?language=en>)
