Add an MQTT bridged to WAMP example
This shows the basics of using the MQTT adapter functionality with Crossbar and WAMP
This commit is contained in:
parent
81d9276f4b
commit
0da90c1a51
@ -51,6 +51,17 @@
|
||||
}
|
||||
],
|
||||
"transports": [
|
||||
{
|
||||
"type": "mqtt",
|
||||
"endpoint": {
|
||||
"type": "tcp",
|
||||
"port": 1883
|
||||
},
|
||||
"options": {
|
||||
"realm": "crossbardemo",
|
||||
"role": "anonymous"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "web",
|
||||
"endpoint": {
|
||||
|
@ -31,6 +31,17 @@
|
||||
}
|
||||
],
|
||||
"transports": [
|
||||
{
|
||||
"type": "mqtt",
|
||||
"endpoint": {
|
||||
"type": "tcp",
|
||||
"port": 1883
|
||||
},
|
||||
"options": {
|
||||
"realm": "crossbardemo",
|
||||
"role": "anonymous"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "web",
|
||||
"endpoint": {
|
||||
|
17
examples/twisted/wamp/mqtt/README
Normal file
17
examples/twisted/wamp/mqtt/README
Normal file
@ -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).
|
26
examples/twisted/wamp/mqtt/mqtt-client.py
Normal file
26
examples/twisted/wamp/mqtt/mqtt-client.py
Normal file
@ -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()
|
45
examples/twisted/wamp/mqtt/wamp-client.py
Normal file
45
examples/twisted/wamp/mqtt/wamp-client.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user