MQTT Client
Publish and subscribe MQTT messages directly from .mqtt files in Visual Studio Code.
.mqtt files support two formats:
| Format |
Description |
| Legacy |
Line-based format using # %%, # $$, # ## block markers |
| YAML |
Structured YAML with connection:, subscriptions:, and publish: keys |
Both formats support variables, and existing legacy files can be converted to YAML using the built-in command.
Write blocks in a .mqtt file. Each block starts on a line beginning with a unique string that starts with #. The block-starting line must not contain any additional characters, including spaces.
Connection block
Write connection settings in a block starting with # %%.
Click Connect displayed above # %% to connect to the broker.
| Name |
Required |
Default |
Description |
| url |
✓ |
|
URL of MQTT broker |
| user |
|
|
User ID |
| password |
|
|
Password, or ${secret:<key>} to retrieve from SecretStorage. |
| clientId |
|
"mqtt_vsc_" + 10 random chars |
Client ID |
| ca |
|
|
Path to CA certificate file (relative to workspace root) |
| reconnectPeriod |
|
0 (disabled) |
Reconnect interval in milliseconds |
| will_topic |
|
|
Last Will topic |
| will_payload |
|
|
Last Will payload |
| will_qos |
|
0 |
Last Will QoS: 0, 1, or 2 |
| will_retain |
|
false |
Last Will retain flag: true or false |
Warning: Avoid writing passwords in plain text, as .mqtt files may be committed to version control. Use ${secret:<key>} instead to keep credentials out of the file.
# %%
url: mqtt://broker:1883
user: user
password: ${secret:my-broker}
Subscription block
Write one topic per line in a block starting with # $$.
Click Subscribe all or Subscribe to open a subscription tab and start receiving messages.
# $$
foo
bar/#
Publish block
Write one block starting with # ## for each message you want to publish.
Write the publish settings, leave a blank line, then write the message payload.
A line matching another block marker (e.g. # %%) inside the payload is not supported — it will be treated as the start of a new block.
Click Publish message displayed above # ## to publish.
| Name |
Required |
Default |
Description |
| topic |
✓ |
|
Publish topic |
| qos |
|
0 |
QoS: 0, 1, or 2 |
| retain |
|
false |
Retain flag: true or false |
# ## Publish 'message'
topic: foo
qos: 0
retain: false
message
# ## Publish '{ "message": "hello" }'
topic: bar
qos: 1
retain: true
{
"message": "hello"
}
Variables
Define variables above all blocks, one per line, in the form name=value.
Reference a variable with {{name}}. If the variable is not defined, it is treated as a literal string.
To use a {{...}} literal that would otherwise match a defined variable, wrap it in an extra layer: {{{{name}}}}.
Variables can be used in:
- Other variable values (only variables defined above can be referenced)
- Setting values (
key: value lines in connection and publish blocks)
- Topics in Subscription blocks
- Payloads in Publish blocks
host=broker
TopicLevel1=foo
TOPIC_FOO={{TopicLevel1}}/foo
message=Hello, world!
# %%
url: mqtt://{{host}}:1883
# $$
{{TopicLevel1}}/#
# ## Publish '{ "message": "Hello, world!" }'
topic: {{TopicLevel1}}/bar
{
"message": "{{message}}"
}
# ## Publish '{{message}}'
topic: {{TOPIC_FOO}}
{{{{message}}}}
Any line that doesn't follow the rules of a block is ignored and can be used as a comment.
Comments are not supported inside subscription topics or message payloads.
comment
host=broker
comment
message=Hello, world!
# %%
comment
url: mqtt://{{host}}:1883
comment
# $$
{{TopicLevel1}}/#
# ##
comment
topic: {{TopicLevel1}}/bar
comment
{
"message": "{{message}}"
}
not a comment
YAML format provides a more structured alternative. A file is detected as YAML format when its first non-blank, non-comment line is one of: variables:, connection:, subscriptions:, or publish:.
Structure
variables:
host: broker.example.com
topic1: sensors/temperature
connection:
url: "mqtt://{{host}}:1883"
username: user
password: "${secret:my-broker}"
clientId: my-client
ca: certs/ca.crt
reconnectPeriod: 5000
subscriptions:
- "{{topic1}}"
- topic: sensors/humidity
qos: 1
publish:
- topic: sensors/update
qos: 0
retain: false
payload: '{"status": "ok"}'
- name: Multiline JSON
topic: sensors/data
qos: 1
payload: |
{
"temperature": 25,
"unit": "celsius"
}
connection
| Name |
Required |
Default |
Description |
| url |
✓ |
|
URL of MQTT broker |
| username |
|
|
User ID |
| password |
|
|
Password, or ${secret:<key>} to retrieve from SecretStorage. |
| clientId |
|
"mqtt_vsc_" + 10 random chars |
Client ID |
| ca |
|
|
Path to CA certificate file (relative to workspace root) |
| reconnectPeriod |
|
0 (disabled) |
Reconnect interval in milliseconds |
| will.topic |
|
|
Last Will topic |
| will.payload |
|
|
Last Will payload |
| will.qos |
|
0 |
Last Will QoS: 0, 1, or 2 |
| will.retain |
|
false |
Last Will retain flag |
Warning: Avoid writing passwords in plain text, as .mqtt files may be committed to version control. Use ${secret:<key>} instead to keep credentials out of the file.
subscriptions
Each item can be a plain string or an object with topic and optional qos:
subscriptions:
- sensors/temperature # qos defaults to 0
- topic: sensors/humidity
qos: 1
publish
| Name |
Required |
Default |
Description |
| topic |
✓ |
|
Publish topic |
| qos |
|
0 |
QoS: 0, 1, or 2 |
| retain |
|
false |
Retain flag |
| payload |
|
(empty) |
Message payload — must be a string (see below) |
| name |
|
|
Display name shown in CodeLens |
Payload formats
The payload field must be a string. The following formats are supported:
| Format |
Example |
| Plain text |
payload: hello |
| JSON |
payload: '{"temperature": 25}' |
| XML |
payload: '<temp>25</temp>' |
| CSV |
payload: "25,celsius" |
| Numeric string |
payload: "25.0" |
| Empty |
payload: "" or omit the field |
| Multiline |
payload: \| followed by indented lines |
Note: Writing payload: as a YAML mapping (without quotes or |) will result in an editor warning and the message will not be sent.
Variables
Variables work the same way as in the legacy format. Use {{variableName}} to reference a variable.
variables:
host: broker.example.com
connection:
url: "mqtt://{{host}}:1883"
YAML Anchors and Merge Keys
Standard YAML anchors (&), aliases (*), and merge keys (<<:) are supported. This is useful for sharing settings across multiple publish entries.
publish:
- &base
topic: sensors/temperature
qos: 1
retain: false
payload: "hello"
- <<: *base
topic: sensors/humidity
payload: "world"
The second entry inherits qos and retain from base, and overrides topic and payload.
Use the MQTT: Convert to YAML command to convert a legacy .mqtt file to YAML format.
- Open the
.mqtt file to convert
- Run the command via:
- Right-click in the editor → MQTT: Convert to YAML
- Command Palette (
Ctrl+Shift+P / Cmd+Shift+P) → MQTT: Convert to YAML
- A new file is created in the same directory with
.yaml inserted before the extension
(e.g., config.mqtt → config.yaml.mqtt)
- The new file opens automatically
Notes:
- The original file is not modified or deleted
- If the file is already in YAML format, a message is shown and no file is created
Example
Before (config.mqtt):
host=broker.example.com
# %%
url: mqtt://{{host}}:1883
user: myuser
password: ${secret:my-broker}
# $$
sensors/temperature
sensors/humidity
# ## Publish status
topic: sensors/status
qos: 1
retain: false
{"status": "ok"}
After (config.yaml.mqtt):
variables:
host: broker.example.com
connection:
url: "mqtt://{{host}}:1883"
username: myuser
password: "${secret:my-broker}"
subscriptions:
- sensors/temperature
- sensors/humidity
publish:
- topic: sensors/status
name: Publish status
qos: 1
retain: false
payload: '{"status": "ok"}'
Secret Storage
Instead of writing passwords in plain text, you can store them in VSCode's SecretStorage (OS keychain) and reference them with ${secret:<key>}.
The <key> is a label to identify the stored secret — not the password itself. It can be any string that does not contain }. Choose a descriptive name like my-broker or work-mqtt.
Syntax
password: ${secret:my-broker}
When connecting, the extension resolves the secret using the following steps:
| Situation |
Behavior |
| Key exists in SecretStorage |
Stored value is used directly |
| Key not found |
A password input dialog is shown; the entered value is saved for future use |
| Input dialog is cancelled |
The connection is aborted |
Commands
| Command |
Description |
| MQTT: Set Secret |
Store or update a secret by key |
| MQTT: Delete Secret |
Delete a stored secret by key |
Legacy format example
# %%
url: mqtt://broker:1883
user: user
password: ${secret:my-broker}
YAML format example
connection:
url: mqtt://broker:1883
username: user
password: "${secret:my-broker}"
Settings
| Key |
Default |
Description |
mqttclient.subscription.show.retain |
false |
Show whether a received message has the retain flag set. |
mqttclient.subscription.maxMessages |
500 |
Maximum number of messages to keep in the subscription window. Set to 0 for unlimited. |
Third-Party Notices
MQTT.js
Copyright (c) 2015-2016 MQTT.js contributors
Released under the MIT license
https://github.com/mqttjs/MQTT.js/blob/main/LICENSE.md
wait-queue
Copyright (c) 2017 flarestart
Released under the MIT license
https://github.com/flarestart/wait-queue/blob/master/LICENSE
js-yaml
Copyright (C) 2011-2015 by Vitaly Puzrin
Released under the MIT license
https://github.com/nodeca/js-yaml/blob/master/LICENSE