An MQTT client library built as a wrapper around Paho MQTT, configurable via environment variables, supporting JSONata transformations and timestamp handling through Zeitgleich.
Project description
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
asynciofor 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. |
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
-
Set Up Environment Variables:
Ensure your
.envfile includes the necessary configurations forMQTTCLIENT1. -
Run the Example:
python swamp_mqtt_client/examples/basic.pyFunctionality:
- Connects to the specified MQTT broker.
- Subscribes to
test/topicwith QoS 0 andtest/topic2with 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
-
Set Up Environment Variables:
Ensure your
.envfile includes the necessary configurations for bothSUBSCRIBE_CLIENTandPUBLISH_CLIENT. -
Run the Example:
python swamp_mqtt_client/examples/bridge/bridge.pyFunctionality:
- Subscribes to
source/topicwith QoS 0 andsource/topic2with QoS 1 on the source broker. - Transforms incoming messages using the specified JSONata expression.
- Republishes the transformed messages to
target/topicon the target broker. - Handles graceful shutdown on receiving termination signals (
SIGINT,SIGTERM).
- Subscribes to
Publish Example
The Publish Example illustrates how to publish messages based on keyboard input to an MQTT topic.
Running the Publish Example
-
Set Up Environment Variables:
Ensure your
.envfile includes the necessary configurations forMQTTCLIENT1. -
Run the Example:
python swamp_mqtt_client/examples/publish/publish.pyFunctionality:
- Connects to the specified MQTT broker.
- Listens for keyboard inputs and publishes each key press to the
keyboard/keystopic. - Stops publishing and disconnects gracefully when the user types
exitor 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,wssschemes. - Verify MQTT broker authentication with username and password.
- Test
topic2payloadfeature. - Validate
pass_throughraw 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_payloadrequiresjsonata_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.Originfor origin handling. - Test
topic2payloadintegration. - Handle additional payload cases in message processing.
- Wrap disconnect errors in custom exceptions and handle logging appropriately.
License
Licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swampclient-0.0.6.tar.gz.
File metadata
- Download URL: swampclient-0.0.6.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06d58d0789ab2301e2719c9e54dd086d4d3781a28f234e0912dd7875244eaa1f
|
|
| MD5 |
62a6766fbb0e0b2a4e440be9c6fc2b2a
|
|
| BLAKE2b-256 |
7a8934468f70baf563c94699810d244b90cde15a72b91cae41276515b6a2be15
|
File details
Details for the file swampclient-0.0.6-py3-none-any.whl.
File metadata
- Download URL: swampclient-0.0.6-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fbf30d7f42f62be7560765dd29959a75a0872806a7e57679d331011811e72057
|
|
| MD5 |
3aaa037d24c6445d14e7dc416789de08
|
|
| BLAKE2b-256 |
95e9d55f5d0510b89c17a97aac0d264c181e10aa7aa9a997d4207ab5ad1fca7f
|