MQTT Client
Publish and subscribe MQTT messages directly from .mqtt files in Visual Studio Code.
- Write the broker connection, the topics to subscribe to and the messages to publish in a
.mqtt file, and run them with links shown above each block
- See received messages in a subscription window, filtered by topic
- Reuse values with variables in settings, topics and payloads
- Keep passwords out of the file with
${secret:<key>} (stored in the OS keychain)
- Two file formats: YAML, and the line-based legacy format with a command to convert it to YAML
The extension works only in a trusted workspace. See Workspace Trust.
Quick Start
Create a file with the .mqtt extension (e.g. test.mqtt) and write:
connection:
url: mqtt://localhost:1883
subscriptions:
- sensors/#
publish:
- topic: sensors/temperature
payload: "25"
Click Connect above connection:.
Click Subscribe all above subscriptions:. The subscription window opens.
Click Publish message above the publish entry. The message appears in the subscription window.
Usage
Links are shown above the blocks of a .mqtt file. They work the same in both file formats.
| Where |
Link |
Shown |
Connection (connection: / # %%) |
Connect / Disconnect |
|
Subscriptions (subscriptions: / # $$) |
Subscribe all, Unsubscribe all |
while connected |
| Each subscription topic |
Subscribe / Unsubscribe |
while connected |
Subscriptions (subscriptions: / # $$) |
Show subscription window |
once you have connected, also after disconnecting |
Each publish entry (- topic: / # ##) |
Publish message |
while connected |
Subscribing opens the subscription window, where received messages are shown.
.mqtt files support two formats:
| Format |
Description |
| YAML |
Structured YAML with connection:, subscriptions:, and publish: keys |
| Legacy |
Line-based format using # %%, # $$, # ## block markers |
Both formats support variables, and existing legacy files can be converted to YAML using the built-in command.
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 Secret Storage. Avoid writing it in plain text. |
| clientId |
|
"mqtt_vsc_" + 10 random chars |
Client ID |
| ca |
|
|
Path to CA certificate file (relative to the first workspace folder). The file must be inside the workspace; symlinks pointing outside it are rejected. Server certificates are always verified for mqtts/ssl/tls/wss; set this when the broker uses a self-signed or private CA. |
| 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 |
Invalid will.qos or will.retain values are shown as warnings in the editor, and the extension does not connect with them.
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
qos must be 0, 1, or 2. It can also be a string that uses a variable, such as qos: "{{qos}}". Invalid values are shown as warnings in the editor, and nothing is subscribed.
Other keys in an item (for example a name for your own notes) are ignored.
A topic that YAML reads as a number or true/false is converted to text (- 123 subscribes to 123). The text may differ from what you wrote: 1.50 becomes 1.5, 007 becomes 7, 0x1F becomes 31 and TRUE becomes true. Quote the topic to use it exactly as written: - "1.50".
An item without a topic, with an empty topic, or with a topic that YAML reads as a list, a mapping or a date (such as 2024-01-01) is shown as a warning, and Subscribe all subscribes nothing. Quote such a topic to use it as text: - "2024-01-01".
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 |
|
|
Name shown in the outline |
qos and retain can also be written as strings, for example to use a variable: qos: "{{qos}}". Invalid values are shown as warnings in the editor and are not sent.
The topic follows the same rules as a subscription topic: a missing or empty topic, a list, a mapping or a date is shown as a warning and nothing is published. The same applies to will.topic, except that leaving it out or empty means no Last Will.
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
Define variables under variables: and reference them with {{name}} in connection settings, subscription topics, and publish entries.
A variable value can also use the variables defined above it.
If the variable is not defined, {{name}} is treated as a literal string.
To use a {{...}} literal that would otherwise match a defined variable, wrap it in an extra layer: {{{{name}}}}.
To keep VS Code responsive, values may not grow too large when they are read: variables can add up to 1,000,000 characters and up to 10,000,000 characters are read for them, and a list or mapping used where a single value is expected (e.g. through a YAML alias) can be up to 1,000 characters. A file that goes beyond this shows a warning on its first line, and nothing is connected, sent or converted.
variables:
host: broker.example.com
message: Hello, world!
base: sensors
topic: "{{base}}/greeting"
connection:
url: "mqtt://{{host}}:1883"
publish:
- topic: "{{topic}}"
payload: '{"message": "{{message}}", "template": "{{{{message}}}}"}'
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.
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.
Invalid settings, such as a missing url or a qos other than 0, 1 or 2, are shown as warnings in the editor.
Connection Block
Write connection settings in a block starting with # %%.
| Name |
Required |
Default |
Description |
| url |
✓ |
|
URL of MQTT broker |
| user |
|
|
User ID |
| password |
|
|
Password, or ${secret:<key>} to retrieve from Secret Storage. Avoid writing it in plain text. |
| clientId |
|
"mqtt_vsc_" + 10 random chars |
Client ID |
| ca |
|
|
Path to CA certificate file (relative to the first workspace folder). The file must be inside the workspace; symlinks pointing outside it are rejected. Server certificates are always verified for mqtts/ssl/tls/wss; set this when the broker uses a self-signed or private CA. |
| 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 |
# %%
url: mqtt://broker:1883
user: user
password: ${secret:my-broker}
Subscription Block
Write one topic per line in a block starting with # $$.
# $$
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.
The text after # ## is shown as the block's name in the outline.
| 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 them with {{name}} in the same way as in the YAML format, including the {{{{name}}}} escape.
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
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
- Right-click the
.mqtt file in the Explorer → MQTT: Convert to YAML (the file does not need to be open)
- 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). If that file already exists, a dialog asks before overwriting it
- 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
- The converted file sends the same messages in most cases, with these differences:
- Blank lines between a payload and the next block are not part of the converted payload (the legacy format sends them as trailing newlines)
- Only the first connection block (
# %%) is converted
- Variables in
reconnectPeriod, will_qos and will_retain are used in the converted file, while the legacy format ignores them and uses the defaults
- Variables keep their values, but may be written differently:
- A variable defined more than once is written once, with its last value
- A value that uses a variable defined again later (or the variable itself, as in
v={{v}}x) is written with that variable already replaced
- A line before the blocks that contains
=, such as a comment # memo a=b, is a variable in the legacy format, so it is converted as one
- Check the converted file before using it in place of the original
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
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.
mqtt://, tcp:// and ws:// send the password unencrypted, even when it is stored with ${secret:<key>}. A warning is shown for such connections unless the broker is on this machine (localhost, 127.x.x.x, ::1). Use mqtts:// or wss:// to protect it.
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
YAML format:
connection:
url: mqtt://broker:1883
username: user
password: "${secret:my-broker}"
Legacy format:
# %%
url: mqtt://broker:1883
user: user
password: ${secret:my-broker}
When connecting, the extension resolves the secret using the following steps:
| Situation |
Behavior |
Key exists and is allowed for the broker (e.g. mqtts://host:port) |
Stored value is used directly |
| Key exists but is not allowed for the broker yet |
A confirmation dialog asks whether to send the secret to that broker: Always Allow (remember the broker), Allow Once, or cancel |
| Key not found |
A password input dialog (showing the destination broker) is shown; the entered value is saved and allowed for that broker |
| A dialog is cancelled |
The connection is aborted |
Each secret is bound to the brokers you have allowed, so a .mqtt file cannot silently send a stored password to a different broker. A broker is identified by:
- the protocol: a secret allowed for
mqtts:// is not sent over unencrypted mqtt:// to the same host and port without asking (tcp:// counts as mqtt://, and ssl:// and tls:// as mqtts://)
- the host and port: a different port on the same host counts as a different broker
- for
mqtts:// and wss://, the ca file: the dialog shows it as mqtts://host:port (CA: "certs/ca.crt"). The content of the file counts, not its path, so the dialog is shown again when the CA certificate changes, for example after renewing a self-signed CA.
Secrets stored with earlier versions are not bound to any broker yet, so the confirmation dialog is shown once on their next use.
Use MQTT: Set Secret and MQTT: Delete Secret to manage stored secrets (see Commands).
Subscription Window
Received messages are shown in the subscription window, which opens when you subscribe. The extension keeps the messages (up to mqttclient.subscription.maxMessages), so closing the window does not lose them.
To open the window again, including the messages received while it was closed, click Show subscription window on the subscriptions block (shown once you have connected, also after disconnecting), or run MQTT: Show Subscription Window from the Command Palette (Ctrl+Shift+P / Cmd+Shift+P) while a .mqtt file is active. Press Clear in the window to empty it.
Type in Filter to show only some of the messages by topic (case-insensitive):
- Without wildcards, the messages whose topic contains the text are shown. Example:
temp matches home/sensor/temperature.
- With
+ or #, the text is matched like an MQTT topic filter: + matches exactly one topic level, and # at the end matches the remaining levels (also none). Example: sensors/+/temp, home/#.
Commands
Run these from the Command Palette (Ctrl+Shift+P / Cmd+Shift+P).
| Command |
Description |
| MQTT: Convert to YAML |
Convert a legacy .mqtt file to YAML format. Also in the editor and Explorer context menus. See Converting Legacy Format to YAML. |
| MQTT: Show Subscription Window |
Open the subscription window for the active .mqtt file |
| MQTT: Set Secret |
Store or update a secret by key (allowed brokers are kept) |
| MQTT: Delete Secret |
Delete a stored secret by key, together with its allowed brokers |
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 (up to 100000). Set to 0 for unlimited, but note that every message is then kept in memory until you press Clear. |
Workspace Trust
A .mqtt file and workspace settings decide which broker to connect to and what to send, including stored secrets. The extension is therefore disabled in Restricted Mode. This includes syntax highlighting: .mqtt files are shown as plain text until you trust the workspace.
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
js-yaml
Copyright (C) 2011-2015 by Vitaly Puzrin
Released under the MIT license
https://github.com/nodeca/js-yaml/blob/master/LICENSE