Subscribe to a topic.
Attention: Valid only with Altair Communication Extension.
Syntax
[mid,status,messageinfo] = mqttsubscribe(clientid, topic, qos)
Inputs
- clientid
- client id, an output of mqttclient.
- Type: string
- topic
- topic to be subscribe.
- Type: string
- qos
- the requested Quality of Service for this subscription, valid options are 0,1,2.
- Type: scalar
-
- qos = 0:
- This level guarantees a best-effort delivery. There is no guarantee of delivery. The recipient does not acknowledge receipt of the message and the message is not stored and re-transmitted by the sender.
- qos = 1:
- This level guarantees that a message is delivered at least one time to the receiver. The sender stores the message until it gets a PUBACK packet from the receiver that acknowledges receipt of the message. It is possible for a message to be sent or delivered multiple times.
- qos = 2:
- This level guarantees that each message is received only once by the intended recipients. It is the safest and slowest quality of service level.
Outputs
- mid
- message id of publish command.
- Type: scalar
- status
- status return codes.
- Type: scalar
- messageinfo
- description of publish status flag
- Type: string
-
- status = 0:
- on success.
- status = 1:
- out of memory condition occurred.
- status = 3:
- input parameters were invalid.
- status = 4:
- client isn’t connected to a broker.
- status = 18:
- topic is not valid UTF-8.
- status = 25:
- resulting packet is larger than supported by the broker.
Examples
subscribe to a given topic
cld = mqttclient('test.mosquitto.org','port',1883);
[mid, rc, Mqttmessageinfo] = mqttsubscribe(cld, 'topic100',2)
mid = 1
rc = 0
Mqttmessageinfo = success
subscribe to a topic with on_subscribe callback
function test_subscribecallback(clientid, mid, grant_qos)
printf('Subscribe callback is triggered successfully\n');
printf('message id: %s\n', num2str(mid));
printf('grant_qos: %s\n', num2str(grant_qos));
end
cld = mqttclient('test.mosquitto.org','port',1883,'on_subscribe','test_subscribecallback');
[mid, rc, Mqttmessageinfo] = mqttsubscribe(cld, 'topic100',2)
mid = 1
rc = 0
Mqttmessageinfo = success
Subscribe callback is triggered successfully
message id: 1
grant_qos: 1