Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How subscribe in an RabbitMQ queue with MQTT Paho

Tags:

rabbitmq

mqtt

I am trying to connect from my Android app to one queue called "messages".

The producer (one webservices under AMQP protocol) is already connected, it can be check through RabbitMQ admin panel.

To connect from my Android device I am coding like this.

private void connect() throws Exception {

    this.sampleClient = new MqttClient(this.broker, this.clientId);

    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setUserName("user");
    connOpts.setPassword("user".toCharArray());
    /*connOpts.setConnectionTimeout(60 * 10);
    connOpts.setKeepAliveInterval(60 * 5);*/
    connOpts.setCleanSession(true);

    this.sampleClient.connect(connOpts);

    this.sampleClient.setCallback(this);

    this.sampleClient.subscribe("messages");

    if(!this.sampleClient.isConnected()){

        System.out.println("Not Connected");
        return;
    }

    System.out.println("Connected");
}

I have tried with "amq.topic", "amq.topic.*", "amq.topic.messages", etc... But when I look in the RabbitMQ queue section "messages" is with 0 consumers, and have been set one new queue called "mqtt-subscription-Sampleqos1" automatically.

What's happening? How can I susbscribe to "messages" queue?

like image 267
Dani Avatar asked Dec 01 '14 12:12

Dani


People also ask

How do I subscribe to MQTT?

To receive messages on topics of interest, the client sends a SUBSCRIBE message to the MQTT broker. This subscribe message is very simple, it contains a unique packet identifier and a list of subscriptions. Packet Identifier The packet identifier uniquely identifies a message as it flows between the client and broker.

Is RabbitMQ compatible with MQTT?

RabbitMQ supports MQTT 3.1 via a plugin.

How does MQTT subscription work?

MQTT is a publish/subscribe protocol that allows edge-of-network devices to publish to a broker. Clients connect to this broker, which then mediates communication between the two devices. Each device can subscribe, or register, to particular topics.

How does The RabbitMQ MQTT plugin work?

The plugin builds on top of RabbitMQ core protocol's entities: exchanges and queues. Messages published to MQTT topics use a topic exchange ( amq.topic by default) internally. Subscribers consume from RabbitMQ queues bound to the topic exchange.

How to subscribe to a topic in PAHO MQTT?

To subscribe to a topic you use the subscribe method of the Paho MQTT Class object. In this tutorial we will look at some examples of using the subscribe method. The diagram below illustrates the subscribe message flow. The subscribe method accepts 2 parameters – A topic or topics and a QOS...

Does RabbitMQ support qos2 subscriptions?

Queues created for MQTT subscribers will have names starting with mqtt-subscription-, one per subscription QoS level. The queues will have queue TTL depending on MQTT plugin configuration, 24 hours by default. RabbitMQ does not support QoS2 subscriptions.

How do I subscribe to a topic in Python MQTT?

Paho Python MQTT Client Subscribe With Examples. To receive messages on a topic you will need to subscribe to the topic or topics. To subscribe to a topic you use the subscribe method of the Paho MQTT Class object. In this tutorial we will look at some examples of using the subscribe method. The diagram below illustrates the subscribe message flow.


2 Answers

There are two important points about this question.

According with the RabbitMQ MQTT documentation: http://www.rabbitmq.com/mqtt.html

Firstly, every queues are bound automatically to amq.topic exchange by the mqtt-plugin.

Secondly, every subscriber has his own queue which look like this, mqtt-subscription-{cliend_id}{qosX} (where X is the qos level of the subscription)

Therefore, producer must to publish the message to "amq.topic" exchange, and "amq.topic.." routing-key, and receiver must to subscribe to "amq.topic.." routing-key.

like image 176
Dani Avatar answered Nov 02 '22 23:11

Dani


First, make sure MQTT plugin is enabled: rabbitmq-plugins enable rabbitmq_mqtt

From the client side (here is you Android app), you need subscriber to a topic, lets say, topic my/android/app/messages

this.sampleClient.subscribe("my/android/app/messages");

Then, from the server side, because of RabbitMQ's implementation, you need send the message to a special exchange 'amq.topic' with appropriate route key my.android.app.messages (notice the mapping between '/' and '.', MQTT use / and AMQP use .). For example if you publish by pika AMQP Python lib, the code will looks like following:

channel.basic_publish(
    exchange='amq.topic',
    routing_key='my.android.app.messages',
    body='hello world'
)

In your case, you want to receive message from queue "messages", basically there is no way to directly subscriber message from that AMQP queue on your MQTT client. The work around is create a service running on your server side, work as AMQP subscriber, receive message from "messages" queue, and transparent forward message to exchange amq.topic with proper routing key.

Hope my answer helpful.

like image 35
Scott Zhou Avatar answered Nov 03 '22 01:11

Scott Zhou