Connect IoT Devices by using RabbitMQ and MQTT.

There is a growing change in the software world these days, due to increase of IoT devices. We live in a world where real time information is important, and one of the challenges that software projects dealing with is how to connect IoT devices together and send messages between them (publish subscribe notifications).

In this post I want to share the concept of using RabbitMQ and MQTT protocol as publish subscribe message broker, in order to connect IoT devices and software applications together.

Understanding messages in term of RabbiMQ:

Messaging in RabbitMQ hides the sender and receiver from each other, as a developers, we have a flexible infrastructure that encourages decoupling of our applications. In addition the messages have no set structure and can even store binary data directly.

What is MQTT?

MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design principles are to minimize network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium.

RabbitMQ – is a message broker- with MQTT plugin.

MQTT is just a protocol specification; therefore we need a supported service that implements this protocol. RabbitMQ is the delivery service for the MQTT protocol. So every application can receive messages and send messages. RabbitMQ is just the MQTT router between each IoT device, the server and his clients.

MQTT Terminology:

Client: Any publisher or subscriber that connects to the RabbitMQ over a network is considered to be the client. Both publishers and subscribers are called as clients since they connect to the centralized RabbitMQ service.

Following example demonstrate how to create connection with RabbitMQ and MQTT with .NET C#:

private void ConnectToRabbitMQ(string address)
	RabbitMqClient = new MqttClient(IPAddress.Parse(address));

this example use the .NET implementation of MQTT Client: M2Mqtt

Topic: A topic is the message label that the clients subscribes to connect each other. Client can subscribe/publish message by topic. The topic enables RabbitMQ detect who is the endpoit adress for the messages.

Payload: payload is the data content of the message. The payload can be anything from JSON to MPEG-4 or even a binary data.  

Networking and RabbitMQ:

Clients communicate with RabbitMQ over TCP/IP protocol. The standard port of RabbiMQ is 5672. For secure communication it is possible to encrypt connections using TLS with RabbitMQ. Authentication using peer certificates is also possible.

Latency and RabbitMQ:

RabbitMQ instance may be able to handle the message throughput generated by IoT application, but what happened when 1,000,000 messages published per second? If the clients will be fast enough to process the messages,  Rabbit message que will eventually be empty. In other hand let say it takes 50ms for Rabbit to take a message from this queue, put it on the network and for it to arrive at the consumer. And it takes 4ms for the client to process the message. Once the consumer has processed the message, it sends an ack back to Rabbit, which takes a further 50ms to be sent to and processed by Rabbit. So we have a total round trip time of 104ms. 104ms latency for each 1,000,000 messages can cause the RabbitMQ to over the memory threshold and disable all publishing messages. disable publish messages can cause RabbitMQ clientns lot issues. Therefore luckily, RabbitMQ comes with built-in clustering solution that can satisfy both problems and make sure IoT app always has a Rabbit to talk to.

Here is RabbiMQ clustering guide


RabbitMQ and MQTT plugin enabled, is the most preferred protocol for IoT applications. Furthermore RabbitMQ can be preferred choice for push notifications platform from .NET server to his clients. By sending  JSON message content, each client can publish and receive any kind of message structure, in a fancy decouple way.


About Idan Reuven

Idan is Microsoft Certified Proffesional Developer (MCPD) Idan Working As Senior Software developer. and specialize in professional application development with WPF, WCF, Entity Framework, XAML, HTML 5, Java Script , jQuery, MVC, MVVM, C#, C++, SQL Server Technologies.
This entry was posted in .NET, .NET 4.5, C# 6, MQTT, RabbitMQ. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s