Skip to content

Commit 522eda1

Browse files
committed
Update README. Specify let retain in onPublish for future usage
1 parent 5dee1eb commit 522eda1

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,27 @@ ____
254254
proc publish*(ctx: MqttCtx, topic: string, message: string, qos=0, retain=false) {.async.} =
255255
```
256256

257-
Publish a message
257+
Publish a message.
258+
259+
**Required:**
260+
- topic: string
261+
- message: string
262+
263+
**Optional:**
264+
- qos: int = 1, 2 or 3
265+
- retain: bool = true or false
266+
267+
**Publish message:**
268+
```nim
269+
ctx.publish(topic = "nmqtt", message = "Hey there", qos = 0, retain = true)
270+
```
271+
272+
**Remove retained message on topic:**
273+
274+
Set the `message` to _null_.
275+
```nim
276+
ctx.publish(topic = "nmqtt", message = "", qos = 0, retain = true)
277+
```
258278

259279

260280
____

src/nmqtt.nim

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,11 @@ proc onConnAck(ctx: MqttCtx, pkt: Pkt): Future[void] =
461461
result = ctx.work()
462462

463463
proc onPublish(ctx: MqttCtx, pkt: Pkt) {.async.} =
464-
let qos = (pkt.flags shr 1) and 0x03
464+
let
465+
qos = (pkt.flags shr 1) and 0x03
466+
retain = pkt.flags and 0x01 # When subscribing and first message is a
467+
# retained message, this will be `1`
468+
# otherwise `0`.
465469
var
466470
offset: int
467471
msgid: MsgId
@@ -662,7 +666,28 @@ proc disconnect*(ctx: MqttCtx) {.async.} =
662666
ctx.state = Disabled
663667

664668
proc publish*(ctx: MqttCtx, topic: string, message: string, qos=0, retain=false) {.async.} =
665-
## Publish a message
669+
## Publish a message.
670+
##
671+
## **Required:**
672+
## - topic: string
673+
## - message: string
674+
##
675+
## **Optional:**
676+
## - qos: int = 1, 2 or 3
677+
## - retain: bool = true or false
678+
##
679+
##
680+
## **Publish message:**
681+
## .. code-block::nim
682+
## ctx.publish(topic = "nmqtt", message = "Hey there", qos = 0, retain = true)
683+
##
684+
##
685+
## **Remove retained message on topic:**
686+
##
687+
## Set the `message` to _null_.
688+
## .. code-block::nim
689+
## ctx.publish(topic = "nmqtt", message = "", qos = 0, retain = true)
690+
##
666691
let msgId = ctx.nextMsgId()
667692
ctx.workQueue[msgId] = Work(wk: PubWork, msgId: msgId, topic: topic, qos: qos, message: message, retain: retain, typ: Publish)
668693
await ctx.work()

0 commit comments

Comments
 (0)