Comments (9)
Hmmm, my use case is to embed mqtt client in IoT device and let it go into the wild, and report some data.
In such case I would except from program (device), to warn me about bad configuration, but keep retrying to connect without crashing. I am configuring device once (asserting that it is working), and letting him live its own life (like gps trackers :) ).
But there are other factors that might fail and device should not care about them. Imagine that your device reboots during such conditions
- broker goes offline (crash/update) - it is temporary unavailable, but
connect
fails due toConnectionRefusedError
- network goes down - temporarily, but
connect
fails due tosocket.gaierror
- somebody deletes (mqtt) user credentails on server, and later restore them,
So if device reboots and goes through connect
while one of above edge cases occur, then program would crash.
However when all exceptions on connect
are handled and reconnect
is manually rescheduled then it works (as suggesed).
That is why I am suggesting that connect
should reconnect automatically but notify rest of program about connection failure through some callback.
from gmqtt.
Hey @d21d3q
I added kwarg raise_exc=True
to client.connect
method. Together with unlimitted reconnects this should work in your case. LEt me know if it helps 🙂
from gmqtt.
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718033
If a server sends a CONNACK packet containing a non-zero return code it MUST then close the Network Connection [MQTT-3.2.2-5]
gmqtt doesn't implement the MQTT standard for this particular point.
from gmqtt.
Actually this is expected behavior. Reconnects are good when you client is working, but connection at the moment becomes broken and after some time your client automatically reconnects. If the first connection attempt was unsuccessful, it probably means that configuration is bad, host, port or authorization method should be fixed.
from gmqtt.
@d21d3q does this resolve your problem? Can this issue be closed?
from gmqtt.
I will have a look at it on Monday. Thank you.
from gmqtt.
No, this is not the correct way of handling this. If you take a look at the reconnect
function you will see that it handles OSError
and connect
function does not. This is why the client explodes when broker is not online and you try to connect.
Workaround is something like this:
client = MQTTClient("client_id")
while True:
try:
await client.connect(broker_host)
except OSError:
print(f'Can not connect to the broker at {broker_host}')
await asyncio.wait([asyncio.sleep(5), STOP.wait()], return_when=asyncio.FIRST_COMPLETED)
if STOP.is_set():
sys.exit()
else:
break
await STOP.wait()
await client.disconnect()
from gmqtt.
@NICOLASGON Specification tells server must close the connection after it sends bad CONNACK package. gmqtt is about client relaization and docs do not specify any required actions after receiving bad connack code.
from gmqtt.
@Lenka42 - Using the current master branch (as of 25 FEB 2022), what is the correct answer?
This works:
try:
await lLocalMRClient.connect("localhost", 1884, keepalive=60)
except ConnectionRefusedError:
asyncio.ensure_future(lLocalMRClient.reconnect(delay=True))
as does:
try:
await lLocalMRClient.connect("localhost", 1884, keepalive=60, raise_exc=True)
except:
asyncio.ensure_future(lLocalMRClient.reconnect(delay=True))
And I see above the suggestion to catch OSError
from gmqtt.
Related Issues (20)
- How to increase the max in IdGenerator from client? HOT 1
- Resubscribe after autoreconnect HOT 1
- [PROPERTIES] received invalid property id 105, disconnecting HOT 2
- Handler does not work for null body retained messages published before the handler created HOT 6
- Client still running after disconnect() in 0.6.9 HOT 1
- Exceeded reconnect_retries seems seems not to be working HOT 1
- Unsubscribe doesn't remove the topic from the internal list
- I can't to use ca.crt, client.crt, client.key (X509 certificate) to connect mqtt broker. HOT 2
- Read message from mqtt and post Data to wialon HOT 1
- request and response mechanism
- won't remove resend messages if PUBREC is faster than message pushed HOT 1
- publish() and then disconnect() may result in messages not received by a broker
- is there a handler like python.paho.mqtt message_callback_add? HOT 2
- No PubBack Message HOT 1
- Connection error
- [RECV EMPTY] Connection will be reset automatically. HOT 1
- blocking the loop when processing messages for a long time HOT 1
- Formal documentation for API HOT 1
- The subscribe non blocking method
- The QoS 2 Message Receive Queue is not being cleared HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gmqtt.