Main Content

subscribe

Subscribe to MQTT topic

Description

example

SubscrTable = subscribe(mqttClient,mqttTopic) subscribes the MQTT client to the specified MQTT topic, and returns a table listing all subscriptions for that client.

SubscrTable = subscribe(mqttClient,mqttTopic,Name=Value) specifies additional subscription behaviors using optional name-value pairs.

Examples

collapse all

Create an MQTT client connected to the Eclipse HiveMQ™ public broker and subscribe to several topics with different options.

Subscribe to the topic "trubits/mqTop48".

mqttClient = mqttclient("tcp://broker.hivemq.com");
mySub = subscribe(mqttClient,"trubits/mqTop48")
mySub =

  1×3 table

          Topic          QualityOfService    Callback
    _________________    ________________    ________

    "trubits/mqTop48"           0               ""
mySub = subscribe(mqttClient, "trubits/mqTmp52",QualityOfService=2)
mySub =

  2×3 table

          Topic          QualityOfService    Callback
    _________________    ________________    ________

    "trubits/mqTop48"           0               ""   
    "trubits/mqTmp52"           2               ""   

Create a callback function in the file showmessage.m that displays the topic and received message.

function showMessage(topic,data)
    disp(topic);
    disp(data);
end

Subscribe to a topic to have the callback executed when a message is received.

mySub = subscribe(mqttClient,"trubits/mqTsp61",Callback=@showmessage)
mySub =

  3×3 table

          Topic          QualityOfService      Callback   
    _________________    ________________    _____________

    "trubits/mqTop48"           0            ""           
    "trubits/mqTmp52"           2            ""           
    "trubits/mqTsp61"           0            "showmessage"

Input Arguments

collapse all

MQTT client specified as an icomm.mqtt.Client object, created with the mqttclient function.

Example: mqttClient = mqttclient()

Data Types: object

MQTT topic to subscribe to, specified as a string or character vector.

Example: "trubits/mqTop48"

Data Types: string | char

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: Callback="showMsg"

Quality of Service (QoS) for message delivery, specified as an integer value of 0, 1, or 2:

  • 0 — Messages delivered at most once, not more (default).

  • 1 — Messages delivered at least once, not less.

  • 2 — Messages delivered exactly once, not more or less.

Example: QualityOfService=1

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Function to execute when a message is received on the subscribed topic, specified as a function handle, string, or character vector. The function is called with two input arguments (topic, data) in that order.

Example: Callback=@notifyUser

Data Types: char | string | function_handle

Output Arguments

collapse all

Table of all topics that the MQTT client is subscribed to.

Version History

Introduced in R2022a

See Also

Functions