Skip to content

Commit f1a65bc

Browse files
committed
umqtt.simple: Add standalone sub example.
1 parent df60404 commit f1a65bc

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

umqtt.simple/example_sub.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import time
2+
from umqtt.simple import MQTTClient
3+
4+
# Publish test messages e.g. with:
5+
# mosquitto_pub -t foo_topic -m hello
6+
7+
# Received messages from subscriptions will be delivered to this callback
8+
def sub_cb(topic, msg):
9+
print((topic, msg))
10+
11+
def main(server="localhost"):
12+
c = MQTTClient("umqtt_client", server)
13+
c.set_callback(sub_cb)
14+
c.connect()
15+
c.subscribe(b"foo_topic")
16+
while True:
17+
if True:
18+
# Blocking wait for message
19+
c.wait_msg()
20+
else:
21+
# Non-blocking wait for message
22+
c.check_msg()
23+
# Then need to sleep to avoid 100% CPU usage (in a real
24+
# app other useful actions would be performed instead)
25+
time.sleep(1)
26+
27+
c.disconnect()
28+
29+
if __name__ == "__main__":
30+
main()

0 commit comments

Comments
 (0)