Wednesday, 20 February 2019

How to deal with MQTT with a client and 2 servers replying to each other?

I need help understanding a mqtt case i'm trying to set up. It is composed by 3 files, control.py, server_a.py and server_b.py.

For sake of simplicity, no ssl, so port 1883 is used and QoS is 2. The whole thing is being supervised with MQTT.fx. Everything is running locally on a Mac OS X (Mosquitto server has been installed using brew)

Example has been mainly taken from book Hands-On MQTT Programming with Python written by Gaston C. Hillar. Modified code is here

  1. The general idea is to start an action from an IoT.
  2. It will publish to a server A which is getting info from a database.
  3. This server A will contact another one (let's say server B, within the loop) which will contact an external Web Service. (Sleep 1s)
  4. Once its job done, server B will reply to server A which will have to update database in return.

control.py is publishing to mqtt/listen/thingstodo/commands

server_a.py is subscribing mqtt/listen/thingstodo/#. When work is done, server_a is publishing to myapi/thingstodo/new/thingstodo/commands

server_b.py is subscribing to myapi/thingstodo/new/# and when work is done, is publishing to myapi/thingstodo/done/thingstodo/commands

This being written, here is my question :

When I tried this code, if i change the number of loops to be done to 10, 50 and 100, it seems everything working fine under a certain number of actions, it's taking ages to have a job done message. For more than 50, some messages are not going back to server_A.

comments                mqtt/listen/thingstodo/#    myapi/thingstodo/new/#  myapi/thingstodo/done/#
10 things to do                    1                           10                      10
50 things to do                    1                           50                      41
100 things to do                   1                           100                     Never received (too long ?)  

I presume this is due to a threading problem but i don't know how to resolve it.

Do i have to implement client.loop_start() and loop_stop() on server_A and B ? If positive, how do i do that properly ?

Regards,

Pierre



from How to deal with MQTT with a client and 2 servers replying to each other?

No comments:

Post a Comment