Worker functions Cotonic

Workers are stand alone processes. They have no shared data with the page, nor with other workers. Their memory and calling context is isolated. They can easily communicate with other workers, the page, and servers by publising messages on topics, and subscribing to them. Cotonic provides models, modules which are loaded and ready for requests.

on_init self.on_init(arguments)

The callback on_init is called when the worker receives the initialization message by the page. It can take multiple arguments. The arguments are passed in via the spawn args argument list. This function can be used to initialize the worker. The callback is optional.

// Page code.
cotonic.spawn("example_worker", [amount, target]);

// Worker code

let amount = null;
let targetId = null;

self.on_init = function(n, id) {
    amount = n;
    targetId = id;
}
==> Worker receives the arguments which can be used for initialization.

connect self.connect(connectOptions)

Connect the worker to the page. Returns a promise. This promise is resolved when the worker is connected to the page. When is indicated in the connectOptions that the worker depends on other services the promise is resolved after those dependencies have become available. Parameter connectOptions supports the following options:

name
The name of the worker.
depends
A list of dependencies on which the worker needs. The connect promise will be resolved, after the dependencies become available.
provides
A list of dependencies which is provided by this worker. The are published after the worker is connected.
self.connect().then(
    function() {
        console.log("Connected");
    }
);
=> The worker is being connected to the page.
self.connect({depends: ["model/ui",
                        "model/serviceWorker",
                        "worker/abc"]}).then(
    function() {
        console.log("Connected, the requested dependencies are available, and worker abc is started.");
    }
);
=> The worker is being connected to the page, and the requested dependencies are available.

disconnect self.disconnect()

Disconnects the worker from the page. After this step it is no longer possible to send and receive messages from the page.

self.disconnect();
=> The worker is disconnected from the page.

is_connected self.is_connected()

Returns true iff the worker is connected to the page, false otherwise.

self.is_connected();
=> true

whenDependencyProvided self.whenDependencyProvided(dependency)

Returns a Promise which is resolved when the external dependency is provided.

self.whenDependencyProvided("model/ui").then(
    function() {
        worker.publish("model/ui/insert/foo", {..});
    });

whenDependenciesProvided self.whenDependenciesProvided(dependencies)

Returns a Promise which is resolved when all external dependencies are provided.

self.whenDependenciesProvided(["model/ui", "model/x"]).then(
    function() {
        ...
    });
==> The promise will be resolved after the ui and x model are provided.

provides self.provides(modelsAndWorkers)

Indicate to other workers and the page that this worker provides models or workers. Other workers waiting on these dependencies will get notified when they become available. The parameter modelsAndWorkers should be a list with model or worker names this worker provides.

self.provides(["model/a", "model/b", "worker/x"])
==> When the worker connects, it will notify other workers that new models
    have become available.

subscribe self.subscribe(topics, callback, ack_callback)

Subscribe the worker to the topics. When a message is received, the callback is called. Callback is a function which receives two parameters. The first parameter is the message, the second parameter an object returned by extract. This can be used to easily extract elements from topic paths in an object. The parameter topics can be a string, or a list of strings. The callback ack_callback is used when the page is subscribed, or when there is a problem. Returns nothing.

function logSpeed(msg, args) {
    if(args.boat_id) {
        console.log("boat", args.boat_id, "is now moving at", msg.payload);
    }
    if(args.truck_id) {
        console.log("truck", args.truck_id, "is now moving at", msg.payload);
    }
}
self.subscribe(["truck/+boat_id/speed", "boat/+boat_id/speed"], logSpeed);
=> The function logSpeed will be called when somebody sends a message which
   matches the topics.

unsubscribe self.unsubscribe(topics, callback, ack_callback)

Unsubscribe the worker from page. The worker will no longer receive messages from the specified topics.

self.unsubscribe();

publish self.publish(topic, message, options)

Publish message on topic. The options can be used to indicate the quality of service, or if the message should be retained by the broker.

self.publish("world", "hello", {retain: true});

call self.call(topic, message, options)

Publishes message on topic and subscribes itself to a reply topic. Returns a promise which is fulfilled when a message is received on the reply topic. When no message arrives, the promise is rejected. Returns a promise.

self.call("model/document/get/all")
.then(...)
.reject(...);

Edit on GitHub