Creating an MQTT Broker With CloudMQTT

Cloud based brokers are likely to become very popular in the future for organisations they operate over a wide geographic area.

In addition they provide a nice user interface making it very easy to setup your own broker instance and you don’t need to have to manage your own virtual server.

CloudMQTT like Amazon,Azure etc provide a managed cloud based mosquitto broker.

The plans on CloudMQTT are shared plans which means that several MQTT brokers run on the same hardware.



The brokers are distinguished by the port numbers as shown in the diagram below..

Shared-Hosting-Mosquitto-Brokers
To create a new broker instance click on the create instance button.


—–
enter the name, select the plan and also the data center then click create new instance.

create-instance-form
The instance will be created and you can see the details by clicking on instance name.

manage-instance

Here are the details for one of my brokers. You will need the server name port numbers etc for configuring your clients.

You can also manage users ,view logs and configure bridges from this panel as shown below.
manage-instance-2

Connecting to your Broker Using Python

Below I’ve shown the relevant bits of code from my script. The important thing to note is the call to the username_pw_set function. (Note: the password and username has been change for security)

broker="m21.cloudmqtt.com"
port=17363
username="toyeerbnp"
username="steve1e"
password="JUlkU47AEy86o"

client = mqtt.Client("Python1",clean_session=CLEAN_SESSION)
client.username_pw_set(username, password)
client.connect(broker,port)

Connecting to your Broker Using Websockets

If you look at your instance details you also have a websockets port which must be encrypted.

You on_connect function will look something like the one below:

	  function MQTTconnect() {
		console.log("connecting to "+ host +" "+ port);
		mqtt = new Paho.MQTT.Client(host,port,"clientjs");
		//document.write("connecting to "+ host);
		var options = {
			useSSL:true,
			timeout: 3,
			userName:"toyerssbnp",
			password:"JUlkU47AEy8aao",
			onSuccess: onConnect
			
		  
		 };

The script websockets-ssl.htm is part of the websockets download video section and will work if you change the:

broker name,username and password

Connecting to your broker using SSL

Cloudmqtt use a certificate signed by comodo and you need to download the root certificate from their website.

It is called addtrustexternalcaroot.crt

comodo-root-ca-download

You can test it using the mosquitto_pub tool as shown in the screenshot below:

mos-pub-testcloudmqtt

Videos

Here is a video I created that takes you through the above.

Here is a video showing you how to publish and subscribe to the Cloudmqtt broker.

Common Questions and Answers

Q- Can I configure topic restrictions?

A- Yes -Under Users and ACL

Q- Do All client connections require a username/password?

A- Yes

Note: If you want to run multiple brokers on your own hardware then take a look at this video –How to run multiple mosquitto brokers on the same host.

Related Tutorials:

Save

Save

Save

Save

Save

Facebooktwittergoogle_plusredditpinterestlinkedinmail

8 comments

  1. nice post, thanks.
    that python example is broken tho
    you are trying to access the client class before creating it.

  2. If you use the Cloud Broker, is necessary install Mosquitto in raspberry pi, or in computer for e.g??
    Or my Mosquitto hosted in the cloud is enough?
    I mean, I wanna know if a ESP8266, w/ pub/sub library in the IDE and a local Wi-Fi is all that I need to communicate with MQTT my server

    1. If you use a cloud broker then you don’t need to install mosquitto locally. You will need to give your devices access to the Internet.
      However the main advantage of having a local mosquitto broker is that you can see status messages on the broker which helps when troubleshooting.

    1. The client id can be whatever you want. Do you mean the broker name? If so you will get tat when you create the broker.

Leave a Reply

Your email address will not be published. Required fields are marked *