6

I am working to connect my server to the clients via an MQTT broker. The MQTT client gets connected. But after publishing a message, the subscribe code receives a connection acknowledgment. The on_message() function never gets called.

I am stuck here.

I have pasted the subscribe client code and the output.

import paho.mqtt.client as paho
import time

client = paho.Client("local_test")
topic = "topic_1"

def on_log(client, userdata, level, buff):  # mqtt logs function
    print(buff)

def on_connect(client, userdata, flags, rc):  # connect to mqtt broker function
    if rc == 0:
        client.connected_flag = True  # set flags
        print("Connected Info")
    else:
        print("Bad connection returned code = " + str(rc))
        client.loop_stop()

def on_disconnect(client, userdata, rc):  # disconnect to mqtt broker function
    print("Client disconnected OK")

def on_publish(client, userdata, mid):  # publish to mqtt broker
    print("In on_pub callback mid=" + str(mid))

def on_subscribe(client, userdata, mid, granted_qos):  # subscribe to mqtt broker
    print("Subscribed", userdata)

def on_message(client, userdata, message):  # get message from mqtt broker 
    print("New message received: ", str(message.payload.decode("utf-8")), "Topic : %s ", message.topic, "Retained : %s", message.retain)

def connectToMqtt():  # connect to MQTT broker main function
    print("Connecting to MQTT broker")
    client.username_pw_set(username=user, password=passwd)
    client.on_log = on_log
    client.on_connect = on_connect
    client.on_publish = on_publish
    client.on_subscribe = on_subscribe
    client.connect(broker, port, keepalive=600)
    ret = client.subscribe(topic, qos=0)
    print("Subscribed return = " + str(ret))
    client.on_message = on_message

connectToMqtt()  # connect to mqtt broker
client.loop_forever()

And the output I get after publishing the message on the same topic is:

Connecting to MQTT broker
Sending CONNECT (u1, p1, wr0, wq0, wf0, c1, k600) client_id=b'local_test'
Sending SUBSCRIBE (d0) [(b'topic_1', 0)]
Subscribed return = (0, 1)
Received CONNACK (0, 0)
Connected Info
Received SUBACK

Subscribed None
Sending CONNECT (u1, p1, wr0, wq0, wf0, c1, k600) client_id=b'local_test'
Received CONNACK (0, 0)
Connected Info

EDIT 1:

Also, I am seeing that my broker has sent the message from the publisher to the client, but the client isn't able to receive it.

5
  • 1
    set the on_message call back before connecting
    – hardillb
    Jun 21, 2019 at 16:55
  • 1
    Also edit the question to include the mqtt_start() function so we can see if it ever makes it to the client.loop_forever() call
    – hardillb
    Jun 21, 2019 at 16:56
  • 1
    upvote for including the output of the print statements ... so many people ignore the printouts when posting here ... I'm whining here ... lol
    – jsotola
    Jun 21, 2019 at 17:49
  • 3
    I would do the client.subscribe in the on_connect callback, ie. once it is connected. That also re-subscribes if you handle disconnects. Jun 21, 2019 at 19:16
  • @hardillb my bad ... there is nothing in the mqtt_start() function. I forgot to remove it. Also, setting on_message callback before on_connect didn't make any difference.
    – ron123456
    Jun 24, 2019 at 5:44

2 Answers 2

0

As others have mentioned/implied in the comments:
The actual sequence that is supposed to happen is:

Setup your callbacks correctly first (including the on_message).
Call connect.
Wait until you get back an ack for the connect.
Then, send out a subscribe request. (This will actually send a message on TCP to the broker. That cannot happen if the connect is not done.)
Note that after the subscribe is complete and you've got a positive ack, there maybe a small amount of time for which the subscription is not actually complete at the broker. This is especially true in large brokers that use a cluster of computers. Usually though, this time of uncertainity is small. A few hundred ms max.
After the subscribe is acked (and after a second, if you can delay it), if you send a message from your publisher, the broker should be able to forward it to your client you should be able to see it.

1
  • Gosh. I didnt realize that the original question had been asked 2 years ago ! Hopefully, the answer will help someone in future. So, I'll leave my answer undeleted. Aug 30, 2021 at 22:15
0

Just a flash from the past as I was trouble shooting this same issue, you need to have a unique name on the mqtt object created client = paho.Client("local_test") If you have multiple instances connecting to the same broker with the same name they will disconnect each other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.