Skip to main content

SwampClient

SwampClient is an MQTT client library built as a wrapper around the Paho MQTT library. It is configureable using environment variables and integrates timestamp handling through the Zeitgleich library. SwampClient supports managing multiple MQTT clients simultaneously and provides functionalities for message transformation and timestamp management within MQTT-based applications.

Table of Contents

Features

  • Environment Variable Configuration: Configure multiple MQTT clients using prefixed environment variables.
  • JSONata Transformations: Apply JSONata expressions to transform MQTT message payloads.
  • Timestamp Handling: Integrate with Zeitgleich for timestamp parsing, conversion, and validation.
  • Multiple Transport Protocols: Supports MQTT over TCP, TLS, WebSockets, and Secure WebSockets.
  • Subscription Management: Subscribe to multiple topics with configurable QoS levels.
  • Asynchronous Operations: Utilize asyncio for non-blocking MQTT operations and concurrent tasks.
  • Logging: Configurable log levels for monitoring and debugging.
  • Graceful Shutdown: Ensure proper disconnection from MQTT brokers on application exit.

Installation

Install SwampClient using pip:

pip install SwampClient

Ensure you also have the required dependencies installed:

pip install -r requirements.txt

Configuration

SwampClient is configured through environment variables, allowing for easy management of multiple MQTT clients by using unique prefixes.

Environment Variables

Note: All environment variable names are prefixed with the client name (e.g., MQTTCLIENT1_) to support multiple clients simultaneously.

Environment Variable Description Default Value Tested
[PREFIX]_URL MQTT Broker URL. Connection string, including the protocol. Supported schemes: mqtt, mqtts, ws, wss. mqtt://localhost:1883 ✅
[PREFIX]_USERNAME MQTT Username. Username for MQTT broker authentication. None ❌
[PREFIX]_PASSWORD MQTT Password. Password for MQTT broker authentication. None ❌
[PREFIX]_TOPICS MQTT Topics. Comma-separated list of topics to subscribe to, each followed by :qos. Example: topic1:0,topic2:1. Empty String ✅
[PREFIX]_JSONATA JSONata Expression Path. Path to a JSONata expression file for transforming MQTT message payloads. Empty String ✅
[PREFIX]_TOPIC2PAYLOAD Topic to Payload Key. Key name to inject the topic into the payload. Empty String ❌
[PREFIX]_LOG_LEVEL Log Level. Sets the logging level for the client. Options: DEBUG, INFO, WARNING, ERROR, CRITICAL. DEBUG ✅
[PREFIX]_RAW_DATA_HANDLING Raw Data Handling. Defines how to handle non-JSON payloads. Options: wrap, pass_through, timeseries. wrap ❌
[PREFIX]_RAW_DATA_KEY Raw Data Key. Key name used when wrapping raw data. Applicable if RAW_DATA_HANDLING is set to wrap. raw_data ✅
[PREFIX]_SPLIT_PAYLOAD Split Payload Expression. JSONata expression to split payloads into multiple messages. None ❌
[PREFIX]_TIMESTAMP Timestamp Key. Key name in the payload that contains the timestamp. timestamp ✅
[PREFIX]_TIMESTAMP_FROM Input Timestamp Format. Format of the incoming timestamp. Options: RFC3339, UNIX, ISO, EPOCH_S, EPOCH_NS. EPOCH_S ✅
[PREFIX]_TIMESTAMP_TO Output Timestamp Format. Desired format for the timestamp in outgoing messages. Options: RFC3339, UNIX, ISO, EPOCH_S, EPOCH_NS. RFC3339 ✅

Example .env Configuration

# Example for MQTTCLIENT1
MQTTCLIENT1_URL="mqtt://localhost:1884"
MQTTCLIENT1_LOG_LEVEL="DEBUG"

# Example for MQTTCLIENT2
MQTTCLIENT2_URL="wss://broker.example.com:443"
MQTTCLIENT2_LOG_LEVEL="INFO"

Usage and Examples

SwampClient provides interfaces to interact with MQTT brokers. Below are examples demonstrating different use cases, including initializing clients, subscribing to topics, handling incoming messages, and publishing messages.

Basic Example

