Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use client id in Mosquitto MQTT?

Tags:

mqtt

mosquitto

I am new to Mosquitto. I have installed Mosquitto and Mosquitto Client in Ubuntu. I am trying to subscribe with client-id also publish with client-id, please look the command that I have run in console, but unfortunately, the subscriber is not receiving the message.

Subscription mosquitto_sub -h localhost -t temp/city1 -c -q 2 --id client-one

Publish mosquitto_pub -h localhost -t temp/city1 -m "32 Celsius" -q 2 --id client-one

but if I Publish message without client id the subscriber is receiving message, so please help me where I did the mistake?

like image 697
Karthikeyan Raju Avatar asked Oct 15 '25 14:10

Karthikeyan Raju


1 Answers

As mentioned in the comment, clientIds are just that, they are a unique identifier for each client connected to the broker.

ClientIds need to be totally unique, if a second client tries to connect with a clientid that is already connected the broker must disconnect the first client to allow the second to connect (this is dictated by the specification). In the example you have given the subscriber will be disconnected before it receives the message published by the second.

Messages are published to topics and clients can subscribe to these topics (or patterns of topics with wildcards)

So using the mosquitto command line tools you can do the following:

mosquitto_sub -v -t 'foo/bar'

This will subscribe to the topic foo/bar and will print out the topic and then message when ever a message is published to that topic. To publish a message containing the string testing you would use:

mosquitto_pub -t 'foo/bar' -m 'testing'

The mosquitto command line tools will generate random clientids if none are provided on the command line.

like image 121
hardillb Avatar answered Oct 17 '25 20:10

hardillb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!