'Python paho-mqtt over websockt disconnects as soon as any data is send

I have a Python script that connects to a WSS MQTT Endpoint like so:

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    if str(rc) == "0":
        print("  Connection successful")
        client.subscribe("myqueue")

client = mqtt.Client(transport="websockets")
client.ws_set_options(path="/my/path", headers={"Authorization": "Bearer ..."})
client.on_connect = on_connect
client.tls_set()
client.connect("my_endpoint.com",443)

But as soon as the client.subscribe is called (or a publish) the socket closes and it disconnects with code 7 (which is MQTT Error for Unexpected Disconnect).

Has anyone ever seen this or an idea how this could be circumvented? If I never call subscribe or publish the connection stays open.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source