Skip to content

compas_eve.mqtt ¤

Classes¤

MqttTransport ¤

MqttTransport(
    host: str,
    port: int = 1883,
    client_id: str | None = None,
    codec: MessageCodec | 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.

  • 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)

Publish a message to a topic.

Parameters:

  • topic (Topic) –

    Instance of the topic to publish to.

  • message (Message) –

    Instance of the message to publish.

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.