eclipse-paho / paho.mqtt.python

paho.mqtt.python
Other
2.2k stars 721 forks source link

why connect() returns but connection is not established? #454

Closed charleshuangcai closed 10 months ago

charleshuangcai commented 4 years ago

paho version is 1.5.0, python 3.8 code example: client.connect(host=self.profile.get_host(), port=self.__profile.get_port(), keepalive=self.profile.get_keep_alive_interval()) client.loop_start() client.is_connected()

but client.is_connect() reslut is false. why sync method connect returns but connection is not established?

smuu commented 4 years ago

Have the same problem and can't find a solution.

client.connect(..) returns 0 (MQTT_ERR_SUCCESS) so no error occur while connecting.

smuu commented 4 years ago

I have another python script to publish data to mqtt.

client.connect(host, port)
client.is_connected()
client.publish(topic, message)
client.disconnect()

client.is_connected() returns False but data get published to the mqtt broker.

cjdcordeiro commented 4 years ago

same here

billimek commented 4 years ago

observing the same behavior

smuu commented 4 years ago

For me was the problem, that I used websockets at the mqtt broker and forgot to specify that while creating the client.

denravonska commented 4 years ago

Same issue here. connect is asynchronous and requires an event loop. That defeats the purpose of a synchronous variant.

dxmann commented 4 years ago

I'm working with AWSIoT. After understanding that is not useful set the CA cert path, I have encountered your same problem. Looking on called callbacks I have on_disconnected called with error code 1: as wrote here, 1 is "Connection refused – incorrect protocol version". so I set the protocol to MQTTv311, the only one used by AWSIot, but with the same result.

eohlde commented 4 years ago

Same issue here. is_connected() always returns False, but MQTT data is being published. I have also overridden the on_connect and on_disconnect callbacks and they are never triggered.

wanZzz6 commented 3 years ago

Have the same problem

Haifischbecken commented 3 years ago

Ran into a similar problem, I didn't know that calling client.tls_set_context() or client.tls_set() was necessary as it wasn't part of any introduction I found online and I'm quit new to this whole networking thing.

chengchenglee commented 3 years ago

I'm working with AWSIoT. After understanding that is not useful set the CA cert path, I have encountered your same problem. Looking on called callbacks I have on_disconnected called with error code 1: as wrote here, 1 is "Connection refused – incorrect protocol version". so I set the protocol to MQTTv311, the only one used by AWSIot, but with the same result.

Same here. I am using aws iot mqtt for data transmit.

I am using mqtt_bridge.

every time , the _ondisconnect will give the response code 1 back .

I do not know how to solve it. I also raised a question here. https://github.com/groove-x/mqtt_bridge/issues/61

MattBrittan commented 3 years ago

Connecting to a broker is a two stage process:

Taking a look at the source it appears that connect returns when the first step (establish network connection) is complete and the CONNECT packet has been sent (connect returns reconnect() returns _send_connect()). At this point in time the second part of the process (receiving/processing the CONNACK) has not happened; that happens latter within the network loop code. Code called within the loop:

This has a few implications:

The above is my assessment based upon a review of the code; I'd appreciate it if someone could confirm my findings and I'll propose some updates to the docs.

FeroxTL commented 1 year ago

Made cool workaround:


def connect():
    client = mqtt.Client()
    client.connect(host="host", port="port")

    started = time.time()
    while time.time() - started < 5.0:
        client.loop()
        if client.is_connected():
            return client

    raise OSError('Not connected')
MattBrittan commented 10 months ago

Closing this because my PR (#615) to update the documentation has now been accepted. Hopefully that will clarify things a bit!