In this post, we'll see how easy it is to execute GnatMQ, the MQTT broker for .NET Framework, in the Cloud using the Microsoft Azure platform. The execution of the broker can be started through a Worker Role that is one of the "Cloud Services" offered by Microsoft.

Create the Cloud Service

In the "Server Explorer", click with the right button on "Windows Azure" and "Connect to Windows Azure ..." and execute login using our Azure credentials(the ones we use in the online management portal).


The "Server Explorer" refreshes and displays all the "Windows Azure" services currently active with our account (Cloud Services for web role and worker role, Service Bus with its namespaces, Virtual Machines and so on).


Since our goal is to execute the GnatMQ broker in a worker role, we need to create a new "Cloud Services", right-clicking on it and selecting "Create Cloud Service ...". Select the subscription you want to associate the new service, the name and the region where it will be executed.


The creation of a new "Cloud Services" can also be performed using the online management portal in the "Compute", "Cloud Service" and "Quick Create"; the information required are the same as Visual Studio unless the subscription is implied that having done the online login.


Create the Worker Role

Using Visual Studio, create a new project of "Windows Azure Cloud Service" type included in the "Cloud" template, giving it a name (eg AzureGnatMQ).


A "Cloud Service" can host one or more services like Web Role, for ASP.NET applications and WCF services, and Worker Role typically for backend operations sometimes also closely related to the Service Bus. In our case, just add a Worker Role by assigning the GnatMQWorkerRole name and create the corresponding project.


The first thing to do is to download the source code from CodePlex to add the project MqttBroker within the newly generated solution and later as a reference to the project GnatMQWorkerRole; nothing prevents you to build the broker separately and then add a reference to the assembly.

The AzureGnatMQ solution consists of the following projects :

  • AzureGnatMQ with all settings related to “Cloud Service” needed to deploy on Azure;
  • GnatMQWorkerRole with the Worker Role will host the MQTT broker;
  • MqttBroker that implements GnatMQ;


GnatMQ executing in the Worker Role

Open the source file for the Worker Role and create a private field of type MqttBroker; instantiate the MqttBroker class in OnStart() method and execute the Start() method on this instance. With these two simple steps we have created and started GnatMQ in the Worker Role !


   1: public override bool OnStart()
   2: {
   3:     // Set the maximum number of concurrent connections 
   4:     ServicePointManager.DefaultConnectionLimit = 12;
   6:     // For information on handling configuration changes
   7:     // see the MSDN topic at
   9: = new MqttBroker();
  12:     return base.OnStart();
  13: }

As we know, the MQTT broker uses 1883 port  to accept incoming TCP connections (not SSL); for this reason, we need to make it accessible from the outside by adding an EndPoint to the Worker Role.




To do this, open the Worker Role properties (not the project but the instance that is located in the "Roles" subfolder of "AzureGnatMQ") and then open the "Endpoints" tab. We have to add a new EndPoint with the following characteristics:

  • “MQTT” as name (or whatever you want);
  • “Input” type;
  • TCP as protocol;
  • 1883 as public port;


The broker is now ready to be compiled and distributed to the Cloud. To test the operation locally, we can execute it with the Windows Azure emulator.

Publish on Azure

The publication on Azure is quite simple thanks to integration with Visual Studio (even if we could run it from the online management portal). We click with the right button on the "AzureGnatMQ" project (relative to the cloud service) and then click "Publish". We must choose the subscription to use, the cloud service in which to run the worker role, the environment (production or staging) and the configuration to use.


The publication is done in a matter of few minutes and we can follow the progress in the "Windows Azure Activity Log” window.


Once published it, the MQTT broker will be available online and accessible at <name> or using the VIP (Virtual IP Address) assigned by Azure that we can find available in the Cloud Service dashboard.