All Projects → sbtinstruments → asyncio-mqtt

sbtinstruments / asyncio-mqtt

Licence: BSD-3-Clause License
Idomatic asyncio wrapper around paho-mqtt

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to asyncio-mqtt

mqttools
MQTT version 5.0 client and broker using asyncio
Stars: ✭ 44 (-67.88%)
Mutual labels:  mqtt, asyncio, mqtt-client
Hbmqtt
MQTT client/broker using Python asynchronous I/O
Stars: ✭ 667 (+386.86%)
Mutual labels:  mqtt, asyncio, mqtt-client
ESPecial
ESP32 automation with web interface and telegram bot
Stars: ✭ 77 (-43.8%)
Mutual labels:  mqtt, mqtt-client
Iot Harbor
reactor3实现的mqtt库
Stars: ✭ 234 (+70.8%)
Mutual labels:  mqtt, mqtt-client
Emqtt
Erlang MQTT v5.0 Client
Stars: ✭ 253 (+84.67%)
Mutual labels:  mqtt, mqtt-client
Broadlink Mqtt
MQTT client to control BroadLink devices
Stars: ✭ 169 (+23.36%)
Mutual labels:  mqtt, mqtt-client
Gmqtt
Python MQTT v5.0 async client
Stars: ✭ 195 (+42.34%)
Mutual labels:  mqtt, asyncio
WeConnect-mqtt
MQTT Client that publishes data from Volkswagen WeConnect
Stars: ✭ 14 (-89.78%)
Mutual labels:  mqtt, mqtt-client
Mqtt Client Framework
iOS, macOS, tvOS native ObjectiveC MQTT Client Framework
Stars: ✭ 1,722 (+1156.93%)
Mutual labels:  mqtt, mqtt-client
mqtt-datasource
MQTT Datasource for Grafana allows streaming data from any MQTT broker running either locally or remotely.
Stars: ✭ 99 (-27.74%)
Mutual labels:  mqtt, mqtt-client
CODESYS-MQTT
MQTT client library for CODESYS, supporting all QoS
Stars: ✭ 63 (-54.01%)
Mutual labels:  mqtt, mqtt-client
zmosq
MQTT/Mosquitto / ZeroMQ proxy
Stars: ✭ 22 (-83.94%)
Mutual labels:  mqtt, mqtt-client
Mqttnet
MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
Stars: ✭ 2,486 (+1714.6%)
Mutual labels:  mqtt, mqtt-client
Mqtt Pwn
MQTT-PWN intends to be a one-stop-shop for IoT Broker penetration-testing and security assessment operations.
Stars: ✭ 156 (+13.87%)
Mutual labels:  mqtt, mqtt-client
Mqttclient
A high-performance, high-stability, cross-platform MQTT client, developed based on the socket API, can be used on embedded devices (FreeRTOS / LiteOS / RT-Thread / TencentOS tiny), Linux, Windows, Mac, with a very concise The API interface realizes the quality of service of QOS2 with very few resources, and seamlessly connects the mbedtls encryption library.
Stars: ✭ 234 (+70.8%)
Mutual labels:  mqtt, mqtt-client
Mqtt
Asynchronous MQTT client for PHP based on workerman.
Stars: ✭ 142 (+3.65%)
Mutual labels:  mqtt, mqtt-client
Android Mqtt Service
A simple MQTT Service that will keep running for the duration of your Android application using the Paho Java MQTT Client.
Stars: ✭ 238 (+73.72%)
Mutual labels:  mqtt, mqtt-client
Coogleiot
A ESP8266 Library for easy IOT device development
Stars: ✭ 118 (-13.87%)
Mutual labels:  mqtt, mqtt-client
Mqttandroidclient
Android消息推送MQTT
Stars: ✭ 131 (-4.38%)
Mutual labels:  mqtt, mqtt-client
wyzesense2mqtt
Configurable WyzeSense to MQTT Gateway intended for use with Home Assistant or other platforms that use MQTT discovery mechanisms.
Stars: ✭ 55 (-59.85%)
Mutual labels:  mqtt, mqtt-client

license semver PyPI

MQTT client with idiomatic asyncio interface 🙌

Write code like this:

Subscriber
async with Client("test.mosquitto.org") as client:
    async with client.filtered_messages("floors/+/humidity") as messages:
        await client.subscribe("floors/#")
        async for message in messages:
            print(message.payload.decode())
Publisher
async with Client("test.mosquitto.org") as client:
    message = "10%"
    await client.publish(
            "floors/bed_room/humidity",
             payload=message.encode()
          )

asyncio-mqtt combines the stability of the time-proven paho-mqtt library with a modern, asyncio-based interface.

  • No more callbacks! 👍
  • No more return codes (welcome to the MqttError)
  • Graceful disconnection (forget about on_unsubscribe, on_disconnect, etc.)
  • Compatible with async code
  • Fully type-hinted
  • Did we mention no more callbacks?

