1
votes

I am using mqtt in my nodejs backed application where there is communication with iot devices and where i am able to publish and subscribe to different topic.every thing is working well i can publish and subscribe to different topic.

I am able to send client id or the publisher id who publish topic in message part which is a repeated task for every publisher.

Is there any way we can know who is publisher while subscribing to topic. my mqtt client code at nodejs is

  var mqtt = require('mqtt')

  var url = "http://127.0.0.1:1883";

  var client = mqtt.connect(url);

client.on('connect', () => {
   console.log('Mqtt client connected to broker');
   client.subscribe('status/open');
   client.subscribe('status/close');
   client.subscribe('sendMsg');

})

client.on('message', (topic, message) => {
  console.log('new topic received at controller', topic);
  console.log('new messge received at controller', message.toString());
///is there any way to determine who is publishere here

 });
1

1 Answers

1
votes

No.

This goes against one of the core principals of pub/sub messaging, which is that a publisher shouldn't care who is subscribed to a given topic and a subscriber shouldn't care where the data is coming from, it just subscribes to a topic.

If you want to make it clear you have 2 options.

  1. Use a topic structure that encodes the publisher in the topic and the subscriber uses wildcard topics to receive data from all publishers.
  2. Encode the publisher information into the message payload.