# cotonic.mqtt

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

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

Language: en



<a name="cotonic.mqtt"></a>
The mqtt module provides functions to work with mqtt topics. The [broker](/id/doc_cotonic_cotonic_broker#cotonic.broker) uses this module as the basis to provide its functionality. This module also has some utility functions to easily extract information from topics.
<!-- More info needed, references to MQTT maybe? --> <!-- matches --> <a name="cotonic.mqtt.matches"></a>
matches `cotonic.mqtt.matches(pattern, topic)`
----------------------------------------------

Returns <tt>true</tt> when the pattern matches the topic, <tt>false</tt> otherwise.

```
cotonic.mqtt.matches("truck/+/speed", "truck/01/speed");
=> true
cotonic.mqtt.matches("boat/+/speed", "truck/01/speed");
=> false
cotonic.mqtt.matches("+/+/speed", "plane/01/speed");
=> true
cotonic.mqtt.matches("+/+/speed", "plane/01/height");
=> false
```
<!-- fill --> <a name="cotonic.mqtt.fill"></a>
fill `cotonic.mqtt.fill(pattern, params)`
-----------------------------------------

Fill can use a <tt>pattern</tt> topic, and use the <tt>param</tt> object to create an mqtt topic. Returns a string with the created topic.

```
cotonic.mqtt.fill("truck/+truck_id/speed", {truck_id: 100});
=> "truck/100/speed"
```
<!-- extract --> <a name="cotonic.mqtt.extract"></a>
extract `cotonic.mqtt.extract(pattern, topic)`
----------------------------------------------

Extract values from <tt>topic</tt> into an object. The pattern <tt>+&lt;key&gt;</tt> matches a single level of the topic path. It places an attribute <tt>key</tt> with as value the found element in the path in the returned object. The pattern <tt>#&lt;key&gt;</tt> matches a multi level path. When it is matched it places that part of the path into a list. Returns an object with the found elements.

```
cotonic.mqtt.extract("truck/+truck_id/speed", "truck/01/speed");
=> {truck_id: "01"}
cotonic.mqtt.extract("truck/+truck_id/#params", "truck/01/speed");
=> {truck_id: "01", params: ["speed"]}
```
<!-- exec --> <a name="cotonic.mqtt.exec"></a>
exec `cotonic.mqtt.exec(pattern, topic)`
----------------------------------------

When the <tt>pattern</tt> [matches](/id/doc_cotonic_cotonic_mqtt#cotonic.mqtt.matches) the <tt>topic</tt>, [extract](/id/doc_cotonic_cotonic_mqtt#cotonic.mqtt.extract) the values from the topic. Returns the extracted values when the pattern matches, <tt>null</tt> otherwise.

```
cotonic.mqtt.exec("truck/+truck_id/speed", "truck/01/speed");
=> {truck_id: "01"}
cotonic.mqtt.exec("boat/+truck_id/speed", "truck/01/speed");
=> null
```
<!-- remove_named_wildcards --> <a name="cotonic.mqtt.remove_named_wildcards"></a>
remove\_named\_wildcards `cotonic.mqtt.remove_named_wildcards(pattern)`
-----------------------------------------------------------------------

Remove the special, and non mqtt compliant, wildcards from the pattern and return a compliant topic.

```
cotonic.mqtt.remove_named_wildcards("truck/+truck_id/speed");
=> "truck/+/speed"
cotonic.mqtt.remove_named_wildcards("truck/#truck_info");
=> "truck/#"
```
<!-- MQTT Bridge -->

License: 

Attribution: 

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