The whole thing is less than 600 lines of code.

Installation 📚

pip install asyncio-mqtt

Advanced use

Let's make the example from before more interesting:

import asyncio
from contextlib import AsyncExitStack, asynccontextmanager
from random import randrange
from asyncio_mqtt import Client, MqttError


async def advanced_example():
    # We 💛 context managers. Let's create a stack to help
    # us manage them.
    async with AsyncExitStack() as stack:
        # Keep track of the asyncio tasks that we create, so that
        # we can cancel them on exit
        tasks = set()
        stack.push_async_callback(cancel_tasks, tasks)

        # Connect to the MQTT broker
        client = Client("test.mosquitto.org")
        await stack.enter_async_context(client)

        # You can create any number of topic filters
        topic_filters = (
            "floors/+/humidity",
            "floors/rooftop/#"
            # 👉 Try to add more filters!
        )
        for topic_filter in topic_filters:
            # Log all messages that matches the filter
            manager = client.filtered_messages(topic_filter)
            messages = await stack.enter_async_context(manager)
            template = f'[topic_filter="{topic_filter}"] {{}}'
            task = asyncio.create_task(log_messages(messages, template))
            tasks.add(task)

        # Messages that doesn't match a filter will get logged here
        messages = await stack.enter_async_context(client.unfiltered_messages())
        task = asyncio.create_task(log_messages(messages, "[unfiltered] {}"))
        tasks.add(task)

        # Subscribe to topic(s)
        # 🤔 Note that we subscribe *after* starting the message
        # loggers. Otherwise, we may miss retained messages.
        await client.subscribe("floors/#")

        # Publish a random value to each of these topics
        topics = (
            "floors/basement/humidity",
            "floors/rooftop/humidity",
            "floors/rooftop/illuminance",
            # 👉 Try to add more topics!
        )
        task = asyncio.create_task(post_to_topics(client, topics))
        tasks.add(task)

        # Wait for everything to complete (or fail due to, e.g., network
        # errors)
        await asyncio.gather(*tasks)

async def post_to_topics(client, topics):
    while True:
        for topic in topics:
            message = randrange(100)
            print(f'[topic="{topic}"] Publishing message={message}')
            await client.publish(topic, message, qos=1)
            await asyncio.sleep(2)

async def log_messages(messages, template):
    async for message in messages:
        # 🤔 Note that we assume that the message paylod is an
        # UTF8-encoded string (hence the `bytes.decode` call).
        print(template.format(message.payload.decode()))

async def cancel_tasks(tasks):
    for task in tasks:
        if task.done():
            continue
        try:
            task.cancel()
            await task
        except asyncio.CancelledError:
            pass

async def main():
    # Run the advanced_example indefinitely. Reconnect automatically
    # if the connection is lost.
    reconnect_interval = 3  # [seconds]
    while True:
        try:
            await advanced_example()
        except MqttError as error:
            print(f'Error "{error}". Reconnecting in {reconnect_interval} seconds.')
        finally:
            await asyncio.sleep(reconnect_interval)


asyncio.run(main())

Alternative asyncio-based MQTT clients

Is asyncio-mqtt not what you are looking for? Try another client:

  • hbmqtt - Own protocol implementation. Includes a broker.
    GitHub stars license
  • gmqtt - Own protocol implementation. No dependencies.
    GitHub stars license
  • aiomqtt - Wrapper around paho-mqtt.
    GitHub stars license
  • mqttools - Own protocol implementation. No dependencies.
    GitHub stars license
  • aio-mqtt - Own protocol implementation. No dependencies.
    GitHub stars license

This is not an exhaustive list.

Honorable mentions

Requirements

Python 3.7 or later.

There is only a single dependency:

Note for Windows Users

Since Python 3.8, the default asyncio event loop is the ProactorEventLoop. Said loop doesn't support the add_reader method that is required by asyncio-mqtt. To use asyncio-mqtt, please switch to an event loop that supports the add_reader method such as the built-in SelectorEventLoop. E.g:

# Change to the "Selector" event loop
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# Run your async application as usual
asyncio.run(main())

Changelog

Please refer to the CHANGELOG document. It adheres to the principles of Keep a Changelog.

Versioning

semver

This project adheres to Semantic Versioning.

Expect API changes until we reach version 1.0.0. After 1.0.0, breaking changes will only occur in major release (e.g., 2.0.0, 3.0.0, etc.).

License

license

Note that the underlying paho-mqtt library is dual-licensed. One of the licenses is the so-called Eclipse Distribution License v1.0. It is almost word-for-word identical to the BSD 3-clause License. The only differences are:

  • One use of "COPYRIGHT OWNER" (EDL) instead of "COPYRIGHT HOLDER" (BSD)
  • One use of "Eclipse Foundation, Inc." (EDL) instead of "copyright holder" (BSD)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].