Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [paho-dev] python client v5 persistent session to mosquitto...

typo... the example uses clean_start, but it doesn't work... 

On Tue, Apr 6, 2021 at 1:05 PM Frank Pagliughi <fpagliughi@xxxxxxxxxxxxxx> wrote:
v5 connections use clean start, not clean sessions.


On 4/6/21 12:55 PM, Peter Silva wrote:

clean_session is not working properly for me in paho.mqtt.client.... wondering if
folks have any clues.

There is a mosquitto on localhost...:

in one shell, I run:
mosquitto_pub -h localhost -u x -P y -q 1 -V mqttv5 -m hello -t test/hello
(as many times as needed.)

in another shell
 mosquitto_sub -h localhost -c -i itsme  -u x -P y -q 1 -V mqttv5 -t 'test/#' -F '%t %m'

and mosquitto_sub prints: 
test/hello 1
test/hello 2

then I stop the subscriber.  I do two more posts, then I restart the subscriber, 
and I see:

test/hello 3
test/hello 4

The idea being that the messages were queued while the sub was away, when it came
back with clean_session=False, it caught up with the messages.

So far so good.  I try to do the same thing in python:

import logging
import paho.mqtt.client

logger = logging.getLogger(__name__)

cid='ring around the rosy, pocket full of posies'

def __sub_on_connect(client, userdata, flags, rc, properties=None):

        logger.error( "rc=%s, flags=%s" % ( paho.mqtt.client.connack_string(rc), flags ) )

        if flags['session present'] != 1:
            logger.error( 'failed to find existing session, no recovery of inflight messages from previous connection' )

        client.subscribe( 'test/#', qos=1 )

def __sub_on_subscribe(client, userdata, mid, granted_qos, properties=None):
        logger.error( "subscribe completed mid={} granted_qos={}".format( mid, list( map( lambda x: x.getName(), granted_qos ))) )

def __sub_on_message(client, userdata, msg):
        logger.error( "Message received: mid=%d, topic=%s body=%s" % (msg.mid, msg.topic, msg.payload.decode('utf-8') ) )

client = paho.mqtt.client.Client( client_id=cid, protocol=paho.mqtt.client.MQTTv5 )
client._on_connect_ = __sub_on_connect
client._on_message_ = __sub_on_message
client._on_subscribe_ = __sub_on_subscribe

client.username_pw_set( username, password )

client.connect_async( host,  clean_start=clean_start )

when I run it, I always see:

rc=Connection Accepted., flags={'session present': 0}

So it never finds the existing session, and never recovers the messages it missed.

any ideas?

paho-dev mailing list
To unsubscribe from this list, visit

paho-dev mailing list
To unsubscribe from this list, visit

Back to the top