0

I have defined my callback function in another python module. How do I attach that callback which is defined in another module to paho.mqtt client callback? For. e.g I have a database.py where I perform all the database operations such as connecting to database and storing the messages in database and mqttbroker.py module where I create a mqtt.client() instance and connect to the broker and subscribe to the topics and define some callbacks like on_connect and on_disconnect. Now for on_message mqtt callback, I have created the callback function in my database.py module. How do I attach this callback? I have main.py(my client) which imports the database.py and mqttbroker.py module.

user2301
  • 1,857
  • 6
  • 32
  • 63

1 Answers1

0

define interface and call that interface in client module and attach callback defined in database module to this called interface.

#mqttbroker.py
def onMessage(self, callback):
        self.client.on_message = callback
        self.client.loop_forever()

#database.py
def on_message(self,client,userdata, msg)
    #store messsages in db

#main.py
import mqqtbroker
import database

client = mqqtbroker()
client.connect(host, port, 60)
client.onMessage(database.on_message)
user2301
  • 1,857
  • 6
  • 32
  • 63
  • This code is only for prototyping some infrastructure testing purpose to see possibilities on how we can separate the database, client and message broker modules. – user2301 Dec 18 '17 at 14:25