From 0da90c1a51a4e5dc73ab10a838df0b70cede8267 Mon Sep 17 00:00:00 2001 From: meejah Date: Thu, 1 Dec 2016 14:23:32 -0700 Subject: [PATCH] Add an MQTT bridged to WAMP example This shows the basics of using the MQTT adapter functionality with Crossbar and WAMP --- examples/router/.crossbar/config-no-tls.json | 11 +++++ .../router/.crossbar/config-with-tls.json | 11 +++++ examples/twisted/wamp/mqtt/README | 17 +++++++ examples/twisted/wamp/mqtt/mqtt-client.py | 26 +++++++++++ examples/twisted/wamp/mqtt/wamp-client.py | 45 +++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 examples/twisted/wamp/mqtt/README create mode 100644 examples/twisted/wamp/mqtt/mqtt-client.py create mode 100644 examples/twisted/wamp/mqtt/wamp-client.py diff --git a/examples/router/.crossbar/config-no-tls.json b/examples/router/.crossbar/config-no-tls.json index 25e0c740..c8c2d44b 100644 --- a/examples/router/.crossbar/config-no-tls.json +++ b/examples/router/.crossbar/config-no-tls.json @@ -51,6 +51,17 @@ } ], "transports": [ + { + "type": "mqtt", + "endpoint": { + "type": "tcp", + "port": 1883 + }, + "options": { + "realm": "crossbardemo", + "role": "anonymous" + } + }, { "type": "web", "endpoint": { diff --git a/examples/router/.crossbar/config-with-tls.json b/examples/router/.crossbar/config-with-tls.json index aa8ad1cd..348b3dc1 100644 --- a/examples/router/.crossbar/config-with-tls.json +++ b/examples/router/.crossbar/config-with-tls.json @@ -31,6 +31,17 @@ } ], "transports": [ + { + "type": "mqtt", + "endpoint": { + "type": "tcp", + "port": 1883 + }, + "options": { + "realm": "crossbardemo", + "role": "anonymous" + } + }, { "type": "web", "endpoint": { diff --git a/examples/twisted/wamp/mqtt/README b/examples/twisted/wamp/mqtt/README new file mode 100644 index 00000000..cecfaa2a --- /dev/null +++ b/examples/twisted/wamp/mqtt/README @@ -0,0 +1,17 @@ +This demonstrates the use of the MQTT bridge now included in +Crossbar.io + +You need to install "paho-mqtt" to run the MQTT client which you can +do via pip: + + pip install paho-mqtt + +Then, with the crossbar router running from "examples/router" dir you +can start up either the WAMP or MQTT client first (ideally in +different shells): + + python wamp-client.py + python mqtt-client.py + +They both subscribe to "mqtt.test_topic" and then publish some data to +that same topic (so try starting them in different orders etc). diff --git a/examples/twisted/wamp/mqtt/mqtt-client.py b/examples/twisted/wamp/mqtt/mqtt-client.py new file mode 100644 index 00000000..f57b01de --- /dev/null +++ b/examples/twisted/wamp/mqtt/mqtt-client.py @@ -0,0 +1,26 @@ +import paho.mqtt.client as paho + +# note that unlike Autobahn and Crossbar, this MQTT client is threaded +# / synchronous + +client = paho.Client() +client.connect('localhost', port=1883) + + +def on_connect(client, userdata, flags, rc): + print("on_connect({}, {}, {}, {})".format(client, userdata, flags, rc)) + client.subscribe("mqtt.test_topic", qos=0) + client.publish( + "mqtt.test_topic", + "some data via MQTT", + ) + + +def on_message(client, userdata, msg): + print("{}: {}".format(msg.topic, msg.payload)) + + +client.on_connect = on_connect +client.on_message = on_message + +client.loop_forever() diff --git a/examples/twisted/wamp/mqtt/wamp-client.py b/examples/twisted/wamp/mqtt/wamp-client.py new file mode 100644 index 00000000..aaed42e6 --- /dev/null +++ b/examples/twisted/wamp/mqtt/wamp-client.py @@ -0,0 +1,45 @@ +from os import environ + +from autobahn.twisted.wamp import ApplicationSession +from autobahn.twisted.wamp import ApplicationRunner +from autobahn.wamp.types import PublishOptions + +from twisted.internet.defer import inlineCallbacks + + +class Component(ApplicationSession): + topic = u'mqtt.test_topic' + + @inlineCallbacks + def onJoin(self, details): + print("session attached {}".format(details)) + + yield self.subscribe(self.on_event, self.topic) + print("Subscribed to '{}'".format(self.topic)) + + # if you send args, then all args (and kwargs) in the publish + # are encoded into a JSON body as "the" MQTT message. Here we + # also ask WAMP to send our message back to us. + yield self.publish( + u"mqtt.test_topic", "some data via WAMP", + options=PublishOptions(exclude_me=False), + ) + + # if you send *just* mqtt_qos and mqtt_message kwargs, and no + # args then it will take mqtt_message as "the" payload + yield self.publish( + u"mqtt.test_topic", + mqtt_qos=0, + mqtt_message="hello from WAMP", + ) + + def on_event(self, *args, **kw): + print("'{}' event: args={}, kwargs={}".format(self.topic, args, kw)) + + +if __name__ == '__main__': + runner = ApplicationRunner( + environ.get("AUTOBAHN_DEMO_ROUTER", u"ws://127.0.0.1:8080/ws"), + u"crossbardemo", + ) + runner.run(Component)