Try PubSub+

Connecting with MQTT without WebSockets

ludobarludobar Member Posts: 7

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

  • AlekseiAleksei Member Posts: 6

    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.

  • honghong Administrator Posts: 93 admin

    @Aleksei Thank you for the info!

  • ludobarludobar Member Posts: 7

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

  • TamimiTamimi Member, Administrator, Employee Posts: 65 admin
    edited October 8

    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!

  • ludobarludobar Member Posts: 7

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

Sign In or Register to comment.