This is my custom aiomqtt client
Project description
DM-aiomqtt
Urls
Usage
Run in Windows
If you run async code in Windows, set correct selector
import asyncio
import sys
if sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
Listen
from dm_aiomqtt import DMAioMqttClient
import asyncio
# create handler for 'test' topic
async def test_handler(client: DMAioMqttClient, topic: str, payload: str) -> None:
print(f"Received message from {topic}: {payload}")
await client.publish("test/success", payload=True)
async def main():
# create client
mqtt_client = DMAioMqttClient("localhost", 1883, "username", "password")
# add handler for 'test' topic
mqtt_client.add_handler("test", test_handler)
# start listening (this operation is block async thread)
await mqtt_client.listen()
if __name__ == "__main__":
asyncio.run(main())
Publish
-
Connection only when publishing
from dm_aiomqtt import DMAioMqttClient import asyncio async def main(): # create client mqtt_client = DMAioMqttClient("localhost", 1883, "username", "password") # publish json message await mqtt_client.publish("test", payload={"key": "value"}) # other code # publish new json message await mqtt_client.publish("test2", payload={"key2": "value2"}) if __name__ == "__main__": asyncio.run(main())
-
One connection for all publishing
from dm_aiomqtt import DMAioMqttClient import asyncio async def main(mqtt_client: DMAioMqttClient): # publish json message await mqtt_client.publish("test", payload={"key": "value"}) # other code # publish new json message await mqtt_client.publish("test2", payload={"key2": "value2"}) async def start(): # create client mqtt_client = DMAioMqttClient("localhost", 1883, "username", "password") # create task for listening (this operation is block async thread) listen_task = asyncio.create_task(mqtt_client.listen(reconnect_interval=5)) # create task to execute other code with an active connection main_task = asyncio.create_task(main(mqtt_client)) # run async tasks in parallel await asyncio.gather(listen_task, main_task) if __name__ == "__main__": asyncio.run(start())
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
dm-aiomqtt-0.1.0.tar.gz
(3.4 kB
view hashes)
Built Distribution
Close
Hashes for dm_aiomqtt-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ec1b3470d852cb48f491549d023a8218b0d582e9ec872d7f25dfba64da34523 |
|
MD5 | ee92944246b780966d84b4a5bf9abe7b |
|
BLAKE2b-256 | 2f187f9e5b31140390418ea0b66b212c1b89b7b801359947e112325702e25bd6 |