Skip to main content

FluxMQ for Python

Project description

FluxMQ Python Library

A flexible, protocol-agnostic messaging library for Python applications, providing a unified interface for different messaging protocols.

Overview

FluxMQ is a messaging abstraction layer that allows applications to use different messaging protocols (NATS, MQTT, etc.) through a consistent interface. This makes it easy to switch between messaging systems without changing application code.

Key features:

  • Protocol-agnostic messaging interface
  • Support for multiple transport implementations (NATS, MQTT)
  • Asynchronous and synchronous APIs
  • Request-reply pattern support
  • Standardized topic naming conventions
  • Consistent status reporting

Installation

pip install fluxmq

Or install from source:

git clone https://github.com/yourusername/fluxmq-py.git
cd fluxmq-py
pip install -e .

Dependencies

  • Python 3.7+
  • For NATS support: nats-py
  • For MQTT support: paho-mqtt
  • For Zenoh support: zenoh (install with pip install fluxmq[zenoh])

Quick Start

Basic Usage with NATS

import asyncio
from fluxmq.adapter.nats import Nats, NatsTopic, NatsStatus
from fluxmq.message import Message

async def main():
    # Initialize transport, topic, and status
    transport = Nats(servers=["nats://localhost:4222"])
    topic = NatsTopic()
    status = NatsStatus()

    # Connect to the messaging system
    await transport.connect()

    # Subscribe to a topic
    async def message_handler(message: Message):
        print(f"Received message: {message.get_data_as_string()}")

    await transport.subscribe("example.topic", message_handler)

    # Publish a message
    await transport.publish("example.topic", "Hello, world!")

    # Request-reply pattern
    response = await transport.request("example.service", {"action": "get_data"})
    print(f"Response: {response.get_data_as_string()}")

    # Clean up
    await transport.close()

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

Using MQTT

import asyncio
from fluxmq.adapter.mqtt import MQTT, MQTTTopic, MQTTStatus
from fluxmq.message import Message

async def main():
    # Initialize transport, topic, and status
    transport = MQTT(host="localhost", port=1883)
    topic = MQTTTopic()
    status = MQTTStatus()

    # Connect to the messaging system
    await transport.connect()

    # Subscribe to a topic
    async def message_handler(message: Message):
        print(f"Received message: {message.get_data_as_string()}")

    await transport.subscribe("example/topic", message_handler)

    # Publish a message
    await transport.publish("example/topic", "Hello, world!")

    # Clean up
    await transport.close()

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

Using Zenoh

import asyncio
from fluxmq.adapter.zenoh import Zenoh, ZenohTopic, ZenohStatus
from fluxmq.message import Message

async def main():
    # Initialize transport, topic, and status
    transport = Zenoh()  # Default configuration connects to local Zenoh router
    topic = ZenohTopic()
    status = ZenohStatus()

    # Connect to the messaging system
    await transport.connect()

    # Subscribe to a topic
    async def message_handler(message: Message):
        print(f"Received message: {message.get_data_as_string()}")

    await transport.subscribe("example/topic", message_handler)

    # Publish a message
    await transport.publish("example/topic", "Hello, world!")

    # Request-reply pattern
    response = await transport.request("example/service", {"action": "get_data"})
    print(f"Response: {response.get_data_as_string()}")

    # Clean up
    await transport.close()

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

Architecture

FluxMQ consists of several key components:

Transport

The Transport interface defines the core messaging operations:

  • connect(): Connect to the messaging system
  • close(): Close the connection
  • publish(): Publish a message to a topic
  • subscribe(): Subscribe to a topic with a message handler
  • unsubscribe(): Unsubscribe from a topic
  • request(): Send a request and wait for a response
  • respond(): Respond to a request message

Implementations:

  • Nats: NATS implementation
  • MQTT: MQTT implementation
  • Zenoh: Zenoh implementation

Message

The Message class represents a message in the system:

  • data: The message payload
  • reply: Optional reply subject for request-reply patterns
  • headers: Optional headers associated with the message

