← Back to team overview

mosquitto-users team mailing list archive

Re: What happens if the broker loses its network connection?

 

How long are you waiting before deciding that it doesn't work?  You might have to wait for a
TCP connection to timeout before the loop will fail, depending on your OS and network stack.

There've been a couple of changes (long ago) that helped propagate this up, but pulling the
plug on an intermediate cable, so no link detection triggers socket cleanup or anything can
really take a while.

Cheers,
Karl P


On Fri, Dec 13, 2013 at 04:49:48PM +0100, Rene Kogels wrote:
> Thanks Nick. But still, loop() will not detect this. At least not with my
> code, it will stay in the mosq_client.loop==0 loop when the connection to
> the broker is lost. When I shut down the broker normally (service mosquitto
> stop) the loop is stopped, and it tries to reconnect.
> 
> Code (part):
> 
> #Setup mosquitto client config
> mosq_client = mosquitto.Mosquitto("client1")
> 
> #Connect mosquitto client; if we cannot connect, we will notice later with
> mosq_client.loop!=0
> mosq_client.connect("some host")
> 
> #Endless loop
> while 1:
> 
>   while mosq_client.loop()==0:
>       #Put some code here to read serial port to get some data
> 
>       #Publish it to the broker
>       mosq_client.publish("serial/data", serial_data)
> 
>   #If we are here, we lost the connection with the mosquitto broker, so
> sleep first and then try to reconnect.
>   print("No connection with broker. Sleeping before retrying...")
>   time.sleep(10)
> 
>   print("Trying to reconnect")
>   mosq_client.connect("somehost")
> 
> 
> Rene
> 
> 
> On Fri, Dec 13, 2013 at 4:17 PM, Nicholas O'Leary  wrote:
> 
> > Hi Rene,
> >
> > if the broker goes away, the tcp layer will notify the client side that
> > the connection has been lost and the usual connection-lost processing will
> > occur within the client.
> >
> > Regards,
> > Nick
> >
> >
> > On 12 December 2013 20:09, Rene Kogels  wrote:
> >
> >> Hi all,
> >>
> >> I have a question about the behaviour of a python program that publishes
> >> data, but to a broker that lost its network connection after a while. The
> >> loop() function will probably not detect this as it will only detect
> >> changes on the network socket caused by the broker. In this case the broker
> >> could not tell the publisher it lost the connection.
> >>
> >> Will the publisher keep sending data, assuming there is a broker? Or will
> >> it detect somehow the broker is gone (TCP does not get an ack)? Will the
> >> publish function block?
> >>
> >> Thanks,
> >> Rene
> >>
> >>
> >>
> >> --
> >> 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
> >>
> >>
> >

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