【问题标题】:How to speed up the recovery of mqtt subscription messages after disconnected network reconnection断网重连后如何加快mqtt订阅消息的恢复
【发布时间】:2019-08-02 01:35:18
【问题描述】:

我有一个python项目代码,需要服务器向设备发送mqtt消息,它需要解决设备和云服务器应用程序操作和信息同步的问题。发现paho.mqtt!的Client函数有一个参数Client(client_id="", clean_session=True)可以恢复未连接的订阅接收离线时发送的消息。但我在实验中发现,断开连接的时间越长,恢复所需的时间就越长,消息传递的速度也会越慢。

如果你们能给我一些建议以加快恢复或任何有关云设备应用程序操作和信息同步的相关参考,我将不胜感激。

我已经写了测试断线恢复

client = mqtt.Client(client_id='Server',clean_session=False)
client.enable_logger(logger)
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_IP,MQTT_PORT,60)
client.subscribe(topic="Cloud/#",qos=2)
client.loop_forever()

def on_message(client, userdata, msg):
    topic = msg.topic
    payload = msg.payload.decode('utf-8')
    print(topic)
    print(payload)
    pattern_create = 'Cloud/create'
    pattern_delete = 'Cloud/delete'
    pattern_modify = 'Cloud/modify'
    pattern_connect = 'Cloud/connect'
    if re.match(pattern_create, topic):
        pub("Device/create","create",MQTT_IP,MQTT_PORT)
    elif re.match(pattern_delete, topic):
        pub("Device/delete","delete",MQTT_IP,MQTT_PORT)
    elif re.match(pattern_modify, topic):
        pub("Device/modify","modify",MQTT_IP,MQTT_PORT)
    elif re.match(pattern_connect, topic):
        print('connected')
        pub("Device/connect","connected",MQTT_IP,MQTT_PORT)

设备

def on_message(client, userdata, msg):
    info = msg.payload.decode('utf-8')
    print("##################info=",info)

def wait_to_handle("Device/#", mqtt_host, mqtt_port):
    client = mqtt.Client(client_id="wait_to_handle", clean_session=False)
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(mqtt_host,mqtt_port,60)
    client.subscribe(topic=topics,qos=2)
    client.loop_forever()

def send_data_func():
    client = mqtt.Client('SendToCloud',clean_session=False)
    client.on_publish = on_publish
    client.connect(MQTT_IP, MQTT_PORT , 60)
    client.loop_start()
    count = 0
    while 1:
        print(count)
        client.publish('Cloud/count',str(count),2)
        count += 1
        time.sleep(2)

设备结果

...
much Device sent message
...
Device sent message
Device sent message
Device sent message
Device sent message
Device sent message
Device sent message
18666
18667
18668
wait handle : Connected with result code 0
##################info= create
##################info= delete
##################info= modify
##################info= create
##################info= delete
##################info= modify
##################info= create
##################info= delete
##################info= modify
##################info= create
##################info= delete
##################info= modify
18669
18670
18671
18672
18673
18674
Device sent message
Device sent message
Device sent message
Device sent message
...
...

【问题讨论】:

    标签: python-3.x mqtt iot paho


    【解决方案1】:

    如您所述,通过禁用 clean_session 标志使用持久会话可维护所有活动订阅,这样客户端无需重新订阅,并且如果客户端将消息排队(仅 QoS 1 或 QoS 2)暂时失去连接并重新连接。

    代理将按照配置将尽可能多的消息排队,这可能是数千条 - 因此恢复时间与断开连接的时间成正比。

    您的选择是:

    • clean_session 标志设置为true,这将禁用所有错过消息的排队,但还需要您重新订阅感兴趣的主题。
    • 保持clean_session 标志为false 以维护您的订阅,但您的主题的发布者必须以 0 的 QoS 发布以绕过排队
      • 此外,发布者可以在已发布的消息上设置retained 标志设置为true,这将使代理持有关于该主题的最新消息,并在重新发送给客户端-连接
    • clean_session 标志设置为true,但确保生产者发布时将retained 标志设置为true,这将使代理持有关于该主题的最新消息并在重新连接时将其发送给客户端,但将支持任何 QoS
    • 配置代理以减少错过的消息排队

    【讨论】:

      猜你喜欢
      • 2021-03-14
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多