RabbitMQ, AMQP, MQTT & Rest of the world – Hacker Noon

Hey folks, welcome back. Hope you all are doing well. Then why wasting time ! Lets move to the tutorial.

Today I am going to write on Message Queue. Very simple nah ? Huh lets dive.

These days we are developing smart applications to make lives easier. There are lots of tech giants with lots of tech engineers fighting to solve problems. There are Google, Facebook, Pathao, Uber, Grab, AirBnB and so on….

A common task we developers do is send notifications or requests to process tasks between applications. One of the solution to do it could be using Message Queue.

RabbitMQ : RabbitMQ is a message queueing hybrid broker. Hybrid is that sense it has support for different protocols like AMQP, MQTT, WebSocket etc.

AMQP ( Advanced Message Queueing Protocol ) : is an open standard application layer protocol for message-oriented middleware. The defining features of AMQP are message orientation, queuing, routing, reliability and security. [ Ref : Wiki ]

MQTT ( Message Queue Telemetry Transport ) : is an ISO standard (ISO/IEC PRF 20922)[2] publish-subscribe-based messaging protocol. It works on top of the TCP/IP protocol. It is designed for connections with remote locations where a “small code footprint” is required or the network bandwidth is limited. The publish-subscribe messaging pattern requires a message broker. Facebook uses MQTT protocol for their messaging engine ( Messenger ). [ Ref : Wiki ]

How Message Queue Works

Lets consider the above scenario. B is subscribed to Topic 1, A is publishing something to Topic 1. And broker is routing message from A to B using topic. Simply this is what broker do and the protocol works.

Now you may ask WTF are so many protocols if all of them are same ?

Ok, all of them are not same. There is small difference with big impact.

MQTT is designed to be used on lightweight devices like Mobile devices, Embedded systems where bandwidth is costly and minimum overhead required. It uses a 2 byte fixed header to control everything and exchange data as byte stream. MQTT is being used widely in IOT.

AMQP is designed with more advanced features and has more overhead than MQTT.

For this reason AMQP is not preferred for lightweight devices like mobile, where MQTT can be used in anywhere.

But in real world application development we may need AMQP like reliable message queue and also has lightweight devices to work with. Here is the point where RabbitMQ comes in. RabbitMQ has the flexibility to use both protocol AMQP & MQTT together.

So the idea is when you are on mobile device use MQTT protocol to connect to RabbitMQ broker. As MQTT protocol is very lightweight and works smoothly with low bandwidth. And for server side communication you can use AMQP or MQTT which one you prefer. Both protocol uses stateful TCP connection for communication. Also has support for WebSocket. RabbitMQ binds MQTT topic to AMQP topic with amq.topic exchange.

Connecting to RabbitMQ broker from terminal using mosquitto cli,

Subscribed to RabbitMQ broker using mosquitto mqtt client cli
import (
"github.com/streadway/amqp"
"fmt"
)

func main() {
conn, err := amqp.Dial("amqp://test:[email protected]:5672/")
if err != nil {
fmt.Println(err)
return
}

ch, err := conn.Channel()
body := "Hello from AMQP"
err = ch.Publish(
"amq.topic", // exchange
"hello.hello", // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
DeliveryMode: 1,
Body: []byte(body),
})
}

Publishing message from AMQP go client to MQTT client. Note that, exchange is amq.topic as by default rabbitmq binds mqtt topics to amq.topic exchange.

RabbitMQ topic binding from RabbitMQ Admin UI

Now publish something from AMQP client and MQTT client will receive that.

See received the message in MQTT client, published from AMQP client

Note another thing that in MQTT, multi path topic looks like /hello/hello and similar thing in AMQP is like hello.hello .

So now your message queue is more reliable and secure as its based on AMQP. In other hand because of MQTT you can optimise resources when using lightweight devices.

RabbitMQ [ Opensource, Erlang platform ]: https://github.com/rabbitmq

EMQ [ Opensource, Erlang platform ] : https://github.com/emqtt

read original article here