How to use client id in Mosquitto MQTT?

14,402

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.

Share:
14,402
Karthikeyan Raju
Author by

Karthikeyan Raju

Updated on June 28, 2022

Comments

  • Karthikeyan Raju
    Karthikeyan Raju almost 2 years

    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?

    • Mark Setchell
      Mark Setchell about 6 years
      I think you are confusing client id with topic. What you receive when you subscribe is dependent on the topic you subscribe to, not your id.