Skip to main content

About

Asynchronous MQTT client for 3.1.1 protocol version (mod). Because of abandoned original repo this fork adds support for python >= 3.10

Installation

Recommended way (via pip):

$ pip install aio-mqtt

Example

Simple echo server:

import asyncio as aio
import logging
import typing as ty

import aio_mqtt

logger = logging.getLogger(__name__)


class EchoServer:

    def __init__(
            self,
            reconnection_interval: int = 10,
            loop: ty.Optional[aio.AbstractEventLoop] = None
    ) -> None:
        self._reconnection_interval = reconnection_interval
        self._loop = loop or aio.get_event_loop()
        self._client = aio_mqtt.Client(loop=self._loop)
        self._tasks = [
            self._loop.create_task(self._connect_forever()),
            self._loop.create_task(self._handle_messages())
        ]

    async def close(self) -> None:
        for task in self._tasks:
            if task.done():
                continue
            task.cancel()
            try:
                await task
            except aio.CancelledError:
                pass
        if self._client.is_connected():
            await self._client.disconnect()

    async def _handle_messages(self) -> None:
        async for message in self._client.delivered_messages('in'):
            while True:
                try:
                    await self._client.publish(
                        aio_mqtt.PublishableMessage(
                            topic_name='out',
                            payload=message.payload,
                            qos=aio_mqtt.QOSLevel.QOS_1
                        )
                    )
                except aio_mqtt.ConnectionClosedError as e:
                    logger.error("Connection closed", exc_info=e)
                    await self._client.wait_for_connect()
                    continue

                except Exception as e:
                    logger.error("Unhandled exception during echo message publishing", exc_info=e)

                break

    async def _connect_forever(self) -> None:
        while True:
            try:
                connect_result = await self._client.connect('localhost')
                logger.info("Connected")

                await self._client.subscribe(('in', aio_mqtt.QOSLevel.QOS_1))

                logger.info("Wait for network interruptions...")
                await connect_result.disconnect_reason
            except aio.CancelledError:
                raise

            except aio_mqtt.AccessRefusedError as e:
                logger.error("Access refused", exc_info=e)

            except aio_mqtt.ConnectionLostError as e:
                logger.error("Connection lost. Will retry in %d seconds", self._reconnection_interval, exc_info=e)
                await aio.sleep(self._reconnection_interval, loop=self._loop)

            except aio_mqtt.ConnectionCloseForcedError as e:
                logger.error("Connection close forced", exc_info=e)
                return

            except Exception as e:
                logger.error("Unhandled exception during connecting", exc_info=e)
                return

            else:
                logger.info("Disconnected")
                return


if __name__ == '__main__':
    logging.basicConfig(
        level='DEBUG'
    )
    loop = aio.new_event_loop()
    server = EchoServer(reconnection_interval=10, loop=loop)
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass

    finally:
        loop.run_until_complete(server.close())
        loop.run_until_complete(loop.shutdown_asyncgens())
        loop.close()

License

Copyright 2019-2020 Not Just A Toy Corp.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Release files for aio-mqtt-mod 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aio-mqtt-mod 0.4.0
File Size Uploaded
aio_mqtt_mod-0.4.0.tar.gz 15.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aio-mqtt-mod 0.4.0
File Interpreter ABI Platform
aio_mqtt_mod-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 34.2 kB

Release files / aio_mqtt_mod-0.4.0.tar.gz

Download URL aio_mqtt_mod-0.4.0.tar.gz
Size 15.8 kB
Tags Source
SHA-256 checksum
How to use checksums
14784b0deedc9e91ef0980046c03cecff685b6e4364b6f3a66d41bca4b98b58f
BLAKE2b-256 checksum
How to use checksums
0d0d90cacc333a0a51ea66acaeb498b47d0d55943ea940a2dd1acb49eca6127e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.9

Release files / aio_mqtt_mod-0.4.0-py3-none-any.whl

Download URL aio_mqtt_mod-0.4.0-py3-none-any.whl
Size 18.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e6438150f65c340c0053ab0a0cc4ad9a9f0156895a77d55398f2c90622c0d6d
BLAKE2b-256 checksum
How to use checksums
4c100c21e1f655e155665736b1448be3f327c65ed6b04b6e5f0176b111b1b69f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.9

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page