MQTT: Scaling up the next level using HiveMQ/ CloudMQTT

My previous post talked about setting a basic MQTT client setup, with one node publishing, and one node subscribing. This post will talk about taking the next step – moving on to a bigger and secure enterprise broker to handle communications.

There are several brokers out there that can be configured and set up to run your own MQTT network.

One option is HiveMQ. It’s an enterprise broker that you can run in your own machine. You can download the setup at their website Do check out the documentation, which has installation instructions too. Once you’ve installed it, run it on a machine, and let your clients connect to the machine’s IP address and port 1883 (by default). The code remains exactly the same as before.

Another option is CloudMQTT ( This is a hosted service, which means the broker is on the cloud. When you create an account and add an instance, you’ll get your own special broker in the cloud. A sample code is for a client connecting to their broker is here –

Both these services are free to use for a few connections, and paid thereafter. I can’t say which is better, but both have their advantages and disadvantages. HiveMQ has been built from the ground up, and has a lot of plug-ins and features that make it easy to monitor, secure and fast. CloudMQTT is on the cloud, and it’s quick to start off since you don’t need to worry about managing your own broker.

Note that both these options let you have your own private broker. Previously, the broker we used for a public broker where anyone could publish and subscribe.

More coming up…


Simple MQTT client in Python

MQTT is a lightweight protocol that’s ideal for the Internet of Things.

MQTT is based on the publish-subscribe model. This is quite different from the typical network protocol models where a connection is established, after which the client requests the server for information directly, and the server sends back information to the client’s IP address directly. In case of MQTT, nodes connect to a broker and subscribe to a topic. Then they wait. If a node publishes to the same topic in the node, all the other nodes who had subscribed to it receive it.

In the diagram below, Node 5 publishes “Hi!” to the topic “hello/world/”, and Node 1 and Node 3 receive the message since they are subscribing to the topic.


There are various public brokers out there which anyone can use for general testing. A complete list can be found here – I’m using the broker- All my clients will connect to this broker at the address and port provided. Think of it like public bulletin boards, free for anyone to post and read. Later on, you might want your own private broker for your application. I’ll cover how to do that in my next post.

This is a tutorial to get MQTT clients up and running quickly on Python 2.7 (on a Linux machine). I’m using the oddly named Open Source MQTT broker, Mosquitto. It’s now been donated to the Eclipse Paho project. A lot of old code I saw online on some blogs has to be modified slightly to make it work now.

Part 1: Installation
Hopefully, you’ll have pip installed. It’s a great tool to install python modules, and it’ll save you a lot of hassle.
sudo apt-get install python-pip
Next, install the MQTT broker Mosquitto (or Paho now)-
pip install paho-mqtt

Part 2: Subscribe
Here’s a basic python code to subscribe to a topic hosted by a broker on the local machine itself.

# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
print(“Connected with result code “+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print “Topic: “, msg.topic+’\nMessage: ‘+str(msg.payload)

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect(“”, 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.

This has been borrowed from

Part 3: Publish
Well now there’s a node which has subscribed to the topic /hello/world and is waiting for someone to publish data. This basic script below will do the job.

# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt

mqttc = mqtt.Client(“python_pub”)
mqttc.connect(“”, 1883)
mqttc.publish(“hello/world”, “Hello, World!”)
mqttc.loop(2) //timeout = 2s

Part 4: Execute
That’s it. You’re done. Run the subscribe node first and then the publish node. You’ll notice the subscribed node receiving any message sent by the publishing node via the broker.

The next post covers how to expand our MQTT architecture by setting up our own broker.