cotonic.mqtt_bridge Cotonic
mqtt_bridge
This module makes it possible to bridge the broker on the local page to an external MQTT broker.
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 "origin" the bridge uses the hostname of the document. When the bridge is connected messages published on the topic matching bridge/+remote/# will be re-published on the remote broker. Subscriptions on the topic bridge/+remote/# 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.
- protocol
- The protocol to use to connect to the mqtt broker. Defaults to "ws" when the page is loaded via "http", "wss" otherwise.
- controller_path
- The pathname to use when connecting the web socket to the broker. Default: "mqtt-transport"
- connect_delay
- Default: 20
- periodic_delay
- Default: 1000
- username
- Default: undefined
- password
- Default: undefined
- mqtt_session
- The mqtt_session module which should be used. Default: cotonic.mqtt_session.
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 "opener" 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");
findBridge cotonic.mqtt_bridge.findBridge([remote])
Find bridge remote, when remote is not specified, "origin" is used. Returns the bridge, or undefined when the bridge is not found.
const b = cotonic.mqtt_bridge.findBridge("test.mosquitto.org:8081");
=> returns the bridge, or undefined
deleteBridge cotonic.mqtt_bridge.deleteBridge([remote])
Delete bridge remote, when remote is not specified, "origin" is used.
cotonic.mqtt_bridge.deleteBridge("test.mosquitto.org:8081");