# cotonic.mqtt\_bridge

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

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

Language: en



<a name="cotonic.mqtt_bridge"></a>
This module makes it possible to bridge the broker on the local page to an external MQTT broker.
<a name="cotonic.mqtt_bridge.newBridge"></a>
newBridge `cotonic.mqtt_bridge.newBridge([remote], [options])`
--------------------------------------------------------------

Create a new bridge. This is the hostname of the mqtt broker to connect to. When set to <tt>"origin"</tt> the bridge uses the hostname of the document. When the bridge is connected messages published on the topic matching <tt>bridge/+remote/#</tt> will be re-published on the remote broker. Subscriptions on the topic <tt>bridge/+remote/#</tt> will be published locally. This makes it possible to connect and communicate with all clients connected to remote mqtt brokers. *Note: It is possible to connect to multiple brokers.*

<dl>
                <dt><tt>protocol</tt></dt>
                <dd>The protocol to use to connect to the mqtt broker. Defaults to "ws" when the page is
                loaded via "http", "wss" otherwise.
                </dd>

                <dt><tt>controller_path</tt></dt>
                <dd>The pathname to use when connecting the web socket to the broker. Default: "mqtt-transport"</dd>

                <dt><tt>connect_delay</tt></dt>
                <dd>Default: 20</dd>

                <dt><tt>periodic_delay</tt></dt>
                <dd>Default: 1000</dd>

                <dt><tt>username</tt></dt>
                <dd>Default: undefined</dd>

                <dt><tt>password</tt></dt>
                <dd>Default: undefined</dd>

                <dt><tt>mqtt_session</tt></dt>
                <dd>The mqtt_session module which should be used. Default: cotonic.mqtt_session.</dd>
            </dl>

```
cotonic.mqtt_bridge.newBridge("test.mosquitto.org:8081",
                              {protocol: "wss"});
=> Connect the local broker to test.mosquitto.org via a websocket.

const decoder = new TextDecoder("utf-8");
cotonic.broker.subscribe("bridge/test.mosquitto.org:8081/bbc/subtitles/bbc_news24/raw",
    function(m, t) {
        console.log(decoder.decode(m.payload));
    });
=> Subscribe to a local topic, it will be bridged from the server
   to the page. This gets the raw subtitles of bbc news24.
```

For windows opened by a window with a cotonic broker, a special bridge <tt>"opener"</tt> can be started with `cotonic.mqtt_bridge.newBridge("opener");`. This bridge is a simple one-way bridge that makes it possible for the opened window to publish to topics in the opening window. It is not (yet) possible to subscribe to topics in the opener window.

Example publishing to the opener:

```
cotonic.broker.publish("bridge/opener/model/sessionStorage/post/test", "Hello");
```
<a name="cotonic.mqtt_bridge.findBridge"></a>
findBridge `cotonic.mqtt_bridge.findBridge([remote])`
-----------------------------------------------------

Find bridge <tt>remote</tt>, when remote is not specified, <tt>"origin"</tt> is used. Returns the bridge, or <tt>undefined</tt> when the bridge is not found.

```
const b = cotonic.mqtt_bridge.findBridge("test.mosquitto.org:8081");
=> returns the bridge, or undefined
            
```
<a name="cotonic.mqtt_bridge.deleteBridge"></a>
deleteBridge `cotonic.mqtt_bridge.deleteBridge([remote])`
---------------------------------------------------------

Delete bridge <tt>remote</tt>, when remote is not specified, <tt>"origin"</tt> is used.

```
cotonic.mqtt_bridge.deleteBridge("test.mosquitto.org:8081");
```
<!-- User interface composer -->

License: 

Attribution: 

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