Async Query-Response Web API using MQTT over Websockets
See also Paho MQTT Javascript client documentation at: https://www.eclipse.org/paho/files/jsdoc/Paho.MQTT.Client.html
MQTT host/connection and channel/topic setup:
All settings are configured by passing a mandatory configuration object to the constructor, with the following fields:
const api_conf = {
hosts: ["picalor.local", "169.254.207.204"],
ports: [9001, 9001],
path: "/mqtt",
useSSL: false,
timeout: 15,
data_topic: "data/picalor/core",
cmd_req_topic: "cmd/picalor/core/req",
cmd_resp_topic: "cmd/picalor/core/resp",
id_prefix: "picalor_gui_",
};
- Requests are sent using the command name as endpoint: "{cmd_req_topic}/{cmd-name}"
Example topic for requests: "cmd/picalor/core/req/{cmd-name}"
- Responses arrive under an "ok" or "err" subtopic for success or failure results: "{cmd_resp_topic}/ok/{cmd-name}" for command success responses and "{cmd_resp_topic}/err/{cmd-name}" for command error responses.
Example topic for responses: "cmd/picalor/core/resp/ok/{cmd-name}"
- For async or streaming results like telemetry data, callbacks can be registered on the data_topic channel, extended with a data subkey as endpoint: "{data_topic}/{data-key}"
Example topic for telemetry data: "data/picalor/core/{data-key}"
Ulrich Lukas 2022-11-11
| async MqttWebsocketsApi::query |
( |
|
cmd, |
|
|
|
value = true |
|
) |
| |
|
inline |
Send a cmd-value query to the message broker and resolve with the response.
The query is published on the {cmd_req_topic}/{cmd-name} topic, with a payload consisting of the value argument as a JSON string.
Each query is supposed to be answered or acknowledged with a response on the topic channel, i.e: {cmd_resp_topic}/ok/{cmd-name} or {cmd_resp_topic}/err/{cmd-name}.
The response is supposed to contain a non-empty JSON string as payload.
- Parameters
-
| cmd | String name of the command |
| value | Any Javascript object which can be sent as a JSON string |
- Returns
- Promise resolving with the payload parsed as a Javascript object