@faybl/client 4.1.1 → 4.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -2
- package/package.json +1 -1
- package/src/mqttUtils.js +52 -11
package/README.md
CHANGED
|
@@ -98,7 +98,9 @@ Changes are published on the bus as they happen:
|
|
|
98
98
|
gets `{ entity, operation, rid, record }`, where `record` is the whole record as read after the
|
|
99
99
|
change, or `{}` for a delete.
|
|
100
100
|
- `conf.subscribeWithAck(...)` takes the same and returns `{ ready, unsubscribe }`; `ready` resolves
|
|
101
|
-
once the broker has acknowledged the subscription, and rejects if
|
|
101
|
+
once the broker has acknowledged the subscription, and rejects only if the broker refused it. A
|
|
102
|
+
connection that closes first is waited out: the subscription is asked for again on the
|
|
103
|
+
reconnect.
|
|
102
104
|
- `conf.unsubscribe({ entity, operation, rid, callback })`. Without a callback it removes every
|
|
103
105
|
callback on that subscription.
|
|
104
106
|
|
|
@@ -174,7 +176,9 @@ subscription may be a filter with wildcards.
|
|
|
174
176
|
- `mqttClient.subscribe(topic, callback, { qos })` returns the function that undoes it. The
|
|
175
177
|
callback gets `{ topic, data }`, with `data` parsed from JSON.
|
|
176
178
|
- `mqttClient.subscribeWithAck(topic, callback, { qos })` returns `{ ready, unsubscribe }`; `ready`
|
|
177
|
-
resolves once the broker has granted the QoS asked for, 0 by default
|
|
179
|
+
resolves once the broker has granted the QoS asked for, 0 by default, and rejects only if it
|
|
180
|
+
refused, as above. Several subscriptions to one topic share one acknowledgement, and the topic
|
|
181
|
+
keeps the strictest QoS asked for.
|
|
178
182
|
- `mqttClient.unsubscribe(topic, callback)`. Without a callback it removes every callback on the
|
|
179
183
|
topic.
|
|
180
184
|
|
package/package.json
CHANGED
package/src/mqttUtils.js
CHANGED
|
@@ -69,14 +69,32 @@ const messageHandler = (getState, report) => async (topic, jsonData) => {
|
|
|
69
69
|
return undefined;
|
|
70
70
|
};
|
|
71
71
|
|
|
72
|
+
// A SUBACK return code of 0x80 or above refuses the subscription.
|
|
73
|
+
const SUBACK_FAILURE = 0x80;
|
|
74
|
+
|
|
75
|
+
// The broker's answer for one topic: undefined when MQTT.js sent nothing for it, which it does for
|
|
76
|
+
// a topic this connection already holds at that QoS or higher.
|
|
77
|
+
const grantedQos = (granted, topic) => granted?.find((grant) => grant.topic === topic)?.qos;
|
|
78
|
+
|
|
79
|
+
const subscriptionRefusal = (topic, qos, granted) =>
|
|
80
|
+
granted !== undefined && (granted >= SUBACK_FAILURE || granted < qos)
|
|
81
|
+
? Error(`MQTT subscription to ${topic} was not granted QoS ${qos}`)
|
|
82
|
+
: undefined;
|
|
83
|
+
|
|
72
84
|
export const createMqttHandler = (client, { report = () => undefined } = {}) => {
|
|
73
85
|
const { getState, dispatch } = createStore({}, reducer);
|
|
86
|
+
// The acknowledgement each topic was subscribed with, so a second subscription to a topic waits
|
|
87
|
+
// for the same SUBACK rather than settling before the broker has answered.
|
|
88
|
+
// eslint-disable-next-line functional/no-let -- Acknowledgements outlive the independent subscribe and unsubscribe calls that share them; each change replaces the object.
|
|
89
|
+
let acknowledgements = {};
|
|
74
90
|
|
|
75
91
|
const handleMessage = messageHandler(getState, report);
|
|
76
92
|
client.on('message', handleMessage);
|
|
77
93
|
const unsubscribe = (topic, callback) => {
|
|
78
94
|
dispatch({ type: 'remove', topic, callback });
|
|
79
|
-
if (
|
|
95
|
+
if (getState(topic)) return;
|
|
96
|
+
acknowledgements = R.dissoc(topic, acknowledgements);
|
|
97
|
+
client.unsubscribe(topic);
|
|
80
98
|
};
|
|
81
99
|
|
|
82
100
|
const subscribe = (topic, callback, { preCallback, qos } = {}) => {
|
|
@@ -85,18 +103,41 @@ export const createMqttHandler = (client, { report = () => undefined } = {}) =>
|
|
|
85
103
|
return () => unsubscribe(topic, callback);
|
|
86
104
|
};
|
|
87
105
|
|
|
106
|
+
// Only the broker can refuse a subscription. An error instead of a SUBACK means the connection
|
|
107
|
+
// closed first, so the request is made again once it reconnects, rather than failing. A client
|
|
108
|
+
// that is being closed rejects. MQTT.js counts the topic as held from the first attempt and would
|
|
109
|
+
// answer a plain retry at once with no grants; `resubscribe`, the flag it uses for its own
|
|
110
|
+
// resubscriptions, makes it send the SUBSCRIBE so the retry waits for the broker's answer.
|
|
111
|
+
const requestAcknowledgement = (topic, qos) =>
|
|
112
|
+
new Promise((resolve, reject) => {
|
|
113
|
+
const answer = (error, granted) => {
|
|
114
|
+
if (error) return client.disconnecting ? reject(error) : client.once('connect', retry);
|
|
115
|
+
const refusal = subscriptionRefusal(topic, qos, grantedQos(granted, topic));
|
|
116
|
+
return refusal ? reject(refusal) : resolve();
|
|
117
|
+
};
|
|
118
|
+
const retry = () => client.subscribe({ [topic]: { qos }, resubscribe: true }, answer);
|
|
119
|
+
client.subscribe(topic, { qos }, answer);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const acknowledgementFor = (topic, qos) => {
|
|
123
|
+
const held = acknowledgements[topic];
|
|
124
|
+
if (held && held.qos >= qos) return held.ready;
|
|
125
|
+
const ready = requestAcknowledgement(topic, qos);
|
|
126
|
+
acknowledgements = R.assoc(topic, { qos, ready }, acknowledgements);
|
|
127
|
+
// A refused subscription is asked for again by the next caller.
|
|
128
|
+
ready.catch(() => {
|
|
129
|
+
if (acknowledgements[topic]?.ready === ready)
|
|
130
|
+
acknowledgements = R.dissoc(topic, acknowledgements);
|
|
131
|
+
});
|
|
132
|
+
return ready;
|
|
133
|
+
};
|
|
134
|
+
|
|
88
135
|
const subscribeWithAck = (topic, callback, { qos = 0, preCallback } = {}) => {
|
|
89
136
|
dispatch({ type: 'add', topic, callback, preCallback });
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return reject(Error(`MQTT subscription to ${topic} was not granted QoS ${qos}`));
|
|
95
|
-
}
|
|
96
|
-
return resolve();
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
return { ready, unsubscribe: () => unsubscribe(topic, callback) };
|
|
137
|
+
return {
|
|
138
|
+
ready: acknowledgementFor(topic, qos),
|
|
139
|
+
unsubscribe: () => unsubscribe(topic, callback),
|
|
140
|
+
};
|
|
100
141
|
};
|
|
101
142
|
|
|
102
143
|
const publish = (topic, message, options) => client.publish(topic, toPayload(message), options);
|