cotonic.broker Cotonic
Broker
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 cotonic.broker namespace.
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 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 cotonic.broker.publish(topic, payload, [options])
Publish the message payload on a topic. The possible options are:
- qos
- Quality of service. Can be 0, 1, or 2. 0 means at most once, 1 at least once, and 2 exactly once.
- retain
- When retain is true, the last message sent will be stored, and delivered immediately when a new client subscribes to the topic.
- properties
- Extra properties which can be attached to the message
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 cotonic.broker.subscribe(topics, callback, [options])
Subscribe to topics. Argument topics can either be a single topic, or a list of topics. The function callback will be called when a message which matches one of the topics is published. It is a function is called with two arguments, message and info. Argument message is the received mqtt message, and info the returned information object returned by extract. This makes it possible to easily extract information from the topic. Parameter options can be
- wid
- The worker if used for making the scription. Can be used to differentiate subscriptions from different components on the page. Defaults to: 0
- qos
- The quality-of-service of the subscription. Defaults to: 0
- retain_handling
- [todo]
- retain_as_published
- [todo]
- no_local
- [todo]
- properties
- [todo]
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 cotonic.broker.unsubscribe(topics, [options])
Unsubscribe from the topics. The parameter topics can be a single topic as a string, or a list of topics. The optional parameter options is an object which has the following properties.
- wid
- The worker id from which to unsubscribe from. Defaults to: 0
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 cotonic.broker.call(topic, payload, [options])
Call is a special kind of publish where the publisher expects an answer back. The payload will be published on topic using the options as described in 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:
- timeout
- The timeout in milliseconds to use before rejecting the returned promise. Default: 15000, or 15 seconds.
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);
});