Divyansh Singh
Divyansh Singh

Reputation: 401

Message not receiving after publishing. RABBITMQ MQTT

Am learning MQTT and facing some issues understanding MQTT with RabbitMQ from http://blog.airasoul.io/the-internet-of-things-with-rabbitmq-node-js-mqtt-and-amqp/.

So, the issue here is when I run publisher code, a queue is added mqtt-subscription-test-qos1 but when I message doesn't get added in that queue. Although I've added binding of amq.topic to this queue with key-binding 'presence'.

This is my publisher code

var payload = {
  message : 'Hello'
};

var client = mqtt.connect(url, { clientId: 'test-', clean:true});

client.on('connect', function () {

  client.publish('presence', JSON.stringify(payload), { qos: 1 }, function() {
    console.log("Sent");
    client.end();
    process.exit();
  });
});

and below is my subscriber code.

var client = mqtt.connect(url, { clientId: 'test-', clean:true});

client.on('connect', function () {
  client.subscribe('presence', { qos: 1 });
});

client.on('message', function (topic, message) {
  console.log('received message ',  message.toString());
});

This works, when I don't declare any options with connect function in publisher code. So what I don't get is, isn't publisher supposed to create a queue and then publish to topics?

What am I doing wrong?

Upvotes: 1

Views: 2588

Answers (1)

Shobhana Sriram
Shobhana Sriram

Reputation: 392

You don't need to create a queue before publishing to the topic. When you publish first MQTT message, a queue gets created automatically with the default exchange name "amq.topic" and binding key same as your topic name.

I suspect your subscriber is not receiving the messages published since it starts and subscribes to the topic AFTER the publisher publishes the messages. Try by starting your subscriber first and then start your publisher.

Upvotes: 2

Related Questions