Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

umqtt: keepalive feature doesn’t function correctly #909

Open
ilshm opened this issue Aug 15, 2024 · 1 comment
Open

umqtt: keepalive feature doesn’t function correctly #909

ilshm opened this issue Aug 15, 2024 · 1 comment

Comments

@ilshm
Copy link

ilshm commented Aug 15, 2024

Hi!

Here’s some example code to reproduce the issue:

import ssl
import machine
import network
from umqtt.simple import MQTTClient

client_id = machine.unique_id().hex()
mqtt_server = 'XXX.s1.eu.hivemq.cloud'
mqtt_user = 'XXX'
mqtt_password = 'XXX'
mqtt_topic_sub = b'test/topic'

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('XXX','XXX')

ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)

def mqtt_connect():
    client = MQTTClient(client_id, mqtt_server, 8883, mqtt_user, mqtt_password, 15, ssl_context)
    client.connect()
    print('Connected to %s MQTT Broker'%(mqtt_server))
    return client

def reconnect():
    print('Failed to connect to the MQTT Broker. Reconnecting...')
    machine.reset()
    
def callback(topic, msg):
    print(topic, msg)

try:
    client = mqtt_connect()
    client.set_callback(callback)
    client.subscribe(mqtt_topic_sub)
except OSError as e:
    reconnect()
while True:
    client.wait_msg()

The library allows setting the keepalive to any value (default is 0, which disables it). However, when you set a keepalive value, the library does not automatically send pings to the server as expected. This causes the server to disconnect the client after 1.5 times the specified keepalive duration.

Are there any possible solutions to this?

I tested this with Mosquitto and HiveMQ serverless.

@ondrej1024
Copy link

The ping is not sent automatically by the library. It has to be sent by your code, using the libraries ping() function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants