Skip to main content

connectmq

Publish and subscribe on the free public MQTT broker broker.connectmq.com in one line, with no account needed. The package is a preconfigured paho-mqtt client and CLI for TCP, TLS, WebSocket and secure WebSocket. It also works with private brokers created on connectmq.com.

pip install connectmq
connectmq test
# OK  mqtts://broker.connectmq.com:8883  connect 220 ms, round-trip 46 ms

Requires Python 3.9 or later.

Quick start

import connectmq

prefix = connectmq.topic_prefix()  # e.g. "connectmq/demo/7f3a91"

# Publish (dicts and lists are sent as JSON)
connectmq.publish(f"{prefix}/temperature", {"value": 21.5}, retain=True)

# Subscribe: iterate over incoming messages
for msg in connectmq.subscribe(f"{prefix}/#", count=1, timeout=10):
    print(msg.topic, msg.json())

The public area is shared, so use a random prefix to keep your topics apart from other testers.

Reuse one connection

import connectmq

with connectmq.Client(transport="tls") as client:
    # Callback style: returns a function that unsubscribes
    stop = client.subscribe("connectmq/demo/7f3a91/#", lambda m: print(m.topic, m.payload))

    for i in range(10):
        client.publish("connectmq/demo/7f3a91/counter", i, qos=1)

    reply = client.once("connectmq/demo/7f3a91/reply", timeout=5)  # raises TimeoutError
    stop()

API

Function Description
publish(topic, payload, qos=0, retain=False, **options) Publish one message with a short-lived connection
subscribe(topics, qos=0, count=0, timeout=None, **options) Iterator over messages; stops after count messages or timeout seconds of silence
once(topic, timeout=30, **options) Next message on a topic filter; raises TimeoutError
connect(**options) / Client(**options) Connected client; use as a context manager
test(transport="tls", **options) Connects, publishes, receives; returns TestResult(ok, url, connect_ms, round_trip_ms, error)
broker_info() Endpoints and limits of the public broker
topic_prefix(root="connectmq/demo") Random topic prefix
broker_url(transport, host, port) URL for tcp, tls, ws or wss
topic_matches(filter, topic) Wildcard matching for +, # and $share/...

Client methods: publish(), subscribe(topics, callback, qos), messages(topics, qos, count, timeout), once() and close(). The underlying paho client is available as client.mqtt.

Callbacks are registered before the broker acknowledges a subscription, so retained messages are never missed.

Message has topic, payload (UTF-8 string), raw (bytes), qos, retain, received (datetime) and json().

Options

Option Default Notes
transport "tls" tcp 1883, tls 8883, ws 8083, wss 8084 (/mqtt)
host "broker.connectmq.com" Any MQTT broker
port Standard port
client_id py- plus a random suffix Must be unique and non-empty
username, password None Not needed on the public broker
protocol 5 5 for MQTT 5.0, 4 for MQTT 3.1.1
keepalive, timeout 60, 10.0 Seconds

Examples

Send sensor readings

import random, time, connectmq

with connectmq.Client() as client:
    while True:
        client.publish("connectmq/demo/7f3a91/sensor", {"temperature": round(random.uniform(20, 25), 1)})
        time.sleep(5)

Log messages to CSV

import csv, connectmq

with open("mqtt-log.csv", "a", newline="") as f:
    writer = csv.writer(f)
    for msg in connectmq.subscribe("connectmq/demo/7f3a91/#"):
        writer.writerow([msg.received.isoformat(), msg.topic, msg.payload])

Private ConnectMQ broker

import os, connectmq

client = connectmq.connect(
    host="YOUR_BROKER_HOST",
    transport="tls",
    username=os.environ["MQTT_USERNAME"],
    password=os.environ["MQTT_PASSWORD"],
)

CLI

connectmq test                                  # connectivity and round-trip
connectmq info                                  # endpoints and limits
connectmq prefix                                # random topic prefix
connectmq sub "connectmq/demo/7f3a91/#"
connectmq pub connectmq/demo/7f3a91/temp '{"value":21.5}' -q 1 -r
echo hello | connectmq pub connectmq/demo/7f3a91/msg
python -m connectmq test                        # if the script folder is not on PATH
Option Description
-t, --transport tcp, tls (default), ws, wss
-H, --host, -P, --port Broker host and port
-q, --qos 0, 1 or 2
-r, --retain Retained message (pub)
-n, --count Exit after n messages (sub)
-j, --json JSON line output (sub)
-u, --username, -p, --password Credentials for private brokers

Public broker

Protocol URL
MQTT (TCP) mqtt://broker.connectmq.com:1883
MQTT over TLS mqtts://broker.connectmq.com:8883
WebSocket ws://broker.connectmq.com:8083/mqtt
Secure WebSocket wss://broker.connectmq.com:8084/mqtt

Access is anonymous, but the client ID must not be empty. Limits: 64 KB per message, 10 messages per second and 10 subscriptions per client, topics up to 8 levels deep, and sessions expire after at most 30 minutes.

Everything on the public broker is public. Any client can subscribe to #. Use the broker only for testing, and always under a random topic prefix. For production, create a private broker with its own credentials at connectmq.com.

More: Free Public MQTT Broker for Testing. Also available for Node.js (@connectmq/mqtt), .NET (ConnectMQ.Mqtt), Node-RED (node-red-contrib-connectmq) and PowerShell (MQTTBroker).

License

MIT

Metadata

Release files for connectmq 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for connectmq 1.0.0
File Size Uploaded Provenance Actions
connectmq-1.0.0.tar.gz 9.5 kB Without provenance Details for connectmq-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for connectmq 1.0.0
File Interpreter ABI Platform Actions
connectmq-1.0.0-py3-none-any.whl Python 3 none any Details for connectmq-1.0.0-py3-none-any.whl

Total release size: 21.1 kB

Release files / connectmq-1.0.0.tar.gz

Download URL connectmq-1.0.0.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8842af07f102bf16ab0a51210275bd900fba63c3c9efa6c7429ec6091b52d7df
BLAKE2b-256 checksum
How to use checksums
6f836ace6585dd21628b182f9a584c0dbf098c1aec99495227f91267c8934b94
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / connectmq-1.0.0-py3-none-any.whl

Download URL connectmq-1.0.0-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
57d9cfe61083952157d4443d5400f9d70ce3991d00c9263e1e9fce85c1c51053
BLAKE2b-256 checksum
How to use checksums
f4b8d5d52a320dae6c283f96eac2215c621d9dd5cf0c7c00be44154d70a1ef3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for connectmq 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page