Skip to main content

Asynchronous MQTT client for 3.1.1 protocol version.

Project description

About

Asynchronous MQTT client for 3.1.1 protocol version.

Installation

Recommended way (via pip):

$ pip install aio-mqtt

Example

Simple echo client:

import asyncio as aio

from aio_mqtt import (
    QOSLevel,
    PublishableMessage,
    MQTTClient,
    MQTTClientError,
    MQTTClientChannelClosedError
)


async def handle_disconnects(client: MQTTClient) -> None:
    while True:
        try:
            await client.wait_disconnecting()
        except MQTTClientError:
            # Handle unusual disconnects here
            return

        else:
            # Gracefully disconnected
            break


async def main(client: MQTTClient) -> None:
    try:
        await client.connect('localhost')
    except MQTTClientError:
        # Handle connection errors here
        return

    try:
        await client.subscribe(('in', QOSLevel.QOS_1))
    except MQTTClientError:
        # Handle subscription errors here
        return

    while True:
        try:
            message = await client.receive_message()
        except MQTTClientChannelClosedError:
            return

        try:
            await client.publish(PublishableMessage(topic_name='out', payload=message.payload, qos=QOSLevel.QOS_1))
        except MQTTClientError:
            return


if __name__ == '__main__':
    loop = aio.new_event_loop()
    client = MQTTClient()
    try:
        loop.run_until_complete(aio.gather(handle_disconnects(client), main(client)))
    except KeyboardInterrupt:
        pass

    finally:
        loop.run_until_complete(client.disconnect())
        loop.close()

License

Copyright 2019 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.

Project details


Release history Release notifications

This version

0.1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for aio-mqtt, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size aio_mqtt-0.1.0-py3-none-any.whl (13.5 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size aio-mqtt-0.1.0.tar.gz (10.0 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page