Skip to content

Commit 9882993

Browse files
committed
Allow for subscribing to multiple topics in nmqtt_sub
1 parent 374f65b commit 9882993

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

src/nmqtt_sub.nim

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import cligen
2+
23
from os import getCurrentProcessId
4+
from strutils import split
35

46
include "nmqtt.nim"
57

@@ -42,16 +44,20 @@ proc nmqttSub(host="127.0.0.1", port=1883, ssl=false, clientid="", username="",
4244
# Connec to broker
4345
await ctx.start()
4446

45-
# Remove retained messages
46-
if removeretained:
47-
waitFor ctx.publish(topic, "", 0, true)
47+
# Loop through topics
48+
for t in topic.split(","):
49+
# Remove retained messages
50+
if removeretained:
51+
waitFor ctx.publish(t, "", 0, true)
4852

49-
# Callback for subscribe
50-
proc on_data(topic, msg: string) =
51-
echo topic, ": ", msg
53+
# Callback for subscribe
54+
proc on_data(t, msg: string) =
55+
echo t, ": ", msg
5256

53-
# Subscribe to topic
54-
await ctx.subscribe(topic, qos, on_data)
57+
# Subscribe to topic
58+
await ctx.subscribe(t, qos, on_data)
59+
if ctx.verbosity >= 1:
60+
ctx.dbg "Subscribing to: " & t
5561

5662
# Control CTRL+c hook
5763
setControlCHook(handler)

0 commit comments

Comments
 (0)