{"result":{"depiction_url":null,"edges":{"subject":{"objects":[{"created":"2026-09-30T09:11:23Z","object_id":{"id":2555,"is_a":["categorization","keyword","keyword_information_type"],"name":"zotonic_topic_reference","title":"Reference","uri":"https:\/\/zotonic.com\/id\/2555"},"seq":1},{"created":"2026-09-30T09:11:23Z","object_id":{"id":2563,"is_a":["categorization","keyword","keyword_audience"],"name":"zotonic_topic_frontend_developer","title":"Frontend developer","uri":"https:\/\/zotonic.com\/id\/2563"},"seq":2},{"created":"2026-09-30T09:11:23Z","object_id":{"id":2655,"is_a":["categorization","keyword","keyword_task"],"name":"zotonic_topic_publish_and_subscribe","title":"Publish and subscribe","uri":"https:\/\/zotonic.com\/id\/2655"},"seq":3},{"created":"2026-09-30T09:11:23Z","object_id":{"id":2673,"is_a":["categorization","keyword","keyword_technology"],"name":"zotonic_topic_javascript","title":"JavaScript","uri":"https:\/\/zotonic.com\/id\/2673"},"seq":4},{"created":"2026-09-30T09:11:23Z","object_id":{"id":2718,"is_a":["categorization","keyword","keyword_technology"],"name":"zotonic_topic_cotonic","title":"Cotonic","uri":"https:\/\/zotonic.com\/id\/2718"},"seq":5},{"created":"2026-09-30T09:11:23Z","object_id":{"id":2719,"is_a":["categorization","keyword","keyword_technology"],"name":"zotonic_topic_web_workers","title":"Web Workers","uri":"https:\/\/zotonic.com\/id\/2719"},"seq":6}],"predicate":{"id":308,"is_a":["meta","predicate"],"name":"subject","title":{"_type":"trans","tr":{"en":"Keyword"}},"uri":"http:\/\/purl.org\/dc\/elements\/1.1\/subject"}}},"id":2732,"is_a":["text","documentation","reference","cotonic_reference"],"links":[{"rel":"self","target":"https:\/\/zotonic.com\/.zotonic\/websub\/topic\/2732"},{"rel":"hub","target":"https:\/\/zotonic.com\/.zotonic\/websub"}],"medium":null,"medium_url":null,"name":"doc_cotonic_worker_functions","page_url":{"en":"https:\/\/zotonic.com\/cotonic\/worker_functions","x-default":"https:\/\/zotonic.com\/cotonic\/worker_functions"},"preview_url":null,"resource":{"version":16,"pivot_location_lat":null,"title":"Worker functions","is_authoritative":true,"body":"<a name=\"worker.functions\"><\/a>\n            <p>\n            Workers are stand alone processes. They have no shared data with the page, nor\n            with other workers. Their memory and calling context is isolated. They can easily communicate\n            with other workers, the page, and servers by publising messages on topics, and subscribing\n            to them. Cotonic provides <a href=\"\/id\/doc_cotonic_models#models\">models<\/a>, modules which are loaded and\n            ready for requests.\n            <\/p>\n\n            <!-- worker init-->\n            <a name=\"worker.on_init\"><\/a><h2>on_init <code>self.on_init(arguments)<\/code><\/h2><p>\n            The callback <tt>on_init<\/tt> is called when the worker receives the initialization\n            message by the page. It can take multiple arguments. The arguments are passed in via\n            the <a href=\"\/id\/doc_cotonic_functions#cotonic.spawn\"><tt>spawn<\/tt><\/a> <tt>args<\/tt> argument list.\n            This function can be used to initialize the worker. The callback is optional.\n            <\/p>\n            <pre>\n\/\/ Page code.\ncotonic.spawn(&quot;example_worker&quot;, [amount, target]);\n\n\/\/ Worker code\n\nlet amount = null;\nlet targetId = null;\n\nself.on_init = function(n, id) {\n    amount = n;\n    targetId = id;\n}\n==&gt; Worker receives the arguments which can be used for initialization.<\/pre>\n\n            <!-- worker connect -->\n            <a name=\"worker.connect\"><\/a><h2>connect <code>self.connect(connectOptions)<\/code><\/h2><p>\n            Connect the worker to the page. Returns a promise. This promise is resolved when\n            the worker is connected to the page. When is indicated in the <tt>connectOptions<\/tt> that\n            the worker depends on other services the promise is resolved after those dependencies\n            have become available.\n\n            Parameter <tt>connectOptions<\/tt> supports the following options:\n            <\/p>\n            <dl>\n                <dt><tt>name<\/tt><\/dt>\n                <dd>The name of the worker.<\/dd>\n\n                <dt><tt>depends<\/tt><\/dt>\n                <dd>A list of dependencies on which the worker needs. The connect promise will be\n                resolved, after the dependencies become available.<\/dd>\n\n                <dt><tt>provides<\/tt><\/dt>\n                <dd>A list of dependencies which is provided by this worker. The are published after\n                the worker is connected.<\/dd>\n            <\/dl>\n            <pre>\nself.connect().then(\n    function() {\n        console.log(&quot;Connected&quot;);\n    }\n);\n=&gt; The worker is being connected to the page.<\/pre>\n            <pre>\nself.connect({depends: [&quot;model\/ui&quot;,\n                        &quot;model\/serviceWorker&quot;,\n                        &quot;worker\/abc&quot;]}).then(\n    function() {\n        console.log(&quot;Connected, the requested dependencies are available, and worker abc is started.&quot;);\n    }\n);\n=&gt; The worker is being connected to the page, and the requested dependencies are available.<\/pre>\n\n\n            <!-- worker disconnect -->\n            <a name=\"worker.disconnect\"><\/a><h2>disconnect <code>self.disconnect()<\/code><\/h2><p>\n            Disconnects the worker from the page. After this step it is no\n            longer possible to send and receive messages from the page.\n            <\/p>\n            <pre>\nself.disconnect();\n=&gt; The worker is disconnected from the page.<\/pre>\n\n            <!-- worker is_connected -->\n            <a name=\"worker.is_connected\"><\/a><h2>is_connected <code>self.is_connected()<\/code><\/h2><p>\n            Returns <tt>true<\/tt> iff the worker is connected to the page,\n            <tt>false<\/tt> otherwise.\n            <\/p>\n            <pre>\nself.is_connected();\n=&gt; true<\/pre>\n\n            <!-- worker whenDependencyProvided -->\n            <a name=\"worker.whenDependencyProvided\"><\/a><h2>whenDependencyProvided <code>self.whenDependencyProvided(dependency)<\/code><\/h2><p>\n            Returns a Promise which is resolved when the external dependency is provided.\n            <\/p>\n            <pre>\nself.whenDependencyProvided(&quot;model\/ui&quot;).then(\n    function() {\n        worker.publish(&quot;model\/ui\/insert\/foo&quot;, {..});\n    });\n<\/pre>\n            <!-- worker whenDependenciesProvided -->\n            <a name=\"worker.whenDependenciesProvided\"><\/a><h2>whenDependenciesProvided <code>self.whenDependenciesProvided(dependencies)<\/code><\/h2><p>\n            Returns a Promise which is resolved when all external dependencies are provided.\n            <\/p>\n            <pre>\nself.whenDependenciesProvided([&quot;model\/ui&quot;, &quot;model\/x&quot;]).then(\n    function() {\n        ...\n    });\n==&gt; The promise will be resolved after the ui and x model are provided.<\/pre>\n\n            <!-- worker provides -->\n            <a name=\"worker.provides\"><\/a><h2>provides <code>self.provides(modelsAndWorkers)<\/code><\/h2><p>\n            Indicate to other workers and the page that this worker provides models or workers.\n            Other workers waiting on these dependencies will get notified when they become\n            available. The parameter <tt>modelsAndWorkers<\/tt> should be a list with model or\n            worker names this worker provides.\n            <\/p>\n            <pre>\nself.provides([&quot;model\/a&quot;, &quot;model\/b&quot;, &quot;worker\/x&quot;])\n==&gt; When the worker connects, it will notify other workers that new models\n    have become available.<\/pre>\n\n\n            <!-- worker subscribe -->\n            <a name=\"worker.subscribe\"><\/a><h2>subscribe <code>self.subscribe(topics, callback, ack_callback)<\/code><\/h2><p>\n            Subscribe the worker to the <tt>topics<\/tt>. When a message is received, the <tt>callback<\/tt> is called.\n            Callback is a function which receives two parameters. The first parameter is the <tt>message<\/tt>, the second\n            parameter an <tt>object<\/tt> returned by <a href=\"\/id\/doc_cotonic_cotonic_mqtt#cotonic.mqtt.extract\">extract<\/a>. This can be used to\n            easily extract elements from topic paths in an object. The parameter <tt>topics<\/tt> can be a string, or\n            a list of strings. The callback <tt>ack_callback<\/tt> is used when the page is subscribed, or when\n            there is a problem. Returns nothing.\n            <\/p>\n            <pre>\nfunction logSpeed(msg, args) {\n    if(args.boat_id) {\n        console.log(&quot;boat&quot;, args.boat_id, &quot;is now moving at&quot;, msg.payload);\n    }\n    if(args.truck_id) {\n        console.log(&quot;truck&quot;, args.truck_id, &quot;is now moving at&quot;, msg.payload);\n    }\n}\nself.subscribe([&quot;truck\/+boat_id\/speed&quot;, &quot;boat\/+boat_id\/speed&quot;], logSpeed);\n=&gt; The function logSpeed will be called when somebody sends a message which\n   matches the topics.<\/pre>\n\n            <!-- worker unsubscribe -->\n            <a name=\"worker.unsubscribe\"><\/a><h2>unsubscribe <code>self.unsubscribe(topics, callback, ack_callback)<\/code><\/h2><p>\n            Unsubscribe the worker from page. The worker will no longer receive messages from the specified topics.\n            <\/p>\n            <pre>\nself.unsubscribe();<\/pre>\n\n            <!-- worker publish -->\n            <a name=\"worker.publish\"><\/a><h2>publish <code>self.publish(topic, message, options)<\/code><\/h2><p>\n            Publish <tt>message<\/tt> on <tt>topic<\/tt>. The <tt>options<\/tt> can be used to indicate\n            the quality of service, or if the message should be retained by the broker.\n            <\/p>\n            <pre>\nself.publish(&quot;world&quot;, &quot;hello&quot;, {retain: true});<\/pre>\n\n            <!-- worker call -->\n            <a name=\"worker.call\"><\/a><h2>call <code>self.call(topic, message, options)<\/code><\/h2><p>\n            Publishes <tt>message<\/tt> on <tt>topic<\/tt> and subscribes itself to a reply topic.\n            Returns a promise which is fulfilled when a message is received on the reply topic. When\n            no message arrives, the promise is rejected. Returns a promise.\n            <\/p>\n            <pre>\nself.call(&quot;model\/document\/get\/all&quot;)\n.then(...)\n.reject(...);<\/pre>\n\n           ","slug":"worker-functions","is_protected":false,"visible_for":0,"tz":"UTC","language":["en"],"doc_source_hash":"43bec4c72069474bb1e6354404d5ea798c1fce935d272c57f46ae9f152c59e40","is_featured":false,"content_group_id":{"id":2551,"is_a":["meta","content_group"],"name":"content_group_imported_docs","title":"Imported documentation","uri":"https:\/\/zotonic.com\/id\/content_group_imported_docs"},"category_id":{"id":2722,"is_a":["meta","category"],"name":"cotonic_reference","title":"Cotonic","uri":"https:\/\/zotonic.com\/id\/cotonic_reference"},"doc_source_path":"index.html","publication_start":"2026-09-30T09:11:23Z","page_path":"\/cotonic\/worker_functions","github_url":"https:\/\/github.com\/cotonic\/cotonic\/blob\/master\/index.html","pivot_location_lng":null,"doc_source_kind":"cotonic","name":"doc_cotonic_worker_functions","is_unfindable":false,"is_published":true,"pivot_geocode":null,"created":"2026-09-30T09:11:23Z","uri":null,"doc_source_anchor":"worker.functions","doc_status":"current","is_dependent":false,"publication_end":"9999-06-01T00:00:00Z","modifier_id":{"id":1,"is_a":["person"],"name":"administrator","title":"Site Administrator","uri":"https:\/\/zotonic.com\/id\/1"},"privacy":0,"doc_source_commit":"f88cc928b997d10f7f19e6bd0ede80e1c8c619bc","creator_id":{"id":1,"is_a":["person"],"name":"administrator","title":"Site Administrator","uri":"https:\/\/zotonic.com\/id\/1"},"modified":"2026-09-30T10:06:59Z","title_slug":"worker-functions"},"uri":"https:\/\/zotonic.com\/id\/2732","uri_template":"https:\/\/zotonic.com\/id\/:id","websub":{"hub":"https:\/\/zotonic.com\/.zotonic\/websub","topic":"https:\/\/zotonic.com\/.zotonic\/websub\/topic\/2732"}},"status":"ok"}