0

I'm trying to use my Flask app as a subscriber, but it does not call the on_message callback when receiving a message. Instead, I get about like the following:

Connected with result code 0
Closing data file...
Connected with result code 0
Closing data file...

This is how I am running the Flask app:

main.py:

from flask import Flask, render_template, redirect, url_for
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import *
import paho.mqtt.client as mqtt
import time

broker_address = <broker_ip>
port = 1883
timeout = 60
username = "first"
password = "last"
uuid = "1234"
topic = "mytopic"
qos = 0



def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))

    client.subscribe(topic)

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload) + '\n')

def on_disconnect(client, userdata, rc):
    print("Closing data file...")

client = mqtt.Client(client_id=uuid)
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.username_pw_set(username, password)

client.connect(broker_address, port, 60)
client.loop_start()

<other Flask code>

if __name__ == "__main__":
    app.run(debug=True)

I've tried using a another Python script to generate some fake data to publish to the topic, but only when that script is running to I get the output above. If that script is not running, then the main.py seems to wait for messages. This is the other script:

fake_data.py:

import paho.mqtt.client as mqtt
import time

broker_address = <broker_ip>
port = 1883
timeout = 60
username = "first"
password = "last"
uuid = "1234"
topic = "mytopic"
qos = 0



def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))

client = mqtt.Client(client_id=uuid, clean_session=True)
client.on_connect = on_connect

client.username_pw_set(username, password)

client.connect(broker_address, port, 60)


client.loop_start()

while True:
    count = 0
    while count != 30:
        print("Publishing {0}".format(count))
        client.publish(topic, count, qos=0)
        count += 1
        time.sleep(1)

My question is why the Flask app keeps connecting and disconnecting endlessly without actually processing a message.

rafafan2010
  • 1,559
  • 2
  • 14
  • 23

2 Answers2

1

The client id needs to be different for all clients connected to a broker. In the code you've posted both the subscriber and the publisher are using the same client id (uuid=1234).

When 2 clients with the same client id connect to the broker, the broker will kick the oldest off. If this is then set to reconnect it will then kick the second one off.

Set uuid to different values.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • I did this, and it does work. However, the `client` still connects and disconnects after receiving and printing the message. While this does not block my app, I thought that the expected behavior would be that the connection stays open until I shut down the whole app. Would you know why that isn't the case? – rafafan2010 Dec 04 '17 at 14:56
1

I know this is from a long time ago...but I believe I know the answer! In case anyone else is having the same problem (disconnecting and reconnecting): your app has to be set to keep looping the client. "client.loop_start()" is actually behaving as designed in your case. If you want the connection to stay open for a program that runs indefinitely, replace that with "client.loop_forever()".

My First Answer! Cheers!

GT1101
  • 11
  • 1
  • This is not right, if you call `loop_forever()` the script will block at that point and never run any of the code that follows it. – hardillb Jan 14 '21 at 07:55