PubNub MQTT bridge
Introduction
At PubNub, we've learned a lot from IoT Device Control customers like Samsung, August Home, Wink, and Logitech about the types of features they require. We're excited to announce that we have taken all our expanded PubNub features, like Functions, Presence, topic wildcard support, and simple setup, and have made them available to our customers with MQTT devices.
It's easy to integrate MQTT devices into the PubNub DSN. No code changes are required - simply put your pub/sub credentials into your device identifier, and PubNub takes care of the rest.
Setup
You'll first need to sign up for a PubNub account and get your unique publish and subscribe keys. You can get your keys in the PubNub Admin Dashboard When using PubNub, you simply create channels (aka, topics
in the MQTT world) and publish messages across those channels with those keys. You'll do the same with MQTT.
The only two things you need to do to connect your device to the PubNub Network are:
- Use a broker address of
mqtt.pndsn.com
. Use the standard ports – for unsecured connections use 1883, for TLS secured connections use 8883 – both are supported. - Use a client ID composed like this:
<publish_key>/<subscribe_key>/<actual device ID>
.
Publishing
This operation happens as regular publish API usage without any specific format. Pick a topic and send message to it.
For example, publishing with the Python MQTT client:
import paho.mqtt.client as mqtt
publish_key = "<your publish key>"
subscribe_key = "<your subscribe key>"
client_id = "<your unique client identifier>"
client = mqtt.Client(client_id=publish_key + "/" + subscribe_key + "/" + client_id)
client.connect("mqtt.pndsn.com", 1883, 60)
client.publish("<topic to publish>", json.dumps({ "hi": 10 }))
Subscribing
This operation happens as regular subscribe API usage w/o any specific format. Pick a topic and subscribe to receive message from it.
For example, subscribing with the Python MQTT client:
import paho.mqtt.client as mqtt
publish_key = "<your publish key>"
subscribe_key = "<your subscribe key>"
client_id = "<your unique client identifier>"
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client(client_id=publish_key + "/" + subscribe_key + "/" + client_id)
client.connect("mqtt.pndsn.com", 1883, 60)
client.on_message = on_message
client.subscribe("<topic to subscribe>")
Wildcard Subscribe
PubNub's MQTT support includes topic wildcard support, so developers can leverage the application flexibility within the MQTT protocol.
When you set up wildcard support in MQTT, you use /
, but in PubNub you use .
. So if you want to provide access across the a
channel/ topic, you would type a/#
in the MQTT client, but a.*
in the PubNub Admin Dashboard. The single-level and multi-level wildcard nomenclature remains the same, respectively +
and #
.
Here are examples of wildcard subscriptions:
// Single-level wildcard will be translated to: .*
/+/sensor
// Multi-level wildcard will be translated to: .house.sensor.*
/house/sensor/#
QoS level 0 support
PubNub only supports QoS
level 0 at this time. Good luck with your IoT Device Control / MQTT initiative!