https://github.com/misaghmomenib/mqtt-messaging-python
In This Repository, I Wrote a Simple Messaging System Using Mqtt That You Can Easily Send and Receive Messages.
https://github.com/misaghmomenib/mqtt-messaging-python
git mqtt mqtt-broker mqtt-server open-source python
Last synced: 2 months ago
JSON representation
In This Repository, I Wrote a Simple Messaging System Using Mqtt That You Can Easily Send and Receive Messages.
- Host: GitHub
- URL: https://github.com/misaghmomenib/mqtt-messaging-python
- Owner: MisaghMomeniB
- Created: 2025-03-09T15:09:36.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-09T15:53:48.000Z (over 1 year ago)
- Last Synced: 2025-03-09T16:30:51.880Z (over 1 year ago)
- Topics: git, mqtt, mqtt-broker, mqtt-server, open-source, python
- Language: Python
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# π‘ MQTT Messaging (Python)
A lightweight and configurable **MQTT pub/sub messaging tool** in Python. Built using the popular **Paho MQTT client**, itβs designed for rapid prototyping of IoT messaging, automation workflows, or testing pub/sub systems.
---
## π Table of Contents
1. [Overview](#overview)
2. [Features](#features)
3. [Prerequisites](#prerequisites)
4. [Installation](#installation)
5. [Usage Examples](#usage-examples)
6. [Code Structure](#code-structure)
7. [Security & Tips](#security--tips)
8. [Contributing](#contributing)
9. [License](#license)
---
## π‘ Overview
This project wraps basic MQTT functionalityβ**connecting**, **publishing**, and **subscribing**βinto a reusable Python tool. Leveraging the **Paho MQTT client**, it supports features essential for IoT messaging such as QoS control, TLS settings, and auto-reconnect logic :contentReference[oaicite:1]{index=1}.
---
## β
Features
- π Connect to MQTT brokers using TCP or WebSocket
- π’ Publish messages with customizable **QoS** and optional retain flag
- π© Subscribe to topics with callback handling
- π Auto-reconnect for improved reliability
- π TLS/SSL support & optional username/password authentication :contentReference[oaicite:2]{index=2}
- π Simple CLI interface or importable module
---
## π οΈ Prerequisites
- Python **3.7+**
- MQTT broker access (e.g., Mosquitto, EMQX, HiveMQ, or cloud broker)
- Required package:
```bash
pip install paho-mqtt
````
---
## βοΈ Installation
```bash
git clone https://github.com/MisaghMomeniB/MQTT-Messaging-Python.git
cd MQTT-Messaging-Python
pip install -r requirements.txt # or 'pip install paho-mqtt'
```
---
## π Usage Examples
### π Connect & Subscribe
```python
from mqtt_messaging import MQTTPublisher, MQTTSubscriber
# Subscribe example
sub = MQTTSubscriber(broker='broker.emqx.io', topic='my/topic', port=1883)
sub.connect()
sub.subscribe()
sub.loop_forever() # listens indefinitely
```
### π€ Publish Messages
```python
pub = MQTTPublisher(broker='broker.emqx.io', port=1883, qos=1, retain=True)
pub.connect()
pub.publish('my/topic', 'Hello MQTT! π')
pub.disconnect()
```
### π¦ CLI Example
```bash
python mqtt_tool.py --mode pub --topic my/topic --message "testing 123" --qos 1
python mqtt_tool.py --mode sub --topic my/topic --broker demo.mosquitto.org
```
---
## π Code Structure
```
MQTT-Messaging-Python/
βββ mqtt_tool.py # CLI wrapper
βββ mqtt_messaging.py # Core publish & subscribe classes
βββ requirements.txt # Dependencies
βββ README.md # This file
```
* `MQTTPublisher`: MQTT client for publishing
* `MQTTSubscriber`: MQTT client for subscribing with callback support
* CLI script parses basic flags and invokes classes
---
## π Security & Tips
* βοΈ Use `client.tls_set(...)` and `username_pw_set()` for secure, authenticated connections ([github.com][1], [emqx.com][2], [hivemq.com][3])
* Auto-reconnect ensures resilience in unstable networks ([emqx.com][2])
* Set **QoS** appropriately (`0`, `1`, `2`) for guaranteed delivery or low latency
* Consider using Last Will & Testament (LWT) for failure messaging in IoT deployments
---
## π€ Contributing
Enhancements are welcome! Suggestions include:
* π Support for batch publishing or message queues
* π Add message logging or replay functionality
* π Support for MQTT v5 features: message expiry, shared subscriptions
* π§ͺ Add unit tests with pytest/mock
* π¦ Package as a pip-installable library
To contribute:
1. Fork the repository
2. Create a branch (`feature/...`)
3. Implement tests and document changes
4. Open a Pull Request
---
## π License
Released under the **MIT License**. See `LICENSE` for details.