move mqtt example to crossbar-examples
This commit is contained in:
parent
88198dff87
commit
2f2dedff49
|
@ -1,47 +0,0 @@
|
|||
# MQTT Bridge
|
||||
|
||||
Crossbar.io includes a MQTT bridge that not only makes it a full scale, great MQTT broker on its own, but also allows WAMP and MQTT publishers and subscribers talk to each other transparently.
|
||||
|
||||
This opens up whole new possibilities, eg immediately integrate MQTT client devices into a larger WAMP based application or system
|
||||
|
||||
## Demo
|
||||
|
||||
The demo make use of the MQTT bridge now included with Crossbar.io 17.2.1 and later.
|
||||
|
||||
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.io router running from [examples/router](https://github.com/crossbario/autobahn-python/tree/master/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).
|
||||
|
||||
## Configuration
|
||||
|
||||
To configure MQTT in Crossbar.io, add a MQTT transport item to a router worker like here:
|
||||
|
||||
|
||||
```
|
||||
"transports": [
|
||||
{
|
||||
"type": "mqtt",
|
||||
"endpoint": {
|
||||
"type": "tcp",
|
||||
"port": 1883
|
||||
},
|
||||
"options": {
|
||||
"realm": "crossbardemo",
|
||||
"role": "anonymous"
|
||||
}
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
Besides the listening endpoint configuration, you can configure the mapping to a WAMP realm.
|
|
@ -1,26 +0,0 @@
|
|||
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()
|
|
@ -1,45 +0,0 @@
|
|||
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)
|
Loading…
Reference in New Issue