feat: setting retain flags and QoS levels for outgoing MQTT messages#3994
feat: setting retain flags and QoS levels for outgoing MQTT messages#3994didier-wenzek wants to merge 5 commits intothin-edge:mainfrom
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! 🚀 New features to boost your workflow:
|
Robot Results
|
albinsuresh
left a comment
There was a problem hiding this comment.
The proposed approach looks good to me. Happy to approve once it is finalized.
| ("payload", payload), | ||
| ("raw_payload", raw_payload), | ||
| ("time", JsonValue::option(value.timestamp)), | ||
| ("transport", JsonValue::option(transport)), |
There was a problem hiding this comment.
Optional:
| ("transport", JsonValue::option(transport)), | |
| ("source", JsonValue::option(source)), |
As it is more about the source of the message: mqtt or file.
There was a problem hiding this comment.
As it is more about the source of the message: mqtt or file.
For an input message, this is the source, yes. But a transformed message, this is the target.
That said I'm not fully happy with the naming. An option is to simply remove this intermediate level that is not easy to name. By the way, this is what is proposed in the issue:
export function onMessage(message, context) {
return [{
topic: "foo",
payload: JSON.stringify({
text: "bar"
}),
qos: 1,
retain: true,
}];
}
There was a problem hiding this comment.
I also agree that moving the qos and retain to an MQTT specific key feels like the right thing to avoid polluting the root namespace, since we support non-mqtt sources/sinks as well. Yes, topic being in the message is contentious in that sense, but I guess we can't go back on that one now and we have already defined what a topic means for file sources as well.
There was a problem hiding this comment.
Yes,
topicbeing in themessageis contentious in that sense
I don't think so. This is a concept shared by all messaging systems. QoS and retained messages is not.
There was a problem hiding this comment.
I can understand we don't want to add too many things to the root level. Especially, QoS and retained are only related to mqtt input. However, then I would have one nested layer. It would look as below.
export function onMessage(message, context) {
return [{
topic: "foo",
payload: JSON.stringify({
text: "bar"
}),
mqtt: {
qos: 1,
retain: true,
}
}];
}The current approach (two nested layers) is a bit too nested for me.
export function onMessage(message, context) {
return [{
...message,
transport: { mqtt: { qos: 2, retain: true } }
}];
}There was a problem hiding this comment.
Yes,
topicbeing in themessageis contentious in that senseI don't think so. This is a concept shared by all messaging systems. QoS and retained messages is not.
I was referring to file sources where the path is conflated into topic.
There was a problem hiding this comment.
I was referring to file sources where the path is conflated into
topic.
Yeah. This is one of the point I wish to improve with this PR.
The path or command of a polling source is not fully conflated into topic. The flow definition can provide a topic name for its source. And the path or command is only used as a default.
To fix that, the proposal is to provide both, topic and source info. For a line received from a polling command:
{
"topic": "logical name of the source", # default to the command
"payload": "line read from the source",
"process": {
"command": "/some/polling/command"
}
}
There was a problem hiding this comment.
Rina's suggestion has been applied: 95928aa
export function onMessage(message, context) {
return [{
topic: "foo",
payload: JSON.stringify({
text: "bar"
}),
mqtt: {
qos: 1,
retain: true,
}
}];
}
Bravo555
left a comment
There was a problem hiding this comment.
Looks all good, also happy to approve once finalized.
| .map(|v| v.to_owned().into_value::<Transport>()) | ||
| .transpose() | ||
| else { | ||
| return Err(anyhow::anyhow!("Unexpected transport data").into()); |
There was a problem hiding this comment.
question: The error itself doesn't include the context for which message caused the problem, but I'm not sure where it's printed, is the message that caused the problem reported there? Otherwise it may be tricky to debug.
Signed-off-by: Didier Wenzek <didier.wenzek@free.fr>
Signed-off-by: Didier Wenzek <didier.wenzek@free.fr>
cdf1544 to
a5d6c03
Compare
|
@didier-wenzek For some reason I can't get a flow to publish a retain message...Am I doing something wrong? Below is the example I was trying to use to verify it (referencing the latest comment with the expected format)...you should be able to copy/paste the code below to recreate the scenario: # create a flow which just appends "/bar" to the incoming topic and adds the retained flag
mkdir -p /etc/tedge/mappers/c8y/flows/custom
cat <<'EOT' > /etc/tedge/mappers/c8y/flows/custom/main.js
export function onMessage(message, context) {
return [{
topic: message.topic + "/bar",
payload: JSON.stringify({
...JSON.parse(message.payload),
time: message.time,
seen: true,
}),
mqtt: {
retain: true,
},
}]
}
EOT
cat <<'EOT' > /etc/tedge/mappers/c8y/flows/custom/flow.toml
input.mqtt.topics = ["foo"]
[[steps]]
script = "/etc/tedge/mappers/c8y/flows/custom/main.js"
EOT
# publish a message to the topic
tedge mqtt pub 'foo' '{}'
sleep 1
# check for the retained message
tedge mqtt sub foo/bar --retained-only -C 1 -W 5 |
Nothing wrong your side. The code was not excepting all the attributes to be set. Fixed by: ec4956f |
reubenmiller
left a comment
There was a problem hiding this comment.
Approved. It works nicely.
Proposed changes
getting command line for polled and streamed incoming messagesTypes of changes
Paste Link to the issue
See #3984
Checklist
just prepare-devonce)just formatas mentioned in CODING_GUIDELINESjust checkas mentioned in CODING_GUIDELINESFurther comments