Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine user ID of MQTT clients?

Tags:

mqtt

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?

like image 270
sbhatla Avatar asked Aug 19 '14 19:08

sbhatla


People also ask

How do I find my MQTT client ID?

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.

What is MQTT username and password?

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.

What is client ID in MQTT Mosquitto?

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.

How do I find my MQTT broker IP address?

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.


1 Answers

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"}

like image 164
Matt. Avatar answered Oct 18 '22 15:10

Matt.