0

please help me with this. I have spent good two days on this with no success.

I am using Paho-Mqtt python client example. I also have HiveMq mqtt server on my digitalocean debian server. The client example code in python can connect and subscribe to digitalocean server IF I run it on my laptop or on my linux Debian machine. BUT. here is the BUT. when I run the client on digitalocean server (where hivemq mqtt server is) it can only connect and does not subscribe! I used both localhost and server IP, but still no luck.

However the same client code running on digitalocean server (along with hivemq mqtt server) can successfully connect and subscribe to external servers such as m2m.eclipse.org.

How do I know it doesn't subscribe but connects? well, it returns RC:0 on connect callback, but does not return anything on subscribe callback. (it should return 'Subscribe: 1' etc)

In conclusion: Clinet can connect but cannot subscribe when it is running on the same machine as the server.

Oliver
  • 11,857
  • 2
  • 36
  • 42
  • Can you provide any actual details of e.g. code or exactly what you are doing so someone can help? At the moment this is a very vague question. – ralight Jul 27 '15 at 12:44
  • @ralight I am using the sub.py example that is provided in paho project. I did not change the code at all (except the ip address and topic afcourse). – Shahab Intezari Jul 27 '15 at 12:58
  • if the on_log function on sub.py doesn.t provide you any clue then you have to take a look at the HiveMq logs. – Teixi Jul 27 '15 at 14:26
  • Can you add the code you're using to the question? Which version of HiveMQ do you use? – Dominik Obermaier Jul 28 '15 at 10:04
  • I was using the latest version of HiveMQ. I ended up removing the HiveMQ and using Mosquitto instead. it is now all solved. bloody HiveMQ was the one causing this problem. It wasted my 2-3 days.... thanks for the help guys – Shahab Intezari Jul 28 '15 at 11:40

1 Answers1

0

The problem seems to be that the sub.py example (from here: http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.python.git/tree/examples/sub.py) seems to be incorrect. It does not execute the subscribe in the on_connect callback but does it directly after connecting. My understanding is, that the connect() method does not block for the connack.

You could verify this yourself if you sleep for e.g. a second after connecting.

So just call the subscribe() in the on_connect callback.

The official Paho page shows how to do this correctly, see the getting started page here: https://eclipse.org/paho/clients/python/

The reason why this wrong code works with mosquitto and not with HiveMQ is, that mosquitto is single threaded and HiveMQ is async and multi-threaded. So the connect and subscribe can get executed in parallel, you want the sequential behaviour most likely, this is why you must use the on_connect() callback.

Dominik Obermaier
  • 5,610
  • 4
  • 34
  • 45