Skip to content

compas_eve.mqtt ¤

Classes¤

MqttTransport ¤

MqttTransport(
    host: str,
    port: int = 1883,
    client_id: str | None = None,
    codec: MessageCodec | None = None,
    transport: str = "tcp",
    tls: bool = False,
    tls_options: dict[str, Any] | None = None,
    *args,
    **kwargs
)

MQTT transport allows sending and receiving messages using an MQTT broker.

Parameters:

  • host (str) –

    Host name for the MQTT broker, e.g. broker.hivemq.com or localhost if you are running a local broker on your machine.

  • port (int, default: 1883 ) –

    MQTT broker port, defaults to 1883.

  • client_id (str | None, default: None ) –

    Client ID for the MQTT connection. If not provided, a unique ID will be generated.

  • transport (str, default: 'tcp' ) –

    Paho MQTT transport to use. Defaults to "tcp". Use "websockets" for MQTT over WebSockets.

  • tls (bool, default: False ) –

    If True, enables TLS by calling client.tls_set() before connecting.

  • tls_options (dict[str, Any] | None, default: None ) –

    Optional keyword arguments for client.tls_set(), e.g. ca_certs, certfile, keyfile, cert_reqs, tls_version, or ciphers. Providing this also enables TLS.

  • codec (MessageCodec | None, default: None ) –

    The codec to use for encoding and decoding messages. If not provided, defaults to JsonMessageCodec.

Functions¤

advertise ¤
advertise(topic: Topic) -> str

Announce this code will publish messages to the specified topic.

This call has no effect on this transport implementation.

Parameters:

  • topic (Topic) –

    Instance of the topic to advertise.

Returns:

  • str

    Advertising identifier.

close ¤
close() -> None

Close the connection to the MQTT broker.

on_ready ¤
on_ready(callback: Callable)

Allows to hook-up to the event triggered when the connection to MQTT broker is ready.

Parameters:

  • callback (Callable) –

    Function to invoke when the connection is established.

publish ¤
publish(topic: Topic, message: Message, **options)

Publish a message to a topic.

Parameters:

  • topic (Topic) –

    Instance of the topic to publish to.

  • message (Message) –

    Instance of the message to publish.

  • retain (bool) –

    If True, the broker retains the last message on this topic and delivers it immediately to any new subscriber. Defaults to False.

subscribe ¤
subscribe(topic: Topic, callback: Callable) -> str

Subscribe to a topic.

Every time a new message is received on the topic, the callback will be invoked.

Parameters:

  • topic (Topic) –

    Instance of the topic to subscribe to.

  • callback (Callable) –

    Callback to invoke whenever a new message arrives. The callback should receive only one msg argument, e.g. lambda msg: print(msg).

Returns:

  • str

    Returns an identifier of the subscription.

unadvertise ¤
unadvertise(topic: Topic)

Announce that this code will stop publishing messages to the specified topic.

This call has no effect on this transport implementation.

Parameters:

  • topic (Topic) –

    Instance of the topic to stop publishing messages to.

unsubscribe ¤
unsubscribe(topic: Topic)

Unsubscribe from the specified topic.

Parameters:

  • topic (Topic) –

    Instance of the topic to unsubscribe from.

unsubscribe_by_id ¤
unsubscribe_by_id(subscribe_id: str)

Unsubscribe from the specified topic based on the subscription id.

Parameters:

  • subscribe_id (str) –

    Identifier of the subscription.