Skip to content

Test MQTT with Docker using the Paho Python client. Simple setup for quick experimentation. 🐳🐍

License

Notifications You must be signed in to change notification settings

Huendeberg/mqtt-docker-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MQTT Docker Python: Test MQTT with Docker and Paho Client πŸš€

License Python Docker

Table of Contents

Overview

This repository, mqtt-docker-python, provides a simple way to test MQTT using Docker and the Paho Python client. MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol ideal for IoT applications. This setup allows you to quickly spin up an MQTT broker and client environment using Docker containers.

Getting Started

To get started, you need to have Docker installed on your machine. If you don't have Docker, follow the official installation guide.

Prerequisites

  • Docker (version 20.10 or higher)
  • Python (version 3.8 or higher)

Installation

  1. Clone the repository:

    git clone https://github.com/Huendeberg/mqtt-docker-python.git
    cd mqtt-docker-python
  2. Build the Docker image:

    docker build -t mqtt-docker-python .
  3. Run the Docker container:

    docker run -d -p 1883:1883 mqtt-docker-python

Usage

To use the Paho MQTT client, you can run the Python script provided in this repository. The script connects to the MQTT broker running in the Docker container and subscribes to a topic.

Example Script

Here is a basic example of how to publish and subscribe to a topic:

import paho.mqtt.client as mqtt

# Callback when the client receives a message
def on_message(client, userdata, msg):
    print(f"Received message: {msg.payload.decode()} on topic: {msg.topic}")

# Create an MQTT client instance
client = mqtt.Client()

# Assign the on_message callback function
client.on_message = on_message

# Connect to the MQTT broker
client.connect("localhost", 1883, 60)

# Subscribe to a topic
client.subscribe("test/topic")

# Start the loop
client.loop_start()

# Publish a message
client.publish("test/topic", "Hello MQTT!")

# Keep the script running
input("Press Enter to exit...\n")
client.loop_stop()
client.disconnect()

Running the Script

  1. Ensure your Docker container is running.
  2. Execute the script:
    python your_script.py

Features

  • Lightweight: Easily deploy MQTT broker and client with Docker.
  • Flexible: Use Paho Python client for various MQTT operations.
  • Scalable: Supports multiple clients connecting to the broker.
  • Testing: Ideal for testing MQTT functionalities in a controlled environment.

Topics

This repository covers various topics related to MQTT:

  • mqtt
  • mqtt-broker
  • mqtt-client
  • mqtt-connector
  • mqtt-docker
  • mqtt-server
  • mqttx
  • paho-mqtt
  • paho-mqtt-apis
  • paho-mqtt-client
  • paho-mqtt-python
  • state-machine

Contributing

Contributions are welcome! If you want to improve this project, please follow these steps:

  1. Fork the repository.
  2. Create a new branch:
    git checkout -b feature/YourFeature
  3. Make your changes and commit them:
    git commit -m "Add some feature"
  4. Push to the branch:
    git push origin feature/YourFeature
  5. Open a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Releases

To download the latest release, visit the Releases section. Here, you can find the necessary files to download and execute.

For more information about the releases, check the Releases section.

MQTT

Acknowledgments

  • Thanks to the Paho MQTT team for providing a robust client library.
  • Docker for making containerization easy and efficient.
  • The open-source community for continuous support and contributions.

Contact

For any questions or feedback, feel free to open an issue in the repository or reach out directly via GitHub.


Feel free to explore the repository and contribute to the project. Happy coding!