find publisher id when subscriber subscribe to topic in mqtt

Internet of Things IoT Frameworks 3 years ago

4.7K 1 0 0 0

_x000D_ _x000D_ 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 });

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago
_x000D_ 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. Use a topic structure that encodes the publisher in the topic and the subscriber uses wildcard topics to receive data from all publishers. Encode the publisher information into the message payload.
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum


Q

Azure IoT hub and sending messages with mosquitto_pub

_x000D_ _x000D_ I'm trying to send some simple message with mosquitto_pub to Azure IoT HUB but faced...
Q

How to generate AWS bootstrap certificates with java SDK?

_x000D_ _x000D_ According to this docs I need to generate so-called bootstrap certificates for my Io...
Q

is it possible to do 3way handshake only one time with mqtt communication?

_x000D_ _x000D_ I am using mosquitto_pub to publish the data with TLS using a topic. I am using mosq...

Important Internet of Things Links