### Setting Up gmqtt Development Environment (Bash) Source: https://github.com/wialon/gmqtt/blob/master/DEVELOPMENT.md This snippet outlines the steps to set up a local development environment for gmqtt. It involves cloning the forked repository, navigating into it, creating and activating a Python virtual environment, and installing the project in editable mode. ```bash git clone git@github.com:[YOUR_GITHUB_USERNAME]/gmqtt.git cd gmqtt python3 -m venv . source bin/activate python3 setup.py develop ``` -------------------------------- ### Installing gmqtt Test Dependencies (Bash) Source: https://github.com/wialon/gmqtt/blob/master/DEVELOPMENT.md This command installs the additional Python dependencies required to run the unit tests for the gmqtt project. It uses pip to install the package in the current directory along with its specified 'test' extras. ```bash pip3 install .[test] ``` -------------------------------- ### Installing gmqtt Python Library Source: https://github.com/wialon/gmqtt/blob/master/README.md This snippet provides the command to install the gmqtt library using pip, the standard package installer for Python. It specifies `pip3` to ensure compatibility with Python 3 environments. ```bash pip3 install gmqtt ``` -------------------------------- ### Running gmqtt Unit Tests (Bash) Source: https://github.com/wialon/gmqtt/blob/master/DEVELOPMENT.md This command executes the unit tests for the gmqtt project using pytest-3. It targets the 'tests' directory, running all discovered test cases within it. ```bash pytest-3 tests ``` -------------------------------- ### Basic Asynchronous MQTT Client Usage in Python Source: https://github.com/wialon/gmqtt/blob/master/README.md This example demonstrates how to set up a basic asynchronous MQTT client using gmqtt. It includes connecting to a broker, defining callback functions for connection, message reception, disconnection, and subscription, and publishing a test message. It also shows how to gracefully handle program termination using signal handlers. ```python import asyncio import os import signal import time from gmqtt import Client as MQTTClient # gmqtt also compatibility with uvloop import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) STOP = asyncio.Event() def on_connect(client, flags, rc, properties): print('Connected') client.subscribe('TEST/#', qos=0) def on_message(client, topic, payload, qos, properties): print('RECV MSG:', payload) def on_disconnect(client, packet, exc=None): print('Disconnected') def on_subscribe(client, mid, qos, properties): print('SUBSCRIBED') def ask_exit(*args): STOP.set() async def main(broker_host, token): client = MQTTClient("client-id") client.on_connect = on_connect client.on_message = on_message client.on_disconnect = on_disconnect client.on_subscribe = on_subscribe client.set_auth_credentials(token, None) await client.connect(broker_host) client.publish('TEST/TIME', str(time.time()), qos=1) await STOP.wait() await client.disconnect() if __name__ == '__main__': loop = asyncio.get_event_loop() host = 'mqtt.flespi.io' token = os.environ.get('FLESPI_TOKEN') loop.add_signal_handler(signal.SIGINT, ask_exit) loop.add_signal_handler(signal.SIGTERM, ask_exit) loop.run_until_complete(main(host, token)) ``` -------------------------------- ### Publishing MQTT Messages with Response Topic Property in Python Source: https://github.com/wialon/gmqtt/blob/master/README.md This example illustrates how to publish a message with a custom MQTT 5.0 property, specifically `response_topic`. It demonstrates setting up a client, connecting, subscribing, and then publishing a message while including the `response_topic` for request/response patterns. ```python TOPIC = 'testtopic/TOPIC' def on_connect(client, flags, rc, properties): client.subscribe(TOPIC, qos=1) print('Connected') def on_message(client, topic, payload, qos, properties): print('RECV MSG:', topic, payload.decode(), properties) async def main(broker_host, token): client = MQTTClient('asdfghjk') client.on_message = on_message client.on_connect = on_connect client.set_auth_credentials(token, None) await client.connect(broker_host, 1883, keepalive=60) client.publish(TOPIC, 'Message payload', response_topic='RESPONSE/TOPIC') await STOP.wait() await client.disconnect() ``` -------------------------------- ### Setting Flespi.io Token Environment Variable (Bash) Source: https://github.com/wialon/gmqtt/blob/master/DEVELOPMENT.md This snippet shows how to export your flespi.io API token as an environment variable named 'USERNAME'. This token is required for the unit tests to authenticate with the flespi.io service. ```bash export USERNAME=YOUR_FLESPI_IO_TOKEN ``` -------------------------------- ### Adding Upstream Remote and Rebasing for gmqtt (Bash) Source: https://github.com/wialon/gmqtt/blob/master/DEVELOPMENT.md This snippet demonstrates how to add the original gmqtt repository as an 'upstream' remote and then rebase your local 'master' branch with the upstream's 'master' to keep your fork synchronized with ongoing development. ```bash git remote add upstream git@github.com:wialon/gmqtt.git git pull --rebase upstream master ``` -------------------------------- ### Configuring MQTT Connect Properties in Python Source: https://github.com/wialon/gmqtt/blob/master/README.md This snippet demonstrates how to configure various MQTT 5.0 connect properties when initializing the `gmqtt.Client` object. It shows examples of setting `receive_maximum`, `session_expiry_interval`, and `user_property` as keyword arguments during client instantiation. ```python client = gmqtt.Client("lenkaklient", receive_maximum=24000, session_expiry_interval=60, user_property=('myid', '12345')) ``` -------------------------------- ### Publishing Messages with Properties in gmqtt (Python) Source: https://github.com/wialon/gmqtt/blob/master/README.md This snippet demonstrates how to publish messages using the gmqtt client, including various MQTT v5 properties like `message_expiry_interval` and `content_type`. It also shows an `on_message` callback function that receives and prints these properties, illustrating how to access them when a message is received. ```python def on_message(client, topic, payload, qos, properties): # properties example here: {'content_type': ['json'], 'user_property': [('timestamp', '1524235334.881058')], 'message_expiry_interval': [60], 'subscription_identifier': [42, 64]} print('RECV MSG:', topic, payload, properties) client.publish('TEST/TIME', str(time.time()), qos=1, retain=True, message_expiry_interval=60, content_type='json') ``` -------------------------------- ### Forcing MQTT Protocol Version in Python Source: https://github.com/wialon/gmqtt/blob/master/README.md This snippet shows how to explicitly set the MQTT protocol version when connecting to a broker using gmqtt. It imports `MQTTv311` from `gmqtt.mqtt.constants` to force the client to use MQTT version 3.1.1 instead of the default 5.0. ```python from gmqtt.mqtt.constants import MQTTv311 client = MQTTClient('clientid') client.set_auth_credentials(token, None) await client.connect(broker_host, 1883, keepalive=60, version=MQTTv311) ``` -------------------------------- ### Defining Asynchronous on_message Callback in gmqtt (Python) Source: https://github.com/wialon/gmqtt/blob/master/README.md This snippet illustrates how to define an asynchronous `on_message` callback function for the gmqtt client. This callback must return a valid PUBACK code (e.g., `0` for success) to acknowledge message processing, allowing for non-blocking message handling. ```python async def on_message(client, topic, payload, qos, properties): pass return 0 ``` -------------------------------- ### Configuring Reconnect Behavior in gmqtt (Python) Source: https://github.com/wialon/gmqtt/blob/master/README.md This code snippet shows how to customize the automatic reconnect behavior of the gmqtt client. It sets the number of reconnect attempts to 10 and the delay between attempts to 60 seconds, overriding the default unlimited retries and 6-second delay. ```python client = MQTTClient("client-id") client.set_config({'reconnect_retries': 10, 'reconnect_delay': 60}) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.