-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
on_message.py
35 lines (22 loc) · 870 Bytes
/
on_message.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import asyncio
import mqttools
class Client(mqttools.Client):
async def on_message(self, message):
await self._messages.put((message.topic,
message.message,
message.retain,
message.response_topic))
async def subscriber():
client = Client('localhost', 1883, subscriptions=['/test/mqttools/foo'])
await client.start()
print('Waiting for messages.')
while True:
topic, message, retain, response_topic = await client.messages.get()
if topic is None:
print('Broker connection lost!')
break
print(f'Topic: {topic}')
print(f'Message: {message}')
print(f'Retain: {retain}')
print(f'ResponseTopic: {response_topic}')
asyncio.run(subscriber())