Topic

The Topic interface provides standardized topic naming conventions:

  • status(): Topic for service status updates
  • configuration(): Topic for service configuration
  • error(): Topic for error messages
  • And many more...

Implementations:

  • StandardTopic: Base implementation with configurable prefix
  • NatsTopic: NATS-specific implementation
  • MQTTTopic: MQTT-specific implementation
  • ZenohTopic: Zenoh-specific implementation

Status

The Status interface provides standardized status values:

  • connected(): Connected status
  • ready(): Ready status
  • active(): Active status
  • paused(): Paused status
  • error(): Error status

Implementations:

  • StandardStatus: Base implementation
  • NatsStatus: NATS-specific implementation
  • MQTTStatus: MQTT-specific implementation
  • ZenohStatus: Zenoh-specific implementation

Advanced Usage

Synchronous API

For applications that can't use asyncio, FluxMQ provides a synchronous API:

from fluxmq.adapter.nats import SyncNats
from fluxmq.message import Message

def message_handler(message: Message):
    print(f"Received message: {message.get_data_as_string()}")

# Initialize and connect
transport = SyncNats(servers=["nats://localhost:4222"])
transport.connect()

# Subscribe to a topic
transport.subscribe("example.topic", message_handler)

# Publish a message
transport.publish("example.topic", "Hello, world!")

# Clean up
transport.close()

Custom Transport Implementation

You can implement your own transport by extending the Transport interface:

from fluxmq.transport import Transport
from fluxmq.message import Message
from typing import Callable, Awaitable, Any, Union

class CustomTransport(Transport):
    async def connect(self) -> None:
        # Implementation...

    async def close(self) -> None:
        # Implementation...

    async def publish(self, topic: str, payload: Union[bytes, str]) -> None:
        # Implementation...

    async def subscribe(self, topic: str, handler: Callable[[Message], Awaitable[None]]) -> Any:
        # Implementation...

    async def unsubscribe(self, topic: str) -> None:
        # Implementation...

    async def request(self, topic: str, payload: Union[bytes, str]) -> Message:
        # Implementation...

    async def respond(self, message: Message, response: Union[bytes, str]) -> None:
        # Implementation...

Testing

FluxMQ includes a comprehensive test suite to ensure reliability and correctness. To run the tests:

  1. Install the development dependencies:
pip install -e ".[dev]"
# or
pip install -r requirements-dev.txt
  1. Run the tests using pytest:
pytest

For more verbose output:

pytest -v

To run only specific tests:

# Run tests for a specific module
pytest tests/test_message.py

# Run tests for a specific class
pytest tests/test_transport.py::TestTransport

# Run a specific test
pytest tests/test_message.py::test_message_init

To run tests with coverage reporting:

coverage run -m pytest
coverage report
coverage html  # Generates an HTML report in htmlcov/

License

MIT License

Project details


Download files

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

Source Distribution

fluxmq-0.2.0.tar.gz (27.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fluxmq-0.2.0-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file fluxmq-0.2.0.tar.gz.

File metadata

  • Download URL: fluxmq-0.2.0.tar.gz
  • Upload date:
  • Size: 27.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for fluxmq-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1c19651db2414931905dcd83056d70bbb90de38a3d20a3a37e8ce5b19f4293e0
MD5 a547baa19970e4215e8de9fc1989d7f2
BLAKE2b-256 ffc2d3c898e857fbab1bea74bad84fd53a35c9a42c7753268c2f7b7bd4887b0a

See more details on using hashes here.

File details

Details for the file fluxmq-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: fluxmq-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for fluxmq-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1c996550dc3bfeb642cd95cc8d8d5bbb51bf683dbad0039c643f6eac6d857f1c
MD5 4a2f2facac2c7d7bb54324dcf294a802
BLAKE2b-256 94541636d6e3d0bbb24fd2f0022c9e23071fd351ad73a1dff9af98031709f24f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page