The Basic Example demonstrates initializing a single MQTT client, subscribing to topics, and handling incoming messages.

Running the Basic Example

  1. Set Up Environment Variables:

    Ensure your .env file includes the necessary configurations for MQTTCLIENT1.

  2. Run the Example:

    python swamp_mqtt_client/examples/basic.py
    

    Functionality:

    • Connects to the specified MQTT broker.
    • Subscribes to test/topic with QoS 0 and test/topic2 with QoS 1.
    • Logs received messages on subscribed topics.
    • Handles graceful shutdown on receiving KeyboardInterrupt.

Bridge Example

The Bridge Example shows how to bridge messages between two MQTT brokers. It subscribes to topics on a source broker and republishes messages to a target broker.

Running the Bridge Example

  1. Set Up Environment Variables:

    Ensure your .env file includes the necessary configurations for both SUBSCRIBE_CLIENT and PUBLISH_CLIENT.

  2. Run the Example:

    python swamp_mqtt_client/examples/bridge/bridge.py
    

    Functionality:

    • Subscribes to source/topic with QoS 0 and source/topic2 with QoS 1 on the source broker.
    • Transforms incoming messages using the specified JSONata expression.
    • Republishes the transformed messages to target/topic on the target broker.
    • Handles graceful shutdown on receiving termination signals (SIGINT, SIGTERM).

Publish Example

The Publish Example illustrates how to publish messages based on keyboard input to an MQTT topic.

Running the Publish Example

  1. Set Up Environment Variables:

    Ensure your .env file includes the necessary configurations for MQTTCLIENT1.

  2. Run the Example:

    python swamp_mqtt_client/examples/publish/publish.py
    

    Functionality:

    • Connects to the specified MQTT broker.
    • Listens for keyboard inputs and publishes each key press to the keyboard/keys topic.
    • Stops publishing and disconnects gracefully when the user types exit or interrupts the program.

Data Formats

SwampClient uses the Zeitgleich library for timestamp handling. For detailed information on timestamp normalization and conversion, refer to the Zeitgleich documentation.

TODOs

  • Unit Tests:

    • Implement unit tests for SwampClient functionalities.
    • Test support for mqtts, ws, wss schemes.
    • Verify MQTT broker authentication with username and password.
    • Test topic2payload feature.
    • Validate pass_through raw data handling.
    • Test split payload functionality.
  • Configuration Enhancements:

    • Add configuration options for Bridge mode.
    • Implement configuration for retaining messages.
    • Make configuration options dependent on each other (e.g., split_payload requires jsonata_expression).
  • Exception Handling:

    • Wrap certain exceptions in custom exception classes.
    • Improve error handling for connection and publishing failures.
  • Documentation:

    • Expand documentation for advanced configurations.
    • Provide more examples covering additional features.
  • Code-Level TODOs:

    • Support configuration of all subscription options, not just QoS.
    • Use ts.Origin for origin handling.
    • Test topic2payload integration.
    • Handle additional payload cases in message processing.
    • Wrap disconnect errors in custom exceptions and handle logging appropriately.
  • client.py

    • line 142 publishes all payloads with retain = false by default.
    • Missing env var to configure required behavior

License

Licensed under the MIT License.

Release files for SwampClient 0.1.3

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

Source distribution (sdist)

Source distribution for SwampClient 0.1.3
File Size Uploaded
swampclient-0.1.3.tar.gz 12.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for SwampClient 0.1.3
File Interpreter ABI Platform
swampclient-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 25.1 kB

Release files / swampclient-0.1.3.tar.gz

Download URL swampclient-0.1.3.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c7cfd27220abdbf4f0c785bfe31b8047763a1d3ee96541d6a4aff18c3634723c
BLAKE2b-256 checksum
How to use checksums
14a24125feea88416e5ebaa69ca9f6e5c35ba496d71adc6df7b25d7a3551134f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Release files / swampclient-0.1.3-py3-none-any.whl

Download URL swampclient-0.1.3-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7c5d40e427b4d3012e5549f5dd2a93f112adbbf4885b45f9bd975ab470e4576d
BLAKE2b-256 checksum
How to use checksums
a0cfdbc83ee1c9a27efa6ee48cfa0f220f28604d19483a4232eedc78d18b6b26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

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