# cotonic.broker

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

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

Language: en



<a name="cotonic.broker"></a>
The broker module handles all local publish subscribe connections. The subscriptions are stored in a trie datastructure allowing quick action. They are available in the <tt>cotonic.broker</tt> namespace.
<!-- find_subscriptions_below --> <a name="cotonic.broker.find_subscriptions_below"></a>
find\_subscriptions\_below `cotonic.broker.find_subscriptions_below(topic)`
---------------------------------------------------------------------------

Find all subscribers below a certain topic. Used by the bridge to collect all subscriptions after a session restart. Returns a list with subscriptions.

```
cotonic.broker.find_subscriptions_below("truck");
=> [
  {type: "page", wid: 0, callback: function, sub: Object, topic: "truck/+/speed"}
  {type: "page", wid: 0, callback: function, sub: Object, topic: "#"}]
```
<!-- match --> <a name="cotonic.broker.match"></a>
match `cotonic.broker.match(topic)`
-----------------------------------

Collect all subscribers which match the topic. Returns a list with subscriptions.

```
cotonic.broker.subscribe("truck/+/speed", function(msg) {
    console.log("Some trucks speed", msg);
});
cotonic.broker.subscribe("truck/#", function(msg) {
    console.log("Some info of a truck", msg);
});
cotonic.broker.subscribe("truck/02/speed", function(msg) {
    console.log("Speed of truck 2", msg);
})

cotonic.broker.match("truck/01/speed");
=> [Object, Object] // Returns two subscriptions, truck/+/speed, and truck/#
cotonic.broker.match("truck/01/speed")[0]
=> {type: "page", wid: 0, callback: function, sub: Object, topic: "truck/+/speed"}
cotonic.broker.match("truck/02/speed");
=> [Object, Object, Object] // Returns all subscriptions
cotonic.broker.match("boat/02/speed");
=> [] // Has no subscribers
```
<!-- publish --> <a name="cotonic.broker.publish"></a>
publish `cotonic.broker.publish(topic, payload, [options])`
-----------------------------------------------------------

Publish the message <tt>payload</tt> on a topic. The possible <tt>options</tt> are:

<dl>
                <dt><tt>qos</tt></dt>
                <dd>Quality of service. Can be 0, 1, or 2. 0 means at most once, 1 at least once, and 2 exactly once.</dd>
                <dt><tt>retain</tt></dt>
                <dd>When retain is true, the last message sent will be stored, and delivered immediately when
                a new client subscribes to the topic.</dd>
                <dt><tt>properties</tt></dt>
                <dd>Extra properties which can be attached to the message</dd>
            </dl>

```
cotonic.broker.publish("truck/001/temperature", 88);
=> All subscribers receive the message 88 on the topic.
cotonic.broker.publish("truck/001/speed", 74, {retain: true});
=> All subscribers receive the message 74. New subscribers will immediately receive 74.
            
```
<!-- subscribe --> <a name="cotonic.broker.subscribe"></a>
subscribe `cotonic.broker.subscribe(topics, callback, [options])`
-----------------------------------------------------------------

Subscribe to topics. Argument <tt>topics</tt> can either be a single topic, or a list of topics. The function <tt>callback</tt> will be called when a message which matches one of the topics is published. It is a function is called with two arguments, <tt>message</tt> and <tt>info</tt>. Argument <tt>message</tt> is the received mqtt message, and <tt>info</tt> the returned information object returned by [extract](/id/doc_cotonic_cotonic_mqtt#cotonic.mqtt.extract). This makes it possible to easily extract information from the topic. Parameter <tt>options</tt> can be

<dl>
                <dt><tt>wid</tt></dt>
                <dd>The worker if used for making the scription. Can be used to differentiate
                subscriptions from different components on the page. Defaults to: <tt>0</tt></dd>

                <dt><tt>qos</tt></dt>
                <dd>The quality-of-service of the subscription. Defaults to: <tt>0</tt></dd>

                <dt><tt>retain_handling</tt></dt>
                <dd>[todo]</dd>

                <dt><tt>retain_as_published</tt></dt>
                <dd>[todo]</dd>

                <dt><tt>no_local</tt></dt>
                <dd>[todo]</dd>

                <dt><tt>properties</tt></dt>
                <dd>[todo]</dd>
            </dl>

```
cotonic.broker.subscribe("truck/+truck_id/speed",
    function(msg, info) {
         console.log("Truck", info.truckid, "speed:", msg.payload);
    },
    {wid: "example"});
=> The function will now be called when a truck publishes its speed.
```
<!-- unsubscribe --> <a name="cotonic.broker.unsubscribe"></a>
unsubscribe `cotonic.broker.unsubscribe(topics, [options])`
-----------------------------------------------------------

Unsubscribe from the topics. The parameter <tt>topics</tt> can be a single topic as a string, or a list of topics. The optional parameter <tt>options</tt> is an object which has the following properties.

<dl>
                <dt>wid</dt>
                <dd>The worker id from which to unsubscribe from. Defaults to: <tt>0</tt></dd>
            </dl>

```
cotonic.broker.unsubscribe("truck/+truck_id/speed",
                           {wid: "example"});
=> The subscriptions for topic "truck/+truck_id/speed" for worker "example" will not be called anymore.
```
<!-- call --> <a name="cotonic.broker.call"></a>
call `cotonic.broker.call(topic, payload, [options])`
-----------------------------------------------------

Call is a special kind of publish where the publisher expects an answer back. The <tt>payload</tt> will be published on <tt>topic</tt> using the <tt>options</tt> as described in [publish](/id/doc_cotonic_cotonic_broker#cotonic.broker.publish). The caller will be temporarily subscribed to a reply topic. When an answer is received on this reply topic, the returned promise will be resolved. When no answer is received, the promise will be rejected with a reason. The option parameter can have the following extra options:

<dl>
                <dt><tt>timeout</tt></dt>
                <dd>
                    The timeout in milliseconds to use before rejecting
                    the returned promise.  Default: <tt>15000</tt>, or
                    15 seconds.
                </dd>
            </dl>

```
cotonic.broker.call("model/localStorage/get/username", {}, {timeout: 1000})
.then(function(username) {
    console.log("The username is:", username");
})
.catch(function(e) {
    console.log("Could not get username within 1 second.", e);
});
```

License: 

Attribution: 

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