in Protocols, Tutorials

AMQP sender and receiver with Visual Studio

AMQP is a great communication protocol and is used a lot in Internet of Things (IoT) scenario’s. The protocol works with queue’s and gives message-delivery guarantees. There is a wide support for AMQP and that support is growing.

This post describes how to build an AMQP sender and receiver with Visual Studio, where RabbitMQ is the message broker.
A few things need to be installed to make it work:

  • Erlang (17.3)
  • RabbitMQ (3.4.2)
  • Visual studio 2013 Community

These subjects will be described step by step.

Erlang

The installation of Erlang is needed to run RabbitMQ. Download and install the windows binary file. Choose the one that match your system specifications.

ErlangDownload

Erlang download page

RabbitMQ

RabbitMQ is a message broker for AMQP. Download and install the latest Rabbit Server.

RabbitMQDownload

RabbitMQ download page

When the installation is done the RabbitMQ server runs as a service.

RabbitMQService

RabbitMQ Service

Manage RabbitMQ service

RabbitMQ default installs applications to manage the service. These applications can be found via the search option in windows 8. The search for rabbit shows these programs.

RabbitMQServiceApplications

RabbitMQ service applications

It’s also possible to manage the service in the command prompt with the tool rabbitmq-service.bat. The tool can be found in %yourinstallationfolder%\RabbitMQ Server\rabbitmq_server-3.4.1\sbin

RabbitMQServiceCommandLineTool2

RabbitMQ service command line tool

RabbitMQ Web UI

Now we have a service running and it would be nice if we can get some sort of dashboard to monitor the service. RabbitMQ has a great web UI for that. It can be installed in the command prompt with the application rabbitmq-plugins. Just execute this command rabbitmq-plugins enable rabbitmq_management.

RabbitMQInstallWebUI2

RabbitMQ install web UI

After the installation you can open the UI in the browser with the URL http://server-name:15672/. In this case the server-name is localhost.
The first page you see when you navigate to the site is the login page.

RabbitMQWebUILogin

RabbitMQ web UI login

Enter username ‘guest’ and password ‘guest’ and hit ‘login.’
After the login you see the RabbitMQ dashboard.

RabbitMQwebUIDashboard

RabbitMQ web UI dashboard

More information about the web ui can be found on this page http://www.rabbitmq.com/management.html.

Visual studio 2013

You can download a free version of Visual Studio right here.

Create the sender and receiver

On the website of RabbitMQ there is a ‘Hello World’ tutorial which we will use.

RabbitMQ library

Download(http://www.rabbitmq.com/dotnet.html) the .NET library. There is a .ZIP file described as ‘Binary, compiled for .NET 3.5 and newer (zip) – includes example code, the WCF binding and WCF examples’, that’s the one we need.
Extract the .ZIP file and find RabbitMQ.Client.dll in the %yourdownloadsfolder%\rabbitmq-dotnet-client-3.4.1-dotnet-3.5\bin\ folder.

Sender

In Visual Studio create a new Visual C# -> Windows Desktop -> Console Application project. Call the project Sender.

Visual Studio new project

Visual Studio console application

Copy and paste the RabbitMQ.Client.dll file to your project folder and add it as a reference to the sender project.

ReferenceRabbitMQClient

Add reference RabbitMQ Client

Open the Program.cs and paste this snipped

class Send
{
	public static void Main()
	{
		var factory = new ConnectionFactory() { HostName = "localhost" };
		using (var connection = factory.CreateConnection())
		{
			using (var channel = connection.CreateModel())
			{
				channel.QueueDeclare("hello", false, false, false, null);
				string message = "Hello World!";
				var body = Encoding.UTF8.GetBytes(message);
				channel.BasicPublish("", "hello", null, body);
				Console.WriteLine(" [x] Sent {0}", message);
				Console.Read();
			}
		}
	}
}

Instead of

class Program
{
	static void Main(string[] args)
	{
	}
}

Then remove the existing using statements and copy these one

using System;
using RabbitMQ.Client;
using System.Text;
Receiver

In Visual Studio, in the same solution as the sender project, create a new Visual C# -> Windows Desktop -> Console Application project. Call the project Receiver.
Copy and paste the RabbitMQ.Client.dll file to your project folder and add it as a reference to the receiver project.
Open the Program.cs and paste this snipped

class Receive
{
	public static void Main()
	{
		var factory = new ConnectionFactory() { HostName = "localhost" };
		using (var connection = factory.CreateConnection())
		{
			using (var channel = connection.CreateModel())
			{
				channel.QueueDeclare("hello", false, false, false, null);
				var consumer = new QueueingBasicConsumer(channel);
				channel.BasicConsume("hello", true, consumer);
				Console.WriteLine(" [*] Waiting for messages." +
				"To exit press CTRL+C");
				while (true)
				{
					var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
					var body = ea.Body;
					var message = Encoding.UTF8.GetString(body);
					Console.WriteLine(" [x] Received {0}", message);
				}
			}
		}
	}
}

Instead of

class Program
{
	static void Main(string[] args)
	{
	}
}

Then remove the existing using statements and copy these one

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;

Run the scenario

Now we can run the projects. First of all open the Rabbit web UI, so we can monitor the messages.
The sender project will create a queue, if not exists, named ‘hello’. Then it sends one message called ‘Hello World!’. Run the project to make it work.
The console should look like this

Sender Console

Sender console

The web ui, with the tab Queue selected, should look like this

RabbitMQwebUIDashboardQueue

RabbitMQ web UI dashboard queue

You can see the new queue called ‘hello’, with one message.
The next step is to run the receiver project. That should take the message from the ‘hello’ queue and print it in the console. Run the project to make it work.
The console should look like this

ReceiverConsole

Receiver console

And the queue is empty

RabbitMQwebUIDashboardQueueEnd

RabbitMQ web UI dashboard queue

Why RabbitMQ?

In this scenario RabbitMQ is used as message broker. RabbitMQ gives a great overview of the messages in the broker. Another reason is that RabbitMQ can be installed locally, so the complete scenario can be done locally and for free.

Conclusion

AMQP is a great and growing communication protocol for IoT.
In the scenario of this post you created a message sender, used a message broker and created a message receiver. You can imagine this post in real projects as the following:

  • The sender can be the IoT device
  • RabbitMQ can be Microsoft Azure Service Bus
  • The receiver can be an Web/Phone application

Write a Comment

Comment