← Back to team overview

mosquitto-users team mailing list archive

Re: Connection resilient client

 

Thanks Roger and Jan-Piet for the help.  Clearly I'm not familiar enough
with all the properties on the client object that I should be setting for
this scenario.  Is there documentation that will help me with this, or do I
just need to go read through the paho client code?

Thx, Jon


On Tue, Feb 11, 2014 at 12:46 AM, Roger Light <roger@xxxxxxxxxx> wrote:

> Hi Jon,
>
> The point that Jan-Piet alludes to is important here. If you are using
> clean_session=False and have a consistent client id, then this should
> just work fine. If you have clean_session=True, then the broker will
> remove all information regarding your client when you disconnect -
> including subscriptions. The client module doesn't keep track of what
> topics you are subscribed to, so when it reconnects for you the
> subscriptions will not be recreated.
>
> I would suggest putting your call to subscribe() in the on_connect()
> callback, then it will be called if the client has to reconnect.
>
> Cheers,
>
> Roger
>
>
> On Mon, Feb 10, 2014 at 11:35 PM, Jon McDermott <jonmc56@xxxxxxxxx> wrote:
> > Hi Roger,
> >
> > Thanks for the help.  Yes - 'crash' was a little melodramatic, I did mean
> > that the script terminated with a network connection error.
> >
> > Your suggested fix did the trick.  The publish logic works as
> advertised, if
> > I stop the broker service the client queues up the messages until the
> broker
> > becomes available again.
> >
> > Now the subscriber is not working as I expect.  All goes well until I
> stop
> > the broker service to simulate a network connection problem.  The client
> > disconnects as expected, then re-connects once the broker service is
> > re-started (I can see it reconnect because of some debug print
> statements in
> > my on-connect callback method.
> >
> > The issue is that my subscriber doesn't receive any of the messages that
> > were queued by the publishing client, and in fact doesn't receive any new
> > messages that the publishing client publishes after the broker service is
> > started again.  In other words, after the broker is stopped and
> restarted,
> > the subscriber never receives a subsequent message again.
> >
> > Here's my subscribe code:
> > mqttclient.connect(HOST, PORT, 60)
> > mqttclient.subscribe("Jon", 1)
> > mqttclient.loop_forever()
> >
> > Apologies if my questions are very rudimentary.
> >
> > Thanks, Jon
> >
> >
> > On Tue, Feb 4, 2014 at 1:32 PM, Roger Light <roger@xxxxxxxxxx> wrote:
> >>
> >> Hi Jon,
> >>
> >> I presume by "crash" you mean it complains about a broken pipe or
> >> similar socket error rather than something more dramatic :)
> >>
> >> There are a couple of things I can suggest, the first is that you need
> >> to use one of the loop*() functions. You need to use these to ensure
> >> that the background network traffic is processed. You have a couple of
> >> options - you can use loop() which you would call yourself in your
> >> loop. You can also use loop_start() once after calling connect().
> >> loop_start() creates a thread that calls loop() for you. The final
> >> choice which isn't appropriate in your case is to use loop_forever(),
> >> which is a blocking call and means your loop couldn't work.
> >>
> >> The Python client queues up messages that you send with publish() even
> >> if the client isn't connected.
> >>
> >> If you use loop() yourself, you need to check the return value to see
> >> whether you need to reconnect. If you do, call reconnect().
> >>
> >> Using loop_start() takes this out of your hands because it will do the
> >> reconnection for you, no problem. Likewise loop_forever().
> >>
> >> For ease, I'd do:
> >>
> >> mqttclient.connect(HOST, PORT, 60)
> >> mqttclient.loop_start()
> >> while True:
> >>     payload = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
> >>     print('About to publish: {P}'.format(P=payload))
> >>     mqttclient.publish("Jon", payload, 1, retain=False)
> >>     # wait for 10 seconds
> >>     time.sleep(10)
> >>
> >> Cheers,
> >>
> >> Roger
> >>
> >> On Tue, Feb 4, 2014 at 7:34 PM, Jon McDermott <jonmc56@xxxxxxxxx>
> wrote:
> >> > Hello,
> >> >
> >> > I am trying to understand how to create a Python client that is
> >> > resilient to
> >> > connection/connectivity problems.
> >> >
> >> > I'm using the latest Paho MQTT client on Win8 and Python 2.7.4.  This
> is
> >> > just my development platform, my plan is to deploy the code on
> >> > RaspberryPi
> >> > with a WiFi adapter.
> >> >
> >> > When I try this simple publish code:
> >> >
> >> > mqttclient.connect(HOST, PORT, 60)
> >> > while True:
> >> > payload = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
> >> > print('About to publish: {P}'.format(P=payload))
> >> > mqttclient.publish("Jon", payload, 1, retain=False)
> >> > # wait for 10 seconds
> >> > time.sleep(10)
> >> >
> >> >
> >> > it publishes to my Mosquitto broker every 10 seconds, and I'm able to
> >> > subscribe to these messages by topic.
> >> >
> >> > If I break the connection, however, or stop the broker service - it
> >> > crashes
> >> > the client on the next attempt to publish.  (this happens using all 3
> >> > QOS
> >> > options)
> >> >
> >> > Is there anything in the Paho MQTT client that can help me queue these
> >> > published messages until the broker becomes available again?  Any
> >> > example
> >> > code available that would show me how to gracefully handle these
> >> > connection
> >> > errors?
> >> >
> >> > Thanks in advance.
> >> > Jon
> >> >
> >> > --
> >> > Mailing list: https://launchpad.net/~mosquitto-users
> >> > Post to     : mosquitto-users@xxxxxxxxxxxxxxxxxxx
> >> > Unsubscribe : https://launchpad.net/~mosquitto-users
> >> > More help   : https://help.launchpad.net/ListHelp
> >> >
> >
> >
>

Follow ups

References