MQTT implementation inconsistent with the MQTT Binding Template.
The current WoT MQTT Binding Template defines the expected vocabulary for MQTT forms as follows:
Example event form:
{
"href": "mqtt://broker.com:1883",
"op": [
"subscribeevent",
"unsubscribeevent"
],
"mqv:filter": "thing1/events/overheating"
}
Example action form:
{
"href": "mqtt://broker.com:1883",
"op": [
"invokeaction"
],
"mqv:topic": "application/devices/thing1/program/commands/reset"
}
However, the current implementation, expects the HREF field to contain the broker url + topic path, with the specific mqtt vocabulary not being supported afaik.
Thanks @dvalnn . This is a back and forth topic. The binding template needs an update as well since we want to move towards using the href for the topic. However, we can still make a fix here until that happens. Do you think you can provide a PR?
FYI: see https://github.com/w3c/wot-thing-description/issues/2069 for the parent issue and https://github.com/w3c/wot-binding-templates/issues/292 for MQTT specifically.
I have been working closely with the W3C WoT for the past few months as part of my master's thesis. The tools for working with Thing Models have been the most useful to me so far, as I have been extensively using various interconnected TMs for my work.
I haven't really been successful when trying to use the WoT scripting API as what it currently supports and what need to do intersect in an awkward manner.
I'd love to give some feedback based on my experience so far with the various tools, if there is an appropriate channel for that.
In regards to this issue, I have been working based of the most recent version of the mqtt binding template. As such my forms tend to look like this:
"forms": [
{
"href": "{{CITYLINK_HREF}}",
"mqv:filter": "{{CITYLINK_EVENT}}/log",
"op": [
"subscribeevent",
"unsubscribeevent"
],
"mqv:qos": 0,
"mqv:retain": false,
"contentType": "application/json"
}
]
I use placeholders quite extensively. In particular, every single "href" value is just the placeholder above that ends up resolving to the broker url during Thing Description instantiation. I have found that the standard is somewhat inconclusive on what should happen if i suppress the href all together and just provide a "base" element at the TD top level, so i have just been doing it like this.
In order to work with these forms, I abandoned the node-wot library and quickly threw together a custom solution. Here are some ts snippets:
For events ( properties would work similarly ) I am currently extracting the href as the brokerUrl, and the mqv:filter property for the topic. This works well as there are no ambiguities regarding the end of the broker address and the start of the mqtt topic. The only quirk here is that mqv:filter is not part of the current Thing Description type, so I have to access is using the object["key"] syntax.
export function subscribeEvent(
td: ThingDescription,
event: string,
/* some other fields */
): {
const eventDef = td.events?.[event];
const eventForm = eventDef?.forms?.[0];
if (!eventForm) {
return new Error(`No valid form found for event "${event}"`);
}
const brokerUrl = eventForm.href;
const topic = eventForm["mqv:filter"] as string;
if (!topic) {
return new Error(`Missing topic ("mqv:filter") for event "${event}"`);
}
// rest of the function ...
for actions, i also extract the the qos and retain properties from the Thing Description.
export function invokeAction(
td: ThingDescription,
action: string,
/* Some other fields */
): Error | null {
const actionDef = td.actions?.[action];
const actionForm = actionDef?.forms?.[0];
if (!actionForm) {
return new Error(`No valid form found for action "${action}"`);
}
const qos = (actionForm["mqv:qos"] as 0 | 1 | 2) ?? 0; // Awkward....
const retain = (actionForm["mqv:retain"] as boolean) ?? false; // Same as above and the previous example
const topic = actionForm["mqv:topic"] as string;
if (!topic) {
return new Error(`Missing topic ("mqv:topic") for action "${action}"`);
}
console.debug("Initiating MQTT client for action:", action);
const brokerUrl = actionForm.href;
const client = mqtt.connect(brokerUrl);
// .... rest of the function
Overall I like this API ( ignoring the quirks that come from the ThingDescription type not including the mqtt bindings ). I could provide a PR when I find some time in order to address this, however it would probably include the need to extend the TD type as well.
My final 2 cents:
Merging the href and topic/filter fields would mitigate somewhat the awkwardness of the href field containing only the broker url for every form, at the cost of some extra parsing overhead at runtime. However, I personally appreciate the clarity provided by having separate fields. The href issue could then maybe be solved by allowing the field to be suppressed, given that a "base" field is included at the Thing level.
Thank you @dvalnn! About feedback for scripting API, you can use our Discord server (to chat about scripting or node-wot) or even straight Github Issues (I'd love to hear more from you about the shortcomings you found).
Sorry but I don't fully follow the problem Which version are you using? Current node-wot should already be working with mqv:topic and mqv:filter see https://github.com/eclipse-thingweb/node-wot/blob/master/packages/binding-mqtt/src/mqtt.ts#L33
However, as @egekorkan mentioned, this convention might be changed in the near future due to updates in the protocol binding document.
Any updates on this? I'm considering migrating from MQTT.js to node-wot to provide a more unified communication interface across protocols, but I'm unsure about the current status of the MQTT binding and its compatibility with the WoT MQTT Binding Template.
Is https://github.com/eclipse-thingweb/node-wot/pull/1511 related?
@Rednas83 hi there. Sorry no updates yet :/
Given no answer from the original author and the fact that he probably hit #1511, I'm closing this issue as the current implementation should indeed be aligned with https://w3c.github.io/wot-binding-templates/bindings/protocols/mqtt/index.html (node-wot is actually more forgiving than the spec). @Rednas83, feel free to reopen if you have some concrete missing points.