paho-mqtt
Connects Python applications to MQTT brokers through callback-based publish and subscription APIs.
- Downloads / mo
- 11.6M
- Stars
- 2.4K
- Last release
- 2.2.0
- License
- EPL-2.0 OR BSD-3-Clause
Strengths
- Supports several MQTT protocol generations. Connect to brokers using the newer or older supported protocol versions.
- Offers several network-loop integrations. Choose blocking, threaded, or external-event-loop integration for the application.
Limitations
- Persistent sessions remain in memory. With clean_session=False, recreating the client loses its saved session state.
- Accepted publishes can remain pending. Wait for completion before treating an accepted publish as sent.
Quickstart
import paho.mqtt.publish as publish publish.single('paho/test/topic', 'hello', hostname='localhost')
paho-mqtt examples
def _create_client(self) -> mqtt.Client: """Build a fresh paho client for a single connect attempt.""" client = mqtt.Client( callback_api_version=CallbackAPIVersion.VERSION2, client_id=self.mqtt_config.client_id, reconnect_on_failure=False, ) client.on_connect = self._on_connect client.on_disconnect = self._on_disconnect client.on_message = self._on_message client.on_subscribe = self._on_subscribe client.on_publish = self._on_publish client.will_set( self.mqtt_config.topic_prefix + "/available", payload="offline", qos=1, retain=True, ) if self.mqtt_config.tls_ca_certs is not None: if ( self.mqtt_config.tls_client_cert is not None and self.mqtt_config.tls_client_key is not None ): client.tls_set( self.mqtt_config.tls_ca_certs, self.mqtt_config.tls_client_cert, self.mqtt_config.tls_client_key, ) else: client.tls_set(self.mqtt_config.tls_ca_certs) if self.mqtt_config.tls_insecure is not None: client.tls_insecure_set(self.mqtt_config.tls_insecure) if self.mqtt_config.user is not None: client.username_pw_set( self.mqtt_config.user, password=self.mqtt_config.password, ) return client