eclipse / paho.mqtt.python

paho.mqtt.python
Other
2.14k stars 724 forks source link

Connection issues in TLS/SSL based publisher-subscriber implementation for Mosquitto #746

Closed akshay-tech-prescient closed 9 months ago

akshay-tech-prescient commented 10 months ago

I tried implementing TLS/SSL based publisher-subscriber implementation for Mosquitto (using paho-mqtt) but was facing connection issues in the implementation.

After running publisher script multiple times I observed that first n messages out of 10 (generally saw 1<=n<=5) get successfully published after which the connection gets lost and rest of the messages fail to publish.

When running subsciber script, I was getting Connection refused - not authorised according to docs (since I was getting rc=5 in on_connect(...))

Publisher script:

import time
import paho.mqtt.client as mqtt

messaging_protocol = 'MQTTS'
ca_file = '/home/tp/mosquitto_poc/steves-internet/ca.crt'
broker_address = 'localhost'
topic = 'topic1'

client = mqtt.Client('publisher-client')

if messaging_protocol == 'MQTTS':
    port = 8883
    client.tls_set(ca_file)
else:
    # When messaging_protocol will be 'MQTT'
    port = 1883

client.connect(broker_address, port)

for i in range(10):
    payload = f'Message no. {i+1}'
    print(f'Attempting to publish message: `{payload}`')
    mqtt_msg_info = client.publish(topic, payload)
    try:
        is_published = mqtt_msg_info.is_published()
    except RuntimeError as e:
        print(f'Error occured: `{str(e)}`. Rc value: {mqtt_msg_info.rc}.', end='\n\n')
    except ValueError as e:
        print(f'Error occured: `{str(e)}`. Rc value: {mqtt_msg_info.rc}. Error string: `{mqtt.error_string(mqtt_msg_info.rc)}`', end='\n\n')
    else:
        print(f'Message published: {is_published}. Rc value: {mqtt_msg_info.rc}. Error string: `{mqtt.error_string(mqtt_msg_info.rc)}`', end='\n\n')
    finally:
        time.sleep(0.005)

Sample Output on Publisher script run:

Attempting to publish message: `Message no. 1`
Message published: True. Rc value: 0. Error string: `No error.`

Attempting to publish message: `Message no. 2`
Message published: True. Rc value: 0. Error string: `No error.`

Attempting to publish message: `Message no. 3`
Message published: True. Rc value: 0. Error string: `No error.`

Attempting to publish message: `Message no. 4`
Error occured: `Message publish failed: The connection was lost.`. Rc value: 7.

Attempting to publish message: `Message no. 5`
Error occured: `Message publish failed: The client is not currently connected.`. Rc value: 4.

Attempting to publish message: `Message no. 6`
Error occured: `Message publish failed: The client is not currently connected.`. Rc value: 4.

Attempting to publish message: `Message no. 7`
Error occured: `Message publish failed: The client is not currently connected.`. Rc value: 4.

Attempting to publish message: `Message no. 8`
Error occured: `Message publish failed: The client is not currently connected.`. Rc value: 4.

Attempting to publish message: `Message no. 9`
Error occured: `Message publish failed: The client is not currently connected.`. Rc value: 4.

Attempting to publish message: `Message no. 10`
Error occured: `Message publish failed: The client is not currently connected.`. Rc value: 4.

Subscriber script:

import paho.mqtt.client as mqtt

messaging_protocol = 'MQTTS'
ca_file = '/home/tp/mosquitto_poc/steves-internet/ca.crt'
broker_address = 'localhost'
topic = 'topic1'

def on_connect(client, userdata, flags, rc):
    print(f'Connected with result code {str(rc)}')
    result, _ = client.subscribe(topic)
    print(f'Attempting to subscribe topic: `{topic}`. Result value: {result}. Error string: `{mqtt.error_string(result)}`')

def on_message(client, userdata, msg):
    print(f'Received message: `{msg.payload}` on topic: {msg.topic}')

def on_disconnect(client, userdata, rc):
    print(f'Client disconnected. Rc value: {rc}. Error string: `{mqtt.error_string(rc)}`', end='\n\n')

client = mqtt.Client('subscriber-client')

if messaging_protocol == 'MQTTS':
    port = 8883
    client.tls_set(ca_file)
else:
    # When messaging_protocol will be 'MQTT'
    port = 1883

client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect

client.connect(broker_address, port)

client.loop_forever()

Output on Subscriber script run:

Connected with result code 5
Attempting to subscribe topic: `topic1`. Result value: 0. Error string: `No error.`
Client disconnected. Rc value: 5. Error string: `The connection was refused.`

Connected with result code 5
Attempting to subscribe topic: `topic1`. Result value: 0. Error string: `No error.`
Client disconnected. Rc value: 5. Error string: `The connection was refused.`

Connected with result code 5
Attempting to subscribe topic: `topic1`. Result value: 0. Error string: `No error.`
Client disconnected. Rc value: 5. Error string: `The connection was refused.`

Connected with result code 5
Attempting to subscribe topic: `topic1`. Result value: 0. Error string: `No error.`
Client disconnected. Rc value: 5. Error string: `The connection was refused.`

Connected with result code 5
Attempting to subscribe topic: `topic1`. Result value: 0. Error string: `No error.`
Client disconnected. Rc value: 5. Error string: `The connection was refused.`

Is this a (known) issue in paho-mqtt? If not, I request you to please help me in debugging this TLS/SSL based pub-sub implementation. Thanks in advance for any advice or help.

gdt commented 10 months ago

It is not a known issue. I run a script that publishes a message once per minute and runs for months. You need to do the usual logs on both ends, tcdpump, ktrace/ktruss or equivalent, etc. Also put in a delay and see if that changes things. (I'm not sure of the right venue for getting help on this, but issues doesn't seem right to me.)

akshay-tech-prescient commented 9 months ago

It is not a known issue. I run a script that publishes a message once per minute and runs for months. You need to do the usual logs on both ends, tcdpump, ktrace/ktruss or equivalent, etc. Also put in a delay and see if that changes things. (I'm not sure of the right venue for getting help on this, but issues doesn't seem right to me.)

Thanks for the reply. I was able to get the implementation working correctly by making a change in mosquitto configuration. So, this is indeed not an issue as you rightly pointed. Hence, closing this.