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...

that was it. THANK YOU!

from import Properties
from paho.mqtt.packettypes import PacketTypes

connect( ... properties=props )

works fine!

On Tue, Apr 6, 2021 at 1:21 PM Roger Light <roger@xxxxxxxxxx> wrote:
Hi Peter,

In v5, clean start only affects the beginning of the session. If you want the session to carry on after you have disconnected, you need to add the "session expiry interval" property on either your CONNECT or DISCONNECT message. I can't remember how to do that in the Python client off the top of my head.

mosquitto_sub cheats a little bit on your behalf and adds that automatically if you use -c,so that it has consistent behaviour for both all protocol versions.



On Tue, 6 Apr 2021, 18:09 Peter Silva, <peter@xxxxxxxxxxxxxxx> wrote:
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
paho-dev mailing list
To unsubscribe from this list, visit
paho-dev mailing list
To unsubscribe from this list, visit

Back to the top