platypush.backend.mqtt

class platypush.backend.mqtt.MqttBackend(host: Optional[str] = None, port: int = 1883, topic='platypush_bus_mq', subscribe_default_topic: bool = True, tls_cafile: Optional[str] = None, tls_certfile: Optional[str] = None, tls_keyfile: Optional[str] = None, tls_version: Optional[str] = None, tls_ciphers: Optional[str] = None, username: Optional[str] = None, password: Optional[str] = None, listeners=None, *args, **kwargs)[source]

Backend that reads messages from a configured MQTT topic (default: platypush_bus_mq/<device_id>) and posts them to the application bus.

Triggers:

Requires:

  • paho-mqtt (pip install paho-mqtt)
__init__(host: Optional[str] = None, port: int = 1883, topic='platypush_bus_mq', subscribe_default_topic: bool = True, tls_cafile: Optional[str] = None, tls_certfile: Optional[str] = None, tls_keyfile: Optional[str] = None, tls_version: Optional[str] = None, tls_ciphers: Optional[str] = None, username: Optional[str] = None, password: Optional[str] = None, listeners=None, *args, **kwargs)[source]
Parameters:
  • host – MQTT broker host
  • port – MQTT broker port (default: 1883)
  • topic – Topic to read messages from (default: platypush_bus_mq/<device_id>)
  • subscribe_default_topic – Whether the backend should subscribe the default topic (default: platypush_bus_mq/<device_id>) and execute the messages received there as action requests (default: True).
  • tls_cafile – If TLS/SSL is enabled on the MQTT server and the certificate requires a certificate authority to authenticate it, ssl_cafile will point to the provided ca.crt file (default: None)
  • tls_certfile – If TLS/SSL is enabled on the MQTT server and a client certificate it required, specify it here (default: None)
  • tls_keyfile – If TLS/SSL is enabled on the MQTT server and a client certificate key it required, specify it here (default: None) :type tls_keyfile: str
  • tls_version – If TLS/SSL is enabled on the MQTT server and it requires a certain TLS version, specify it here (default: None)
  • tls_ciphers – If TLS/SSL is enabled on the MQTT server and an explicit list of supported ciphers is required, specify it here (default: None)
  • username – Specify it if the MQTT server requires authentication (default: None)
  • password – Specify it if the MQTT server requires authentication (default: None)
  • listeners

    If specified then the MQTT backend will also listen for messages on the additional configured message queues. This parameter is a list of maps where each item supports the same arguments passed to the main backend configuration (host, port, topic, password etc.). Note that the message queue configured on the main configuration will expect valid Platypush messages that then can execute, while message queues registered to the listeners will accept any message. Example:

    listeners:
        - host: localhost
          topics:
              - topic1
              - topic2
              - topic3
        - host: sensors
          topics:
              - topic4
              - topic5
    
run()[source]

Starts the backend thread. To be implemented in the derived classes if the loop method isn’t defined.

send_message(msg, topic: Optional[str] = None, **kwargs)[source]

Sends a platypush.message.Message to a node. To be implemented in the derived classes. By default, if the Redis backend is configured then it will try to deliver the message to other consumers through the configured Redis main queue.

Parameters:
  • msg – The message to send
  • queue_name – Send the message on a specific queue (default: the queue_name configured on the Redis backend)
stop()[source]

Stops the backend thread by sending a STOP event on its bus