Skip to content

Connect nicegui with MQTT #304

Closed Answered by Lxnus
Lxnus asked this question in Q&A
Discussion options

You must be logged in to vote

Here is one very dirty and hacky solution i found:

async def connect_mqtt():
    mqtt_handler = MQTTHandler(
        hostname="127.0.0.1",
        port=1883,
        client_id="demo_id",
        message_handler=on_message,
    )
    mqtt_handler.connect()
    mqtt_handler.subscribe("demo")
    while mqtt_handler.is_connected:
        await asyncio.sleep(1)


def on_message(payload):
    print(payload["topic"])


app.on_startup(connect_mqtt)


ui.run(
    host="127.0.0.1",
    port=8080,
    title="ALPHOS",
)

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Lxnus
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #303 on January 27, 2023 14:39.