subscribe
Syntax
Description
subscribes the MQTT client to the specified MQTT topic, and returns a table listing all
subscriptions for that client.SubscrTable
= subscribe(mqttClient
,mqttTopic
)
specifies additional subscription behaviors using optional name-value pairs.SubscrTable
= subscribe(mqttClient
,mqttTopic
,Name=Value
)
Examples
Subscribe to MQTT Topics
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
mqttClient
— MQTT client
Client
object
MQTT client specified as an icomm.mqtt.Client
object, created with
the mqttclient
function.
Example: mqttClient = mqttclient()
Data Types: object
mqttTopic
— MQTT topic
string | char
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"
QualityOfService
— Quality of Service (QoS)
0 (default) | 1 | 2
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
Callback
— Function to execute when message received
function handle | string | char
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
SubscrTable
— Subscriptions table
table
Table of all topics that the MQTT client is subscribed to.
Version History
Introduced in R2022a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)