Skip to content

Commit bc34e09

Browse files
authored
new func subx. add QoS docs. Fixes #56 (#67)
Addition of subx to allow user to choose quality of service level Docs on quality of service levels (for publishing and subscribing) - affects performance/quality/throughput Prev sub method hardcoded with a QoS level. Existing sub functionality not changed (calls subx).
1 parent c4a8ed8 commit bc34e09

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

docs/reference.md

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ They allow you to interact with MQTT brokers and send and receive messages.
1313
[`pub`](#mqttpub) publish a message to a topic<br>
1414
[`pubx`](#mqttpubx) publish a message to a topic controlling qos and ret<br>
1515
[`sub`](#mqttsub) subscribe to a topic<br>
16+
[`subx`](#mqttsubx) subscribe to a topic<br>
1617
[`unsub`](#mqttunsub) unsubscribe from a topic
1718

1819
**Callback functions**<br>
@@ -209,7 +210,7 @@ q).mqtt.pub[`topic1;"This is a test message"];
209210
>
210211
> Where
211212
>
212-
> 1. `kqos` is set to 1. The broker/client will deliver the message at least once, with confirmation required.
213+
> 1. `kqos` is set to 1. The broker/client will deliver the message at least once, with ack messages required.
213214
> 2. `kret` is set to `0b`. Messages are not retained after sending.
214215
215216

@@ -225,7 +226,10 @@ Where
225226

226227
- `topic` is a symbol denoting the topic that the message is to be sent to
227228
- `msg` is a string of the message being sent to the broker
228-
- `kqos` is an long denoting the quality of service to be used
229+
- `kqos` is an long denoting the quality of service to be used. The higher the quality of service, the lower the throughput/performance. Possible values are:
230+
- 0 (QoS 0 - at most once) comparable to TCP
231+
- 1 (Qos 1 - at least once) as with QoS 0 but with use of ack message for each message sent
232+
- 2 (QoS 2 - only once) as with QoS 1 but with use of multiple request/response flows for each message
229233
- `kret` is a boolean denoting if published messages are to be retained
230234

231235
returns a callback to the process stating that the message has been sent to the broker.
@@ -248,7 +252,7 @@ _Subscribe to a topic on a Mosquitto broker process_
248252
.mqtt.sub topic
249253
```
250254

251-
Where `topic` is a symbol denoting the topic that the process should listen to, returns a callback to the process when a message is received on topic stating that the message was received and what that message is.
255+
Where `topic` is a symbol denoting the topic that the process should listen to. Received msgs on that topic can then be processed via the callback [.mqtt.msgrcvd](mqttmsgrecv)
252256

253257
```q
254258
// Connect to the host broker and publish a message
@@ -259,6 +263,47 @@ q).mqtt.sub[`topic1]
259263
(`msgrcvd;"topic1";"This is a test message")
260264
```
261265

266+
> This function is a projection of the function `.mqtt.pubx` defined below.
267+
>
268+
> Where
269+
>
270+
> 1. `kqos` is set to 1. The broker/client will deliver the message at least once, with confirmation messages sent to the broker from the subscriber for each message received.
271+
272+
## `.mqtt.subx`
273+
274+
_Subscribe to a topic on a Mosquitto broker process, controlling quality of service_
275+
276+
```txt
277+
.mqtt.subx[topic;kqos]
278+
```
279+
280+
Where `
281+
282+
- `topic` is a symbol denoting the topic that the process should listen to. Received msgs on that topic can then be processed via the callback [.mqtt.msgrcvd](mqttmsgrecv)
283+
- `kqos` is an long denoting the quality of service to be used. The higher the quality of service, the lower the throughput/performance. Possible values are:
284+
- 0 (QoS 0 - at most once) comparable to TCP
285+
- 1 (Qos 1 - at least once) as with QoS 0 but with use of ack message for each message sent
286+
- 2 (QoS 2 - only once) as with QoS 1 but with use of multiple request/response flows for each message
287+
288+
> Note that the subscriber QoS can downgrade the intended Qos from the publisher, but not upgrade it. For example, when publisher subscriber using QoS 0 or 1 with a Mosquitto broker:
289+
>
290+
> | Pub QoS | Sub QoS | Acks From Broker to Pub | Acks from Sub to Broker |
291+
> | --- | --- | --- | --- |
292+
> |0|0|No|No|
293+
> |1|0|Yes|No|
294+
> |0|1|No|No|
295+
> |1|1|Yes|Yes|
296+
>
297+
> In this scenario, when the publisher uses QoS 1 and the subscriber has less requirements for QoS (ie. can set to 0), the subscriber will gain better performance.
298+
299+
```q
300+
// Connect to the host broker and publish a message
301+
q).mqtt.conn[`$"tcp://localhost:1883";`rcv]
302+
303+
// Subscribe to topic1 and recieve a message sent to that topic
304+
q).mqtt.subx[`topic1;0]
305+
(`msgrcvd;"topic1";"This is a test message")
306+
```
262307

263308
## `.mqtt.unsub`
264309

q/mqtt.q

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
connX:`mqttkdb 2:(`connX;3);
44
init :`mqttkdb 2:(`init;1);
55
pubx :`mqttkdb 2:(`pub ;4);
6-
sub :`mqttkdb 2:(`sub ;1);
6+
subx :`mqttkdb 2:(`sub ;2);
77
unsub:`mqttkdb 2:(`unsub;1);
88
isConnected:`mqttkdb 2:(`isConnected;1);
99
disconnect :`mqttkdb 2:(`disconnect;1);
1010

1111

1212
pub:.mqtt.pubx[;;1;0];
13+
sub:.mqtt.subx[;1];
1314
conn:{[tcpconn;pname;opt]connX[tcpconn;pname](enlist[`]!enlist(::)),opt};
1415
msgsent:{0N!(`msgsent;x);};
1516
disconn:{0N!(`disconn;x);};

src/mqtt.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,27 @@ EXP K pub(K topic, K msg, K kqos, K kret){
279279
/* Subscribe to a topic
280280
* topic = topic name as a symbol
281281
*/
282-
EXP K sub(K topic){
282+
EXP K sub(K topic,K kqos){
283283
int err;
284+
long qos;
284285
if(topic->t != -KS)
285286
return krr("topic type");
286287
if(client == 0)
287288
return krr("not connected");
288-
if(MQTTCLIENT_SUCCESS != (err = MQTTClient_subscribe(client, topic->s, 1)))
289+
switch(kqos->t){
290+
case -KH:
291+
qos = kqos->h;
292+
break;
293+
case -KI:
294+
qos = kqos->i;
295+
break;
296+
case -KJ:
297+
qos = kqos->j;
298+
break;
299+
default:
300+
return krr("qos type");
301+
}
302+
if(MQTTCLIENT_SUCCESS != (err = MQTTClient_subscribe(client, topic->s, qos)))
289303
return krr((S)MQTTClient_strerror(err));
290304
return (K)0;
291305
}

0 commit comments

Comments
 (0)