Sending messages
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.
Yandex IoT Core provides two resource management models:
- When using a broker, you can send and receive messages in any topics that do not start with
$. - When using registries and devices, the available topics are limited.
For more information about the differences between brokers and device registries, see Resource relationships in Yandex IoT Core.
When using registries and devices, you can send messages of the following types:
- Send data from a device to a registry using the
$devices/<device_ID>/eventsor$registries/<registry_ID>/eventstopics. - Send data from a device to a registry using the retained
$devices/<device_ID>/stateor$registries/<registry_ID>/statetopics. - Send commands from a registry to a device using the
$devices/<device_ID>/commandsor$registries/<registry_ID>/commandstopics. - Send commands from a registry to a device using the retained
$devices/<device_ID>/configor$registries/<registry_ID>/configtopics.
To receive messages, you need to subscribe to the sender. Learn how to do this in Subscribing to messages.
To learn how to verify that the message was successfully sent and delivered, see Testing message delivery.
Warning
Registry topics and device topics are independent of each other. If a device sends data to a device telemetry topic, you can only receive this data by subscribing to that topic. The same applies to registry topics.
Getting started
Set up resources:
- Optionally, create registry and device certificates.
- Create a registry.
- Add a certificate to the registry.
- Create a device.
- Add a certificate to the device.
Authentication
To send messages, you need to get authenticated. You can use an X.509 certificate or a username and password for authentication. This section provides examples for both authentication methods.
Sending a data message
Note
If you encounter an error while running a command, add the --debug flag and retry the command. This flag outputs a debug log when running the command, which helps troubleshoot the issue.
Sending data from a device to a device topic
A registry subscribed to this topic will know which device sent the data because the topic contains a unique device ID.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder used by default is the one specified when creating the CLI profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also specify a different folder for any command using --folder-name or --folder-id. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.
-
Send data to a device topic using certificate-based authentication:
yc iot mqtt publish \ --cert device-cert.pem \ --key device-key.pem \ --topic '$devices/<device_ID>/events' \ --message 'Test data' \ --qos 1 -
Send data to a device’s retained topic using certificate-based authentication:
yc iot mqtt publish \ --cert device-cert.pem \ --key device-key.pem \ --topic '$devices/<device_ID>/state' \ --message 'Test data' \ --qos 1Where:
--certand--key: Certificate-based authentication parameters.--topic: Device topic for sending data.--message: Message text.--qos: Quality of service (QoS) level.
-
Send data to a device topic using username and password authentication:
yc iot mqtt publish \ --username <device_ID> \ --password <device_password> \ --topic '$devices/<device_ID>/events' \ --message 'Test data' \ --qos 1 -
Send data to a device’s retained topic using username and password authentication:
yc iot mqtt publish \ --username <device_ID> \ --password <device_password> \ --topic '$devices/<device_ID>/state' \ --message 'Test data' \ --qos 1Where:
--usernameand--password: Username and password authentication parameters.--topic: Device topic for sending data.--message: Message text.--qos: Quality of service (QoS) level.
To send device data to a device topic, use the publish REST API method for the DeviceData resource or the DeviceDataService/Publish gRPC API call.
Sending data from a device to a registry topic
A registry subscribed to this topic will not know which device sent the data because the topic does not contain a unique device ID.
-
Send data to a registry topic using certificate-based authentication:
yc iot mqtt publish \ --cert device-cert.pem \ --key device-key.pem \ --topic '$registries/<registry_ID>/events' \ --message 'Test data' \ --qos 1 -
Send data to a registry’s retained topic using certificate-based authentication:
yc iot mqtt publish \ --cert device-cert.pem \ --key device-key.pem \ --topic '$registries/<registry_ID>/state' \ --message 'Test data' \ --qos 1Where:
--certand--key: Certificate-based authentication parameters.--topic: Registry topic for receiving data.--message: Message text.--qos: Quality of service (QoS) level.
-
Send data to a registry topic using username and password authentication:
yc iot mqtt publish \ --username <device_ID> \ --password <device_password> \ --topic '$registries/<registry_ID>/events' \ --message 'Test data' \ --qos 1 -
Send data to a registry’s retained topic using username and password authentication:
yc iot mqtt publish \ --username <device_ID> \ --password <device_password> \ --topic '$registries/<registry_ID>/state' \ --message 'Test data' \ --qos 1Where:
--usernameand--password: Username and password authentication parameters.--topic: Registry topic for receiving data.--message: Message text.--qos: Quality of service (QoS) level.
To send data from a device to a registry topic, use the publish REST API method for the DeviceData resource or the DeviceDataService/Publish gRPC API call.
Sending a command message
A registry can send a command message to one, multiple, or all devices added to it. Let’s consider all options.
Note
If you encounter an error while running a command, add the --debug flag and retry the command. This flag outputs a debug log when running the command, which helps troubleshoot the issue.
Sending a command to one device
-
Send a command using certificate-based authentication:
yc iot mqtt publish \ --cert registry-cert.pem \ --key registry-key.pem \ --topic '$devices/<device_ID>/commands' \ --message 'Test command for first device' \ --qos 1 -
Send a command using a retained topic and certificate-based authentication:
yc iot mqtt publish \ --cert registry-cert.pem \ --key registry-key.pem \ --topic '$devices/<device_ID>/config' \ --message 'Test command for first device' \ --qos 1Where:
--certand--key: Certificate-based authentication parameters.--topic: Device topic for receiving commands.--message: Message text.--qos: Quality of service (QoS) level.
-
Send a command using username and password authentication:
yc iot mqtt publish \ --username <registry_ID> \ --password <registry_password> \ --topic '$devices/<device_ID>/commands' \ --message 'Test command for first device' \ --qos 1 -
Send a command using a retained topic and username and password authentication:
yc iot mqtt publish \ --username <registry_ID> \ --password <registry_password> \ --topic '$devices/<device_ID>/config' \ --message 'Test command for first device' \ --qos 1Where:
--usernameand--password: Username and password authentication parameters.--topic: Device topic for receiving commands.--message: Message text.--qos: Quality of service (QoS) level.
To send a command to a single device, use the publish REST API method for the RegistryData resource or the RegistryDataService/Publish gRPC API call.
Sending a command to all devices added to the registry
-
Send a command to all devices using certificate-based authentication:
yc iot mqtt publish \ --cert registry-cert.pem \ --key registry-key.pem \ --topic '$registries/<registry_ID>/commands' \ --message 'Test command for all devices' \ --qos 1 -
Send a command to all devices using a retained topic and certificate-based authentication:
yc iot mqtt publish \ --cert registry-cert.pem \ --key registry-key.pem \ --topic '$registries/<registry_ID>/config' \ --message 'Test command for all devices' \ --qos 1Where:
--certand--key: Certificate-based authentication parameters.--topic: Registry topic for sending commands.--message: Message text.--qos: Quality of service (QoS) level.
-
Send a command to all devices using username and password authentication:
yc iot mqtt publish \ --username <registry_ID> \ --password <registry_password> \ --topic '$registries/<registry_ID>/commands' \ --message 'Test command for all devices' \ --qos 1 -
Send a command to all devices using a retained topic and username and password authentication:
yc iot mqtt publish \ --username <registry_ID> \ --password <registry_password> \ --topic '$registries/<registry_ID>/config' \ --message 'Test command for all devices' \ --qos 1Where:
--usernameand--password: Username and password authentication parameters.--topic: Registry topic for sending commands.--message: Message text.--qos: Quality of service (QoS) level.
To send a command to all devices added to a registry, use the publish REST API method for the RegistryData resource or the RegistryDataService/Publish gRPC API call.
Sending a message in a broker
When using a broker, you can send a message to any topic using your username and password or any certificate assigned to the broker for authentication.
-
Send a message using username and password authentication:
yc iot mqtt publish \ --username <broker_ID> \ --password <broker_password> \ --topic /my/custom/topic \ --message 'Test broker message' --qos 1Where:
--usernameand--password: Username and password authentication parameters.--topic: Topic name that matches the MQTT specification and does not start with$.--message: Message text.--qos: Quality of service (QoS) level.
-
Send a message using certificate-based authentication:
yc iot mqtt publish \ --cert broker-cert.pem \ --key broker-key.pem \ --topic /my/custom/topic \ --message 'Test broker message' --qos 1Where:
--certand--key: Certificate-based authentication parameters.--topic: Topic name that matches the MQTT specification and does not start with$.--message: Message text.--qos: Quality of service (QoS) level.
To send a message in a broker, use the publish REST API method for the BrokerData resource or the BrokerDataService/Publish gRPC API call.