Connecting with MQTT without WebSockets

ludobar
ludobar Member Posts: 10

Hello everyone,
I'm getting started with Solace, and I am trying to write a simple Python program that publishes a message to a broker topic using the MQTT protocol.
I have issues establishing a connection with the broker service without using Web Sockets. My program does not seem to get any connack message.
I was wondering if any broker configuration step is needed to be able to connect over TLS.
Thanks :)

Tagged:

Comments

  • Aleksei
    Aleksei Member Posts: 14

    Hi! You will need to configure a server certificate on solace broker to activate SSL/TLS ports. Documentation section for the procedure is here: https://docs.solace.com/Configuring-and-Managing/Managing-Server-Certs.htm
    If network security allows it, try connecting first to non-ssl port to confirm the problem is with TLS configuration.

  • hong
    hong Guest Posts: 480 ✭✭✭✭✭

    @Aleksei Thank you for the info!

  • ludobar
    ludobar Member Posts: 10

    @Aleksei said:
    Hi! You will need to configure a server certificate on solace broker to activate SSL/TLS ports. Documentation section for the procedure is here: https://docs.solace.com/Configuring-and-Managing/Managing-Server-Certs.htm
    If network security allows it, try connecting first to non-ssl port to confirm the problem is with TLS configuration.

    I'll configure the server certificate and see if it works. Thank you!

  • Tamimi
    Tamimi Member, Administrator, Employee Posts: 538 admin
    edited October 2020 #5

    Hey @ludobar! I wrote a bolg post a while back on how to use Python MQTT to establish a connection with a Solace Broker , you will find it interesting you can check it out here https://dev.to/solacedevs/how-i-used-covid-tracking-data-to-build-and-learn-a-python-event-driven-application-1l44. In a nutshell, I used the paho.mqtt.client as follows

    import paho.mqtt.client as mqtt
    import json
    
    def on_connect(client, data, flags, rc):
        assert (rc == 0), "Error Connecting. Return code: " + str(rc)
        print("Connected to PubSub+ Solace Broker!")
    
        for t in topics:
            print("Subscribing to: " + t)
            client.subscribe(t)
    
    def on_message(client, data, msg):
        msg_json_object = json.loads(msg.payload.decode('ascii'))
        msg_json_pretty = json.dumps(msg_json_object, indent=2)
        print("Received message on: %s\n %s" % (msg.topic, msg_json_pretty))
    
    # Broker Info
    url = <host_name>
    username = <username>
    password = <password>
    
    topics = [
        "topic/v1/sample",
        "topic/v2/sample/#",
    ]
    
    client = mqtt.Client()
    client.username_pw_set(username=username, password=password)
    
    client.on_connect = on_connect
    client.on_message = on_message
    
    client.connect(url)
    client.loop_forever()
    
    

    You can also check this thread https://solace.community/discussion/comment/973#Comment_969
    Also, are you aware of the Solace Python API? It is still in beta early access, let me know if you're interested to be invited to a group and try it out!

  • ludobar
    ludobar Member Posts: 10

    @Tamimi thank you for the code snippet and for the post. It really helps me speeding up at coding!
    Right now I'm focusing on MQTT, but I might be interested in exploring Solace APIs in the future. I will keep it in mind!

  • Tamimi
    Tamimi Member, Administrator, Employee Posts: 538 admin

    Hey @ludobar ! I just came across this again and wanted to follow up, do you still have any issues with TLS connection with the broker?

  • ludobar
    ludobar Member Posts: 10

    Hey @Tamimi ! At the end I figured everything out. I shared my program on the community in this post https://solace.community/discussion/461/python-program-connecting-over-tls-ssl-to-pubsub-cloud#latest, in the hope of helping anyone struggling with it :)