Paho MQTT in browser with jupyterlite

72 Views Asked by At

I'm attempting to run Paho MQTT in a browser using jupyterlite and pyodide. I was able to install paho using micropip (see example in link below). But the paho client times out when attempting to connect. I tried using websockets but this also failed with timeout. I tried ports 443, 9001 but that failed.

client = mqtt_client.Client(client_id, transport='websockets')

Broker is https://mqtt.eclipseprojects.io/

Here is the notebook example (see morse_decoder.ipynb) which you can run on a browser.

I import as follows:

import pyodide_kernel
import micropip
await micropip.install(['numpy', 'scikit-learn', 'https://www.piwheels.org/simple/paho-mqtt/paho_mqtt-1.6.1-py3-none-any.whl#sha256=c44b3dd1b298894c44e3e842f7b0ca3ebe01f628a6f229c881b2324613d7bba7'])
pyodide_kernel.__version__    

Client code:

from paho.mqtt import client as mqtt_client

# MQTT settings
client_id = f'publish-{random.randint(0, 1000)}'

def connect_mqtt(broker, port):
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("Connected to MQTT Broker!")
        else:
            print("Failed to connect, return code %d\n", rc)
    client = mqtt_client.Client(client_id, transport='websockets')
    client.on_connect = on_connect
    client.connect(broker, port)
    return client

Thanks for your assistance.

0

There are 0 best solutions below