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).

01

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).

02

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.

03

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.

04

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).

05

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.

06

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;

07

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;
   5:  
   6:     // For information on handling configuration changes
   7:     // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
   8:  
   9:     this.broker = new MqttBroker();
  10:     this.broker.Start();
  11:  
  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;

08

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.

09

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

10

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

11