0

How do I publish and subscribe through MQTT in two PC connected in the same network through LAN with Publisher and Subscriber in different PC. I have my PC connected to same network interface (ethernet) with Ubuntu as is operating system.

To test, on subscribing pc i put the command mosquitto_sub -h localhost -t topic_name and on the publisher pc mosquitto_pub -h subscriber_ip -t topic_name -m message. This returns an error, connection refused or sometimes network is unreachable.

How can I resolve this?

Brits
  • 14,829
  • 2
  • 18
  • 31
Parag
  • 13
  • 3
  • 2
    What have you tried and how didn't it work? – hardillb Jul 14 '23 at 07:44
  • For subscribing pc i put the command 'mosquitto_sub -h localhost -t topic_name'. For publisher pc 'mosquitto_pub -h subscriber_ip -t topic_name -m message, but it shows connection refused or sometimes network is unreachable...why? what can be done to fix it?? – Parag Jul 19 '23 at 05:41
  • 1
    Is Mosquitto (or another broker) running on `subscriber_ip`? If so please share your `mosquitto.conf` and the log (set `log_type` to `all` - see the [man page](https://mosquitto.org/man/mosquitto-conf-5.html) for details). Note: when adding detail please edit the question (makes it easier to see all relevant info in one place). – Brits Jul 19 '23 at 05:50
  • Duplicate https://stackoverflow.com/a/65278769/504554 – hardillb Jul 19 '23 at 07:44
  • I did as follows, but now after publishing the message if I subscribe with a new device, it does not receive the message immediately instead I have to publish the message again to recieve it. Why does that happen and how do I fix it? – Parag Jul 21 '23 at 10:38

1 Answers1

0

Install mqtt broker like Mosquitto in both machine. https://www.vultr.com/docs/install-mosquitto-mqtt-broker-on-ubuntu-20-04-server/#Introduction this link could help. You can enter command 'mosquitto -v' to check if it is installed.

Now for subscribing pc hit command 'mosquitto_sub -h localhost -t topic_name'. For publisher pc 'mosquitto_pub -h subscriber_ip -t topic_name -m message. If the message is displayed on subscribing pc, the mqtt is working fine. Now, you can explore security/wildcard and other features as well.

hasmat ali
  • 11
  • 3
  • 2
    "mqtt broker like Mosquitto in both machine" - why both machines? If you are suggesting this to gain access to the command line tools then the `mosquitto-clients` package is probably a better choice. Also note that the default mosquitto configuration only accepts local connections (so some additional configuration is needed - i.e. `listener`). – Brits Jul 16 '23 at 20:09