PowerShell
The official MQTTBroker module is a pure PowerShell MQTT 3.1.1 client: no DLLs, no Mosquitto, no NuGet packages. It runs on Windows PowerShell 5.1 and PowerShell 7+ on Windows, Linux and macOS, so it suits scheduled tasks, admin scripts and quick tests.
How it connects
Install
Install-Module MQTTBroker -Scope CurrentUser| Command | What it does |
|---|---|
Connect-MQTTBroker | Opens a connection and returns it for reuse |
Publish-MQTTMessage | Publishes a string or byte[] payload; accepts pipeline input |
Receive-MQTTMessage | Subscribes and streams messages to the pipeline |
Disconnect-MQTTBroker | Sends DISCONNECT and closes the socket |
Test-MQTTBroker | Connects, publishes, receives and reports timings |
New-MQTTTopicPrefix, Get-MQTTBrokerInfo | Helpers for the public test broker |
Use Get-Help <command> -Full for every parameter.
Connect to your broker
Open a connection with your credentials, then pass it to the other commands with -Connection:
$c = Connect-MQTTBroker -Tls `
-ClientId "$($env:CMQ_CLIENT_ID)_ps-$env:COMPUTERNAME" `
-Username $env:CMQ_USERNAME `
-Password (Read-Host -AsSecureString 'Password')
Publish-MQTTMessage -Connection $c -Topic 'acme/istanbul/assembly/line1/pc-01/status' -Message '{"online":true}' -Qos 1
Disconnect-MQTTBroker $c-Tls connects to broker.connectmq.com on port 8883 with TLS 1.2 and checks the certificate against the system CA store. -Password takes a SecureString or a plain string.
Credentials only go through Connect-MQTTBroker
Publish-MQTTMessage and Receive-MQTTMessage have no -ClientId, -Username or -Password. Without -Connection they connect anonymously with a random ps-xxxxxxxx client ID, which only works on the public test broker. For your private broker, always call Connect-MQTTBroker with -ClientId set to your issued client ID plus a suffix (why), and pass the result as -Connection.
Connect-MQTTBroker parameter | Default | Notes |
|---|---|---|
-Tls | off | Use it: port 8883 with TLS. Without it, plain TCP on port 1883 |
-BrokerHost | broker.connectmq.com | |
-Port | 1883 / 8883 | |
-ClientId | ps- + random | Set it to <issued client ID>_<suffix> |
-Username, -Password | none | From MQTT Connection Settings |
-KeepAlive | 60 | Seconds |
-TimeoutSeconds | 10 | Connect timeout |
Publish
# Send CPU load every 5 seconds
while ($true) {
$cpu = (Get-CimInstance Win32_Processor | Measure-Object LoadPercentage -Average).Average
$json = @{ cpu = $cpu } | ConvertTo-Json -Compress
Publish-MQTTMessage -Connection $c -Topic "acme/istanbul/it/servers/$env:COMPUTERNAME/telemetry" -Message $json
Start-Sleep 5
}Send JSON objects (for example with ConvertTo-Json -Compress): the console turns every numeric field into a live value with a sparkline. Use -Qos 1 or 2 for delivery acknowledgement and -Retain to keep the last value on the broker. Publish an empty message with -Retain to clear a retained message.
Subscribe
# Print every message under acme/istanbul/
Receive-MQTTMessage -Connection $c -Topic 'acme/istanbul/#'
# Wait for one message, then parse it
$msg = Receive-MQTTMessage -Connection $c -Topic 'acme/istanbul/assembly/line1/robot-01/config' -Count 1 -TimeoutSeconds 30
$config = $msg.Payload | ConvertFrom-Json
# Log everything to CSV
Receive-MQTTMessage -Connection $c -Topic 'acme/istanbul/#' |
Select-Object Received, Topic, Payload |
Export-Csv mqtt-log.csv -NoTypeInformationEach message is an object with Topic, Payload (string), RawPayload (bytes), Qos, Retain and Received. -Topic accepts several filters, and + and # wildcards work. Without -Count or -TimeoutSeconds it runs until you press Ctrl+C, and sends keep-alive pings while it waits.
Test without an account
Test-MQTTBroker -Tls # connect, publish, receive against the public test broker
$prefix = New-MQTTTopicPrefix # e.g. connectmq/demo/7f3a91
Receive-MQTTMessage -Topic "$prefix/#" -Tls
Publish-MQTTMessage -Topic "$prefix/temperature" -Message '{"value":21.5}' -TlsThe public test broker needs no account, but anyone can read its topics: use it for experiments only, under a random prefix.
Notes
- The module speaks MQTT 3.1.1 over TCP or TLS only (no WebSockets). If port 8883 is blocked on your network, use a client that supports secure WebSockets on 8084, such as Node.js or Python.
- The same client is also published as the
connectmq.commodule (*-ConnectMQ*command names) and as single-file scripts (MQTTBroker-cli,connectmq.com-mqtt) that need no module install.