IN THIS ARTICLE

    Subscribe to Our Newsletter

    Stay updated with the latest on web, mobile, and IoT, delivered weekly.
    Thanks for subscribing!

    Thanks for subscribing!

    Get ready for some great content.

    Alarm

    Since we published this post, we’ve significantly improved our MQTT support!

    Learn more here →

    Embedded Device with PubNubAt PubNub, we’re looking to solve the 5 challenges of Internet of Things connectivity. One of our focuses is on embedded devices, and we want to make connecting those devices as easy and reliable as possible, all while reducing battery, CPU, and bandwidth consumption.

    To further that goal, we’re excited to announce we now support the MQTT protocol over the PubNub Data Stream Network. You can communicate using MQTT to our gateway while still leveraging the full power of PubNub for embedded devices.

    MQTT Background, History, and Design Philosophy

    MQTT Protocol

    If you aren’t familiar with MQTT, the Message Queuing Telemetry Transport (MQTT) is a publish/subscribe protocol aimed specifically at low-powered embedded devices.

    Originally developed at IBM, MQTT has recently begun to pick up a lot of steam. You can use it however you want, however we envision its use as a powerful solution to connect smart devices and embedded devices easily and with low overhead. There are plenty of documentation and open source client implementations if you wish to dive further into the protocol itself.

    Why Use MQTT or PubNub?

    You may be asking, “When do I use MQTT vs. PubNub? And when does it make sense to use both?

    MQTT targets low-powered embedded device environments where resources are scarce. PubNub solves the problems of large-scale connectivity for real-time apps and IoT devices. If you’ve already deployed MQTT-based infrastructure, it makes sense to take advantage of this new PubNub gateway to facilitate scaling and maintenance of your embedded device connectivity layer.

    If MQTT is not part of your current infrastructure, you might consider leveraging the PubNub Data Stream Network directly along with PubNub’s numerous IoT SDKs for all of your Internet of Things connectivity.

    Connect to PubNub’s Data Stream Network Using MQTT

    So lets dive right in. How do you use MQTT with PubNub?

    With PubNub, we give you unique publish and subscribe keys. You’ll need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Portal. You will use those same keys when you want to leverage our MQTT gateway. When using PubNub, you simply create channels and publish messages across those channels with those keys. You’ll do the same with MQTT.

    MQTT PubNub Embedded Devices

    When implementing your client, the gateway into our network is mqtt.pubnub.com listening on the default MQTT port 1883. If you are familiar with MQTT, this is the broker that will handle all your messages. To connect as a client, MQTT requires that you have a client name. This is the first step where things will matter when connecting to PubNub.

    For any client you connect, the first part of the name must include the publish and subscribe keys plus a unique identifier. All three components of the name must be separated by a forward slash.

    mqtt_connect = "<publish_key>/<subscribe_key>/<client_uuid>"

    For instance, to connect to the demo channel, the following would work:

    publish_key   = "pub-c-7f5b7bb7-2111-43d0-8442-ade8ca712f07"
    subscribe_key = "sub-c-c673b326-d4a0-11e3-b488-02ee2ddab7fe"
    client_uuid   = "ddb86ef5-fb96-4def-95e4-90b716c5480d"
    mqtt_topic    = publish_key + "/" + subscribe_key + "/" + channel_name
    mqtt_connect  = publish_key + "/" + subscribe_key + "/" + client_uuid
    

    The publish and subscribe keys must be valid and there must be a unique identifier for the third part of the client name.

    Create a MQTT “Hello World!” Publish and Subscribe Client

    There are many MQTT clients available, but many people use the Paho project that includes many different languages for clients. To use the Python client you can pip install mosquitto and to get the Pubnub Python client quickly pip install pubnub==3.5.1. The documentation for using the Mosquitto client is located here. The PubNub client documentation for Python is all open source as well.

    The following code quickly shows how to subscribe:

    Install PIP Dependencies

    sudo pip install mosquitto
    sudo pip install pubnub==3.5.1
    

    Subscribe Using MQTT

    from mosquitto import Mosquitto
    publish_key   = "pub-c-7f5b7bb7-2111-43d0-8442-ade8ca712f07"
    subscribe_key = "sub-c-c673b326-d4a0-11e3-b488-02ee2ddab7fe"
    channel_name  = "hello_world"
    client_uuid   = "2fb96def5"
    mqtt_hostname = "mqtt.pubnub.com"
    mqtt_connect  = publish_key + "/" + subscribe_key + "/" + client_uuid
    mqtt_topic    = publish_key + "/" + subscribe_key + "/" + channel_name
    mosq_object   = Mosquitto(mqtt_connect)
    def on_message( mosq, obj, msg ):
        print( msg.payload, msg.topic )
    mosq_object.on_message = on_message
    mosq_object.connect(mqtt_hostname)
    mosq_object.subscribe(mqtt_topic)
    mosq_object.loop_forever()
    

    This will create a Mosquitto object that sets a callback function for any message it gets while listening. We then connect to the PubNub broker gateway and start to listen on the channel “demo/demo/hello_world” forever. Now let’s publish from a different client.

    Publish Using MQTT

    from mosquitto import Mosquitto
    publish_key   = "pub-c-7f5b7bb7-2111-43d0-8442-ade8ca712f07"
    subscribe_key = "sub-c-c673b326-d4a0-11e3-b488-02ee2ddab7fe"
    channel_name  = "hello_world"
    client_uuid   = "1ddb86ef5"
    mqtt_hostname = "mqtt.pubnub.com"
    mqtt_connect  = publish_key + "/" + subscribe_key + "/" + client_uuid
    mqtt_topic    = publish_key + "/" + subscribe_key + "/" + channel_name
    mosq_object   = Mosquitto(mqtt_connect)
    mosq_object.connect(mqtt_hostname)
    mosq_object.publish( mqtt_topic,   "Hello World!" )
    

    This will publish “Hello, World!” to the broker. You can also take any of our over 50 PubNub SDKs and subscribe to the channel to get the same message.

    Developer Console with MQTT and PubNub

    You can connect to the PubNub Developer Console and match the topic channel name of the MQTT client by also redundantly including the Publish Key and Subscribe Key in the Channel Name matching the mqtt_topic variable.

    mqtt_topic = publish_key + "/" + subscribe_key + "/" + channel_name

    PubNub Developer Console Live URL: www.pubnub.com/docs/console?channel=pub-c-7f5b7bb7-2111-43d0-8442-ade8ca712f07/sub-c-c673b326-d4a0-11e3-b488-02ee2ddab7fe/hello_world&origin=pubsub.pubnub.com&sub=sub-c-c673b326-d4a0-11e3-b488-02ee2ddab7fe&pub=pub-c-7f5b7bb7-2111-43d0-8442-ade8ca712f07

    PubNub Developer Console

    Subscribe using PubNub SDK

    from Pubnub import Pubnub
    publish_key   = "pub-c-7f5b7bb7-2111-43d0-8442-ade8ca712f07"
    subscribe_key = "sub-c-c673b326-d4a0-11e3-b488-02ee2ddab7fe"
    channel_name  = "hello_world"
    mqtt_topic    = publish_key + "/" + subscribe_key + "/" + channel_name
    client_uuid   = "ddb86ef5-fb96-4def-95e4-90b716c5480d"
    pubnub        = Pubnub( publish_key, subscribe_key )
    def callback( message, channel ): print( message, channel )
    pubnub.subscribe( mqtt_topic, callback=callback )
    

     

    The Future and Caveats of MQTT with PubNub

    Currently our support of the MQTT protocol only supports QoS level 0 and does not include topic wildcards. In the very near future, we’ll be adding support for these. We’re very excited about MQTT and what it provides our customers!


    Oh, by the way, bring your most outrageous Internet of Things ideas to PubNub’s HQ for an exciting Hack Day featuring free leading-edge technologies, cool prizes. For more info, check out:  http://pubnub-electricimp-iot-hackday.eventbrite.com/?aff=pn


    Subscribing to Channels Leveraging MQTT with PubNub

    When creating a PubNub channel to use for MQTT, the channel name also has the requirement that your publish and subscribe keys prepend the channel name.

    <publish_key>/<subscribe_key>/<channel_name>
    

    For instance the following could be a channel name for the demo channel:

    publish_key   = "pub-c-7f5b7bb7-2111-43d0-8442-ade8ca712f07"
    subscribe_key = "sub-c-c673b326-d4a0-11e3-b488-02ee2ddab7fe"
    channel_name  = "hello_world"
    

    Publish over MQTT to PubNub’s Global Data Stream Network

    Let’s publish! Connect a client to our gateway at mqtt.pubnub.com on port 1883. Make sure the client has the publish and subscribe keys in the name per the above rules. Subscribe to a channel using the same rules and publish away. Publish to MQTT on a channel that you are subscribed to and you should now get messages in realtime.

    Message Rules for Publishing from PubNub to an MQTT Client

    You may be thinking that you can only talk MQTT to MQTT, PubNub to MQTT or MQTT to PubNub. Yes. All are supported.

    • If you publish a message talking MQTT, it will publish on both MQTT and PubNub on the given channel.
    • If you publish through PubNub, it will also be published to MQTT if there is a MQTT client subscribing to that channel.

    If you do publish a message on PubNub and want to receive it as an MQTT client, you have to include two things in the message. 1) the JSON of the message must contain the channel it’s being published on, and 2) the payload of the message must be in a MQTT identifier. The following example shows how:

    message = { 
        "mqtt"    : <some_payload>, 
        "channel" : "<publish_key>/<subscribe_key>/<channel_name>"
    }

    If these are not in the message, your MQTT client will not receive the message.

    Resources
    Resources

    Building a HIPAA-compliant App

    Everything You Need to Know About Developing and Scaling a HIPAA-compliant App
    Download Now
    Building a HIPAA-compliant App