# Sending a message using Mosquitto

{% note warning %}

Yandex IoT Core is no longer available to new users. 

Current users can create resources until November 1, 2026. Afterwards, the service will go read-only and cease to operate on December 1, 2026. For more information on the timing and procedure, see [Service shutdown](../../sunset.md).

{% endnote %}

You can send the following types of messages:

- Send data from a device to a registry using the `$devices/<device_ID>/events` or `$registries/<registry_ID>/events` topics.
- Send data from a device to a registry using the permanent `$devices/<device_ID>/state` or `$registries/<registry_ID>/state` topics.
- Send registry commands to a device using the `$devices/<device_ID>/commands` or `$registries/<registry_ID>/commands` topics.
- Send registry commands to a device using the permanent `$devices/<device_ID>/config` or `$registries/<registry_ID>/config` topics.

To receive messages, you need to subscribe to the sender. For information about how to do this, see [Subscribing a device or registry to receive messages using Mosquitto](mosquitto-subscribe.md).

On how to make sure a message has been successfully sent and delivered, see [Testing message delivery](../message-delivery-check.md).

{% note warning %}

Registry and device topics are not interconnected. If a device sends data to the device topic for telemetry data, you can only receive it by subscribing to this topic. The same is true for registry topics.

{% endnote %}

## Getting started {#before-you-begin}

Prepare the following:

1. [Registry](../../operations/registry/registry-create.md).
1. [A registry certificate](../../operations/certificates/registry-certificates.md).
1. [Device](../../operations/device/device-create.md).
1. [A device certificate](../../operations/certificates/device-certificates.md).
1. [Mosquitto](https://mosquitto.org), an open source MQTT message broker. It is used in instructions for sending messages and subscribing to devices. [Download and install](https://mosquitto.org/download/) it to work with commands given in instructions.

## Connecting to an MQTT server {#connect-mqtt-server}

To connect to the MQTT server, use the following parameters:
- [A certificate from the certificate authority](https://storage.yandexcloud.net/mqtt/rootCA.crt).
- **Server address**: `mqtt.cloud.yandex.net`.
- **Server port**: `8883`.
- **Protocol**: `TLSv1.2`.

## Sending a message with data {#pub-events}

Send a message with data using the following parameters:
- `-h`: MQTT server address.
- `-p`: MQTT server port.
- `--cafile`: Path to the certificate from the certificate authority.
- `--cert`: Path to the public part of the certificate.
- `--key`: Path to the private part of the device certificate.
- `-t`: Device topic.
- `-m`: Message text.
- `-q`: [Quality of service (QoS) level](../../concepts/index.md#qos).

{% note info %}

If you encounter an error while running the command, add the `--debug` flag to the command and try again. This flag outputs the debug log when running the command, which helps you diagnose the problem.

{% endnote %}

{% list tabs group=instructions %}

- Mosquitto {#mosquitto}

    - Send data to the device topic:

        ```
        mosquitto_pub -h mqtt.cloud.yandex.net \
          -p 8883 \
          --cafile rootCA.crt \
          --cert device-cert.pem \
          --key device-key.pem \
          -t '$devices/<device_ID>/events' \
          -m 'Test data' \
          -q 1
        ```

    - Send data to the permanent device topic:

        ```
        mosquitto_pub -h mqtt.cloud.yandex.net \
          -p 8883 \
          --cafile rootCA.crt \
          --cert device-cert.pem \
          --key device-key.pem \
          -t '$devices/<device_ID>/state' \
          -m 'Test data' \
          -q 1
        ```

        A registry subscribed to this topic will know which device sent the data because the topic contains a unique device ID.

  - Send data to the registry topic:

        ```
        mosquitto_pub -h mqtt.cloud.yandex.net \
          -p 8883 \
          --cafile rootCA.crt \
          --cert device-cert.pem \
          --key device-key.pem \
          -t '$registries/<registry_ID>/events' \
          -m 'Test data' \
          -q 1
        ```

    - Send data to the permanent registry topic:

        ```
        mosquitto_pub -h mqtt.cloud.yandex.net \
          -p 8883 \
          --cafile rootCA.crt \
          --cert device-cert.pem \
          --key device-key.pem \
          -t '$registries/<registry_ID>/state' \
          -m 'Test data' \
          -q 1
        ```

        A registry subscribed to this topic will not know which device sent the data because the topic does not contain a unique device ID.

{% endlist %}

## Sending messages with commands {#pub-commands}

A registry can send messages with commands to one, multiple, or all devices added to it. Let's look at all the options.

Send a message with a command using the following parameters:
- `-h`: MQTT server address.
- `-p`: MQTT server port.
- `--cafile`: Path to the certificate from the certificate authority.
- `--cert`: Path to the public part of the registry.
- `--key`: Path to the private part of the registry certificate.
- `-t`: Device topic.
- `-m`: Message text.
- `-q`: [Quality of service (QoS) level](../../concepts/index.md#qos).

{% note info %}

If you encounter an error while running the command, add the `--debug` flag to the command and try again. This flag outputs the debug log when running the command, which helps you diagnose the problem.

{% endnote %}

{% list tabs group=instructions %}

- Mosquitto {#mosquitto}

    - Send a command to a single device:

        ```
        mosquitto_pub -h mqtt.cloud.yandex.net \
          -p 8883 \
          --cafile rootCA.crt \
          --cert registry-cert.pem \
          --key registry-key.pem \
          -t '$devices/<device_ID>/commands' \
          -m 'Test command for first device' \
          -q 1
        ```

    - Send a command to a single device using the permanent topic:

        ```
        mosquitto_pub -h mqtt.cloud.yandex.net \
          -p 8883 \
          --cafile rootCA.crt \
          --cert registry-cert.pem \
          --key registry-key.pem \
          -t '$devices/<device_ID>/config' \
          -m 'Test command for first device via permanent topic' \
          -q 1
        ```

    - Send a command to two devices:

        ```
        mosquitto_pub -h mqtt.cloud.yandex.net \
          -p 8883 \
          --cafile cert.pem \
          --cert registry-cert.pem \
          --key registry-key.pem \
          -t '$devices/<device_1_ID>/commands' \
          -t '$devices/<device_2_ID>/commands' \
          -m 'Test command for first and second device' \
         -q 1 # Quality of service (QoS) level 1.
        ```

    - Send a command to two devices using the permanent topic:

        ```
        mosquitto_pub -h mqtt.cloud.yandex.net \
          -p 8883 \
          --cafile cert.pem \
          --cert registry-cert.pem \
          --key registry-key.pem \
          -t '$devices/<device_1_ID>/config' \
          -t '$devices/<device_2_ID>/config' \
          -m 'Test command for first and second devices via permanent topic' \
          -q 1 # Quality of service (QoS) level 1.
        ```

    - Send a command to all devices added to the registry:

        ```
        mosquitto_pub -h mqtt.cloud.yandex.net \
          -p 8883 \
          --cafile cert.pem \
          --cert registry-cert.pem \
          --key registry-key.pem \
          -t '$registries/<registry_ID>/commands' \
          -m 'Test command for all devices' \
          -q 1
        ```

    - Send a command to all devices added to the registry using the permanent topic:

        ```
        mosquitto_pub -h mqtt.cloud.yandex.net \
          -p 8883 \
          --cafile cert.pem \
          --cert registry-cert.pem \
          --key registry-key.pem \
          -t '$registries/<registry_ID>/config' \
          -m 'Test command for all devices via permanent topic' \
          -q 1
        ```

{% endlist %}