Skip to main content

FastMQTT

FastMQTT is a high-performance, easy-to-use MQTT v5 client library for Python, built on top of asyncio. It provides a simple and intuitive API for working with MQTT, offering features like automatic reconnection, custom encoders/decoders, and full support for MQTT v5 capabilities.

Features

  • Full support for MQTT v5
  • Asynchronous API based on asyncio
  • Automatic reconnection
  • Flexible subscription management
  • Custom payload encoders and decoders
  • Request-response pattern support
  • Router-based topic handling

Installation

Install FastMQTT using pip:

pip install fastmqtt

Usage Guide

Basic Usage

Here's a simple example demonstrating how to use FastMQTT:

import asyncio
import logging
from fastmqtt import FastMQTT, Message

logging.basicConfig(level=logging.INFO)

fastmqtt = FastMQTT("test.mosquitto.org")

@fastmqtt.on_message("my/topic/1")
async def message_handler(message: Message):
    print(f"Message received: {message.payload.decode()} on topic {message.topic}")

async def main():
    async with fastmqtt:
        await fastmqtt.publish("my/topic/1", "Hello from FastMQTT!")
        await asyncio.sleep(5)

if __name__ == "__main__":
    asyncio.run(main())

Subscription Management

FastMQTT offers multiple ways to manage subscriptions:

  1. Using the @on_message decorator, can be used only before connecting:
@fastmqtt.on_message("my/topic")
async def handler(message: Message):
    ...
  1. Using the register method before connecting, can be used only before connecting:
fastmqtt.register(handler, "my/topic")
  1. Using the subscribe method after connecting, can be used always:
await fastmqtt.subscribe(handler, "my/topic")

Router-based Topic Handling

For better organization of your MQTT handlers, use the MQTTRouter:

from fastmqtt import MQTTRouter

router = MQTTRouter()

@router.on_message("my/topic")
async def handler(message: Message):
    ...

fastmqtt = FastMQTT("test.mosquitto.org", routers=[router])

Custom Encoders and Decoders

FastMQTT supports custom payload encoding and decoding:

from fastmqtt.encoders import JsonEncoder, JsonDecoder

fastmqtt = FastMQTT(
    "test.mosquitto.org",
    payload_encoder=JsonEncoder(),
    payload_decoder=JsonDecoder()
)

# Now you can publish and receive JSON payloads
await fastmqtt.publish("my/topic", {"key": "value"})

Request-Response Pattern

FastMQTT provides a convenient way to implement request-response patterns:

async with fastmqtt.response_context("response/topic") as ctx:
    response = await ctx.request("request/topic", "Hello")
    print(f"Response: {response.payload.decode()}")

MQTT v5 Features

FastMQTT fully supports MQTT v5 features. Here are some examples:

  1. Using MQTT v5 properties:
from fastmqtt.properties import PublishProperties

props = PublishProperties(
    content_type="application/json",
    user_property=[("key", "value")]
)
await fastmqtt.publish("my/topic", payload, properties=props)
  1. Handling retained messages:
from fastmqtt.types import RetainHandling

@fastmqtt.on_message("my/topic", retain_handling=RetainHandling.DO_NOT_SEND)
async def handler(message: Message):
    ...
  1. Working with shared subscriptions:
@fastmqtt.on_message("$share/group/my/topic")
async def shared_handler(message: Message):
    ...

Contributing

Contributions to FastMQTT are welcome! Please follow these steps to contribute:

  1. Fork the repository
  2. Create a new branch for your feature or bug fix
  3. Write your code and tests
  4. Run the example scripts to ensure everything passes
  5. Submit a pull request with a clear description of your changes

License

FastMQTT is released under the MIT License. See the LICENSE file for details.

Metadata

Release files for fastmqtt 0.2.1

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

Source distribution (sdist)

Source distribution for fastmqtt 0.2.1
File Size Uploaded Provenance Actions
fastmqtt-0.2.1.tar.gz 14.1 kB Without provenance Details for fastmqtt-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastmqtt 0.2.1
File Interpreter ABI Platform Actions
fastmqtt-0.2.1-py3-none-any.whl Python 3 none any Details for fastmqtt-0.2.1-py3-none-any.whl

Total release size: 32.8 kB

Release files / fastmqtt-0.2.1.tar.gz

Download URL fastmqtt-0.2.1.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
05634ed1fc61888b4ecab034452951ba918e26e9a106bb9fafb7b664de4dfe9f
BLAKE2b-256 checksum
How to use checksums
4aecad5775a4759a4ae0e9687ec0dd0f280c30f61bfad5747d047dad2ab2ba64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.5

Release files / fastmqtt-0.2.1-py3-none-any.whl

Download URL fastmqtt-0.2.1-py3-none-any.whl
Size 18.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d75eb263fa6ec5006f3542d12891b745523d77234bb89b4e7f62edd0fbe02785
BLAKE2b-256 checksum
How to use checksums
b55b817e0334ddaeeef1dcefc0f5c22588a9fd4bea7d5f28f0ef159b7e602f28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.5

Security for fastmqtt 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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