Currently I have an MQTT system where one client publishes, and one subscribes. Now I want a system where many clients publish. The susbcriber must understand which client is sending information.
While one obvious way to do this is to simply append a client ID in the publishing message, I wanted to know if there is a way of getting client ID without explicitly adding it to the message.
To elaborate, suppose the topic is "/hello/world", and client 1 publshes "OK", client 2 publishes "ERR". Is there a way to determine which client sent what message?
This short article will hopefully help you during your MQTT journey. The short answer is that the client id is chosen by you. Often it's not even necessary to specify a client id because the client library will use a randomly generated if it's not given explicitly.
Username and password authentication is common on all computer systems and the Mosquitto MQTT broker supports this authentication mechanism. To use Password authentication you need to configure the MQTT broker to require it. The username and password are sent in clear text, and you will need to use TLS to secure it.
The client identifier (ClientId) identifies each MQTT client that connects to an MQTT broker. The broker uses the ClientId to identify the client and the current state of the client. Therefore, this Id should be unique per client and broker. In MQTT 3.1.
In order for you to run an MQTT broker on your home network it needs to be reachable at a public IP address. This can be achieved by using the Public IP address the ISP assigned to your router and enabling what is known as port forwarding.
I don't believe you can tell who has sent the message without watching the log.
The ways I have overcome this in the past is to either publish the messages with the client id in the topic; (client_id could be anywhere in the topic)
/hello/clientid1/world ok
/hello/clientid2/world err
/hello/clientid3/world warning
Then subscribe to the topic like this;
/hello/+/world
Then in your code transform the topic name to get the message and client id.
The other approach I use is to use json in the payload, for example
/hello/world {"msg":"err", "client":"clientid1